Tải bản đầy đủ (.pdf) (46 trang)

Code Examplets phần 9 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.99 MB, 46 trang )



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
(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
(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 = "";
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
[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
[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
[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

(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
(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
[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
[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
[8/1/2000 7:50:42 AM]


javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Separating 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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:42 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 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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:43 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 Grid
When components are added to the container, they fill the grid
left-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(
rows, cols));
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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:43 AM]


javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Laying 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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:44 AM]


javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Setting a Border
This example lists the various borders that are available.

component.setBorder(
BorderFactory.createEmptyBorder());
component.setBorder(
BorderFactory.createLineBorder(
Color.black));
component.setBorder(

BorderFactory.createEtchedBorder());
component.setBorder(
BorderFactory.createRaisedBevelBorder());
component.setBorder(
BorderFactory.createLoweredBevelBorder());

ImageIcon icon = new ImageIcon("image.gif");
component.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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:44 AM]



javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Adding 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
titledBorder.setTitlePosition(
TitledBorder.BELOW_TOP);

component.setBorder(titledBorder);

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
(1 of 2) [8/1/2000 7:50:50 AM]


javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Creating 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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:51 AM]


javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Determining 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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:52 AM]


javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Setting the Look and Feel
To change the look and feel, you need to know the class name of
the new look and feel. This example changes it to the Windows look
and 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.
Code Samples from the Java Developers Almanac 2000
[8/1/2000 7:50:53 AM]



javax.swing
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Painting the Background of a Container with an Image
Pattern
This example shows how to use an image pattern to fill the
background of a container instead of a solid color. The method
used in this example makes use of a TexturePaint object to paint
the 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;
g2d.setPaint(texture);
// The next two lines should be in one line.
g2d.fillRect(0, 0, getSize(
).width, getSize().height);

}
}

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 ]
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:53 AM]


javax.swing.event
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Handling Hyperlink Events
Hyperlink events are fired by a JEditorPane when the user clicks on
a hyperlink.

try {
String url = "";
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 ]
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:54 AM]


javax.swing.event
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms

Handling Changes to a List Component
List data events are fired by a ListModel whenever one or more
items 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.
}
}
Code Samples from the Java Developers Almanac 2000
(1 of 2) [8/1/2000 7:50:54 AM]

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
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
(2 of 2) [8/1/2000 7:50:54 AM]


javax.swing.filechooser

Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Adding a Filter to a File Chooser Dialog
This example add a filter for .java files to the file chooser.

JFileChooser fileChooser =
new JFileChooser(new File(filename));
fileChooser.addChoosableFileFilter(new MyFilter());

// Open file dialog.
fileChooser.showOpenDialog(frame);
openFile(fileChooser.getSelectedFile());

class MyFilter extends
javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
String filename = file.getName();
return filename.endsWith(".java");
}
public String getDescription() {
return "*.java";
}
}

Examplets
TM
provided by permission of the publisher, Addision-Wesley, and
Author Patrick Chan.
Order this book from Amazon

[ This page was updated: 31-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
(1 of 2) [8/1/2000 7:50:55 AM]


javax.swing.tree
Code Samples Index
These code examples and other materials are subject to Sun Microsystems,
Inc. Legal Terms
Creating a Tree
This example creates a tree component with a root node and a
child of the root node. You build the tree hierarchy by adding nodes
to nodes.

DefaultMutableTreeNode root =
new DefaultMutableTreeNode("Root Label");
root.add(new DefaultMutableTreeNode(
"Node Label"));

JTree tree = new JTree(root);

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
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
[8/1/2000 7:50:56 AM]

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×