Object data = TableModelevt.getSource.getValueAt row, column; processdata; } } Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.. } } Ex
Trang 1Creating a Table
A table fires change events when the contents of one of its cells ismodified
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
Trang 2Creating a Text Area
The text area fires a document event whenever the text changes orsome 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
http://developer.java.sun.com/developer/codesamples/javax.swing/209.html (1 of 2) [8/1/2000 7:50:37 AM]
Trang 3Displaying 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
Trang 4Creating 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
http://developer.java.sun.com/developer/codesamples/javax.swing/211.html [8/1/2000 7:50:38 AM]
Trang 5Creating 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
Trang 6Showing 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
http://developer.java.sun.com/developer/codesamples/javax.swing/213.html (1 of 2) [8/1/2000 7:50:39 AM]
Trang 7Creating Key Strokes and Binding Them to Actions
// Some examples of keystrokes
ActionEvent evt) { process(evt);
} } );
Examplets TM provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon
Trang 8Adding an InputMap to a Component
InputMap inputMap = new InputMap();
// Add a KeyStroke inputMap.put(KeyStroke.getKeyStroke(
"F2"), "actionName");
inputMap.setParent(component.getInputMap(
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
http://developer.java.sun.com/developer/codesamples/javax.swing/215.html [8/1/2000 7:50:41 AM]
Trang 9Setting 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
Trang 10Laying 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
http://developer.java.sun.com/developer/codesamples/javax.swing/217.html [8/1/2000 7:50:42 AM]
Trang 11Separating Components in a Row or Column
Box box = new Box(BoxLayout.X_AXIS);
// Glue spreads the components as far apart as // possible
box.add(component1);
box.add(Box.createGlue());
box.add(component2);
// Strut spreads the components apart by a fixed // distance
int width = 10; box.add(Box.createHorizontalStrut(width));
box.add(component3);
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
Trang 12Laying Out Components in a Flow (Left-to-Right, Top-to-Bottom)
int align = FlowLayout.CENTER; // or LEFT, RIGHT JPanel panel = new JPanel(new FlowLayout(align));
panel.add(component1);
panel.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
http://developer.java.sun.com/developer/codesamples/javax.swing/219.html [8/1/2000 7:50:43 AM]
Trang 13Laying Out Components in a Grid
When components are added to the container, they fill the gridleft-to-right, top-to-bottom
int rows = 2; int cols = 2; // The next two lines should be in one line
JPanel panel = new JPanel(new GridLayout(
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
Trang 14Laying Out Components Using Absolute Coordinates
JPanel panel = new JPanel(null);
component.setBounds(x, y, w, h);
panel.add(component);
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
http://developer.java.sun.com/developer/codesamples/javax.swing/221.html [8/1/2000 7:50:44 AM]
Trang 15component.setBorder(BorderFactory.createMatteBorder( -1, -1, -1, -1, icon));
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
Trang 16Adding a Title to a Border
// Use default border
TitledBorder titledBorder = BorderFactory.createTitledBorder("Title");
// Create around existing border
titledBorder = BorderFactory.createTitledBorder(
border, "Title");
// Also available: DEFAULT_JUSTIFICATION, LEFT, // RIGHT
titledBorder.setTitleJustification(
TitledBorder.CENTER);
// Also available: DEFAULT_POSITION, ABOVE_TOP, TOP, // ABOVE_BOTTOM, BOTTOM, BELOW_BOTTOM
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
http://developer.java.sun.com/developer/codesamples/javax.swing/223.html (1 of 2) [8/1/2000 7:50:50 AM]
Trang 17Creating a Compound Border
// border1 is around border2 Border newBorder =
BorderFactory.createCompoundBorder(border1, border2);
component.setBorder(newBorder);
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
Trang 18Determining the Available Look and Feels
UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
for (int i=0; i<info.length; i++) { String humanReadableName = info[i].getName();
String className = info[i].getClassName();
// The className is used with // UIManager.setLookAndFeel() }
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
http://developer.java.sun.com/developer/codesamples/javax.swing/225.html [8/1/2000 7:50:52 AM]
Trang 19Setting the Look and Feel
To change the look and feel, you need to know the class name ofthe new look and feel This example changes it to the Windows lookand feel
try { // The next three lines should all be in one line UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.
WindowsLookAndFeel");
} catch (InstantiationException e) { } catch (ClassNotFoundException e) { } catch (UnsupportedLookAndFeelException e) { } catch (IllegalAccessException 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
Trang 20Painting the Background of a Container with an Image Pattern
This example shows how to use an image pattern to fill thebackground of a container instead of a solid color The methodused in this example makes use of a TexturePaint object to paintthe image pattern For information on how to convert an image to
a buffered image, see "Converting an Image to a Buffered Image"
public class ImageBgPanel extends JPanel { TexturePaint texture;
ImageBgPanel() { texture = new TexturePaint(bufferedImage, // The next two lines should be in one line new Rectangle(0, 0,
bufferedImage.getWidth(
), bufferedImage.getHeight()));
} // The class should override this method
public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g;
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 ]
http://developer.java.sun.com/developer/codesamples/javax.swing/227.html (1 of 2) [8/1/2000 7:50:53 AM]
Trang 21Handling Hyperlink Events
Hyperlink events are fired by a JEditorPane when the user clicks on
a hyperlink
try { String url = "http://java.sun.com"; JEditorPane editorPane = new JEditorPane(url);
editorPane.setEditable(false);
editorPane.addHyperlinkListener(
new MyHyperlinkListener());
} catch (IOException e) { }
class MyHyperlinkListener implements HyperlinkListener { public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED) { JEditorPane pane =
(JEditorPane)evt.getSource();
try { // Show the new page in the editor pane pane.setPage(evt.getURL());
} catch (IOException e) { 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 ]
Trang 22Handling Changes to a List Component
List data events are fired by a ListModel whenever one or moreitems are changed, added, or removed
JList list = new JList(new DefaultListModel());
list.getModel().addListDataListener(
new MyListDataListener());
class MyListDataListener implements ListDataListener { public void contentsChanged(ListDataEvent evt) { // A non-contiguous set of items were added, // removed, or modified
ListModel model = (ListModel)evt.getSource(); // The next tow lines should be in one line for (int i=evt.getIndex0(); i<=evt.getIndex1(); i++) {
process(model.getElementAt(i));
} } public void intervalAdded(ListDataEvent evt) { // The items between evt.getIndex0() and // evt.getIndex1(), inclusive, were added ListModel model = (ListModel)evt.getSource(); for (int i=evt.getIndex0();
i<=evt.getIndex1(); i++) {
process(model.getElementAt(i));
} } public void intervalRemoved(ListDataEvent evt) { // The items between evt.getIndex0()
// and evt.getIndex1(), inclusive, // have been removed
} }
http://developer.java.sun.com/developer/codesamples/javax.swing.event/229.html (1 of 2) [8/1/2000 7:50:54 AM]
Trang 23FAQ | 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