javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Creating a Table A table fires change events when the contents of one of its cells is modified. Object[][] cellData = { {"row1-col1", "row1-col2"}, {"row2-col1", "row2-col2"}}; String[] columnNames = {"col1", "col2"}; JTable table = new JTable(cellData, columnNames); // The next two lines should be in one line. table.getModel().addTableModelListener( new MyTableChangedListener()); // Make the table scrollable. JScrollPane scrollPane = new JScrollPane(table); // The next two lines should be in one line. class MyTableChangedListener implements TableModelListener { public void tableChanged(TableModelEvent evt) { int row = evt.getFirstRow(); int column = evt.getColumn(); // The next three lines should all be in one line. Object data = ((TableModel)evt.getSource()).getValueAt( row, column); process(data); } } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/208.html (1 of 2) [8/1/2000 7:50:36 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Creating a Text Area The text area fires a document event whenever the text changes or some style on the text changes. TextArea textArea = new JTextArea("Line1\nLine2"); TextArea.getDocument().addDocumentListener( new MyDocumentListener()); // The next two lines should be in one line. class MyDocumentListener implements DocumentListener { public void insertUpdate(DocumentEvent evt) { // Some text was inserted. } public void removeUpdate(DocumentEvent evt) { // Some text was inserted. } public void changedUpdate(DocumentEvent evt) { // The style of some text was changed. } } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 19-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/209.html (1 of 2) [8/1/2000 7:50:37 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Displaying Simple HTML Files try { String url = "http://java.sun.com"; JEditorPane editorPane = new JEditorPane(url); editorPane.setEditable(false); JFrame frame = new JFrame(); frame.getContentPane().add(editorPane, BorderLayout.CENTER); frame.setSize(width, height); frame.setVisible(true); } catch (IOException e) { } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 25-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/210.html [8/1/2000 7:50:37 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Creating a Toolbar This example adds an image button to the toolbar. ImageIcon icon = new ImageIcon("image.gif"); JButton button = new JButton(icon); button.addActionListener(actionListener); JToolBar toolbar = new JToolBar(); toolbar.add(button); Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 25-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/211.html [8/1/2000 7:50:38 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Creating a Borderless Window JWindow window = new JWindow(); // Add component to the window. // The next two lines should be in one line. window.getContentPane().add(component, BorderLayout.CENTER); // Set initial size. window.setSize(300, 300); // Show the window. window.setVisible(true); < Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 25-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/212.html [8/1/2000 7:50:38 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Showing a Dialog Box // Modal dialog with OK button. JOptionPane.showMessageDialog(frame, "Line1\nLine2"); // Modal dialog with yes/no button. int answer = JOptionPane.showConfirmDialog( frame, "Line1\nLine2"); if (answer == JOptionPane.YES_OPTION) { // User clicked YES. } else if (answer == JOptionPane.NO_OPTION) { // User clicked NO. } // Modal dialog with OK/cancel and a text field String text = JOptionPane.showInputDialog( frame, "Line1\nLine2"); if (text == null) { // User clicked cancel } else { process(text); } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 25-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/213.html (1 of 2) [8/1/2000 7:50:39 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Creating Key Strokes and Binding Them to Actions // Some examples of keystrokes component.getInputMap().put(KeyStroke.getKeyStroke( "F2"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke( "control A"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke( "shift F2"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke( '('), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke( "button3 F"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke( "typed x"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke( "released DELETE"), "actionName"); component.getInputMap().put(KeyStroke.getKeyStroke( "shift UP"), "actionName"); component.getActionMap().put("actionName", new AbstractAction("actionName") { // The next two lines should be in one line public void actionPerformed( ActionEvent evt) { process(evt); } } ); Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/214.html (1 of 2) [8/1/2000 7:50:40 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Adding an InputMap to a Component InputMap inputMap = new InputMap(); // Add a KeyStroke inputMap.put(KeyStroke.getKeyStroke( "F2"), "actionName"); inputMap.setParent(component.getInputMap( JComponent.WHEN_FOCUSED)); component.setInputMap( JComponent.WHEN_FOCUSED, inputMap); Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 25-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/215.html [8/1/2000 7:50:41 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Setting a Tool Tip component.setToolTipText("aString"); Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 25-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/216.html [8/1/2000 7:50:41 AM] javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Laying Out Components in a Row or Column // Use Y_AXIS for a vertical column. Box box = new Box(BoxLayout.X_AXIS); box.add(component1); box.add(component2); Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan. Order this book from Amazon [ This page was updated: 25-Jul-2000 ] Products & APIs | Developer Connection | Docs & Training | Online Support Community Discussion | Industry News | Solutions Marketplace | Case Studies Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World FAQ | Feedback | Map | A-Z Index For more information on Java technology and other software from Sun Microsystems, call: (800) 786-7638 Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first. Copyright © 1995-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/217.html [8/1/2000 7:50:42 AM] [...]... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.swing/2 19. html [8/1/2000 7:50:43 AM] Code Samples from the Java Developers Almanac 2000 javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc... your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.swing.event/2 29. html (2 of 2) [8/1/2000 7:50:54 AM] Code Samples from the Java Developers Almanac 2000 javax.swing.filechooser Code Samples Index These code examples and other materials are subject to Sun Microsystems,... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/java.lang/73.html [8/1/2000 7:50: 59 AM] Code Samples from the Java Developers Almanac 2000 java.lang Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc Legal... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/java.lang/74.html [8/1/2000 7:50: 59 AM] Code Samples from the Java Developers Almanac 2000 java.lang Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc Legal... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.swing/222.html [8/1/2000 7:50:44 AM] Code Samples from the Java Developers Almanac 2000 javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.swing/224.html [8/1/2000 7:50:51 AM] Code Samples from the Java Developers Almanac 2000 javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.swing/225.html [8/1/2000 7:50:52 AM] Code Samples from the Java Developers Almanac 2000 javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.swing/226.html [8/1/2000 7:50:53 AM] Code Samples from the Java Developers Almanac 2000 javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc... e.printStackTrace(); } } } } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan Order this book from Amazon [ This page was updated: 19- Jul-2000 ] http://developer.java.sun.com/developer/codesamples/javax.swing.event/228.html (1 of 2) [8/1/2000 7:50:54 AM] Code Samples from the Java Developers Almanac 2000 javax.swing.event Code Samples Index These code examples and... Canada, dial your country's AT&T Direct Access Number first Copyright © 199 5-2000 Sun Microsystems, Inc All Rights Reserved Terms of Use Privacy Policy http://developer.java.sun.com/developer/codesamples/javax.swing/218.html [8/1/2000 7:50:42 AM] Code Samples from the Java Developers Almanac 2000 javax.swing Code Samples Index These code examples and other materials are subject to Sun Microsystems, Inc . A-Z Index Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/2 09. html (1 of 2) [8/1/2000 7:50:37 AM] javax.swing Code Samples. first. Copyright © 199 5-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/210.html. first. Copyright © 199 5-2000 Sun Microsystems, Inc. All Rights Reserved. Terms of Use. Privacy Policy. Code Samples from the Java Developers Almanac 2000 http://developer.java.sun.com/developer/codesamples/javax.swing/211.html