Part 2 Swing Chapter 14 18 14.1 JWindow 20 14.1.1 JWindow 25 14.1.2 JWindow 25 14.1.3 AWT 27 14.2 JDialog 28 14.2.1 JDialog 32 14.2.2 JDialog 33 14.2.3 AWT 36 14.3 JOptionPane 36 14.3.1 39 14.3.2 JOptionPane 41 14.3.3 43 14.3.4 49 14.3.5 52
6 14.3.6 58 14.3.7 JOptionPane 62 14.3.8 JOptionPane 65 14.3.9 JOptionPane 68 14.3.10 AWT 78 14.4 78 Chapter 15 80 15.1 JInternalFrame 81 15.1.1 JInternalFrame 85 15.1.2 JInternalFrame 87 15.1.3 JInternalFrame 94 15.1.4 AWT 100 15.2 JDesktopPane 100 15.2.1 JDesktopPane 107 15.2.2 JDesktopPane 107 15.2.3 JDesktopPane 107 15.2.4 AWT 109 15.3 DesktopManager 109 15.3.1 DefaultDesktopManager 117 15.4 118 Chapter 16 120 16.1 JFileChooser 121 16.1.1 126 16.1.2 130 16.1.3 134 16.1.4 144 16.1.5 149 16.1.6 JFileChooser 152 16.1.7 JFileChooser 156 16.1.8 JFileChooser 161 16.1.9 AWT 168
7 16.2 JColorChooser 168 16.2.1 170 16.2.2 175 16.2.3 JColorChooser 185 16.2.4 JColorChooser 186 16.2.5 JColorChooser 187 16.2.6 AWT 189 16.3 189 Chapter 17 190 17.1 193 17.1.1 AbstractListModel 195 17.1.2 DefaultListModel 196 17.2 202 17.3 204 17.4 JList 211 17.4.1 JList 212 17.4.2 JList 216 17.4.3 JList 233 17.4.4 AWT 238 17.5 238 Chapter 18 240 18.1 241 18.2 242 18.3 243 18.3.1 ComboBoxModel 245 18.3.2 MutableComboBoxModel 245 18.3.3 DefaultComboBoxModel 246 18.4 247 18.5 251 18.5.1 252 18.5.2 255 18.5.3 259
8 18.6 263 18.7 JComboBox 274 18.7.1 JComboBox 275 18.7.2 JComboBox 276 18.7.3 JComboBox 280 18.7.4 AWT 285 18.8 286 Chapter 19 288 19.1 290 19.1.1 292 19.2 293 19.2.1 294 19.2.2 TableModel 295 19.2.3 AbstractTableModel 297 19.2.4 DefaultTableModel 300 19.2.5 306 19.3 310 19.3.1 311 19.3.2 315 19.4 323 19.4.1 DefaultTableColumnModel 324 19.4.2 325 19.4.3 329 19.4.4 331 19.5 337 19.6 341 19.6.1 341 19.6.2 TableCellRenderer 347 19.6.3 CellEditor 353 19.6.4 TableCellEditor 355 19.6.5 TableCellEditor 357 19.7 368 19.7.1 368 19.7.2 371
9 19.8 373 19.8.1 377 19.9 382 19.9.1 JTableHeader 383 19.9.2 384 19.10 JTable 389 19.10.1 JTable 390 19.10.2 JTable 393 19.10.3 TableModelEvent 393 19.10.4 TableColumnModelEvent 397 19.10.5 ListSelectionEvent 399 19.10.6 JTable 401 19.10.7 AWT 408 19.11 408 Chapter 20 410 20.1 412 20.2 416 20.2.1 TreeNode 416 20.2.2 MutableTreeNode 417 20.2.3 DefaultMutableTreeNode 417 20.3 426 20.4 430 20.4.1 DefaultTreeModel 431 20.5 439 20.5.1 DefaultTreeSelectionModel 442 20.6 446 20.6.1 DefaultTreeCellRenderer 446 20.6.2 Metal 457 20.6.3 457 20.7 459 20.7.1 DefaultCellEditor 460 20.7.2 DefaultTreeCellEditor 461
10 20.8 466 20.8.1 Test 468 20.8.2 SelectableFile FileNode 470 20.8.3 471 20.8.4 473 20.9 JTree 476 20.9.1 JTree 477 20.9.2 JTree 479 20.9.3 JTree 493 20.9.4 AWT 500 20.10 500 Chapter 21 502 21.1 Swing 503 21.1.1 javax.swing.text 505 21.2 506 21.2.1 506 21.2.2 510 21.3 514 21.4 518 21.4.1 521 21.4.2 523 21.5 527 21.5.1 527 21.5.2 529 21.5.3 531 21.5.4 533 21.6 / 535 21.7 JTextComponent 539 21.7.1 JTextComponent 541 21.7.2 JTextComponent 543 21.8 548
11 Chapter 22 550 22.1 JTextField 551 22.1.1 555 22.1.2 558 22.1.3 562 22.1.4 JTextField 566 22.1.5 JTextField 567 22.1.6 JTextField 570 22.1.7 AWT 573 22.2 JPasswordField 574 22.2.1 JPasswordField 576 22.2.2 JPasswordField 576 22.3 JTextArea 578 22.3.1 JTextArea 582 22.3.2 JTextArea 582 22.3.3 AWT 585 22.4 JEditorPane 586 22.4.1 JEditorPane 589 22.4.2 JEditorPane 589 22.4.3 JEditorPane 592 22.5 JTextPane 595 22.5.1 595 22.5.2 597 22.5.3 JTextPane 605 22.5.4 JTextPane 605 22.5.5 AWT 608 22.6 608 Chapter 23 610 23.1 611 23.2 AttributeSet StyleConstants 614 23.3 617
12 23.4 621 23.4.1 622 23.5 StyleContext 626 23.6 633 23.7 636 Appendix A 637 Appendix B 641 655
CHAPTER 14 JWindow - JWindow - JWindow JDialog - JDialog - JDialog JOptionPane - - JOptionPane - - - -
Window Frame Dialog - 14-1 Frame
20 Chapter 14 Frame Dialog Window Window JRootPane JFrame Frame Window Dialog JWindow java.awt.window JRootPane JWindow JWindow - JWindow JLabel - - JWindow JLabel
14.1 JWindow 21 14-1 JWindow 14-1 JWindow import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JFrame { Toolkit toolkit = Toolkit.getDefaultToolkit(); JWindow window = new JWindow(); JLabel label = new JLabel(new ImageIcon("mandrill.jpg")); static public void main(string[] args) { JFrame frame = new Test(); public Test() { label.setborder(borderfactory.createraisedbevelborder()); window.getcontentpane().add(label, "Center"); centerwindow(); window.show(); window.addmouselistener(new MouseAdapter() { public void mousepressed(mouseevent e) { if(e.getclickcount() == 2) { window.dispose(); System.exit(0); ); private void centerwindow() { Dimension scrnsize = toolkit.getscreensize(); Dimension labelsize = label.getpreferredsize();
22 Chapter 14 int labelwidth = labelsize.width, labelheight = labelsize.height; window.setlocation(scrnsize.width/2 - (labelwidth/2), scrnsize.height/2 - (labelheight/2)); window.pack(); pack() show() pack() dispose() JWindow JComponent JWindow setborder() JWindow JWindow JRootPane - JWindow 14-2 JWindow
14.1 JWindow 23 - JFrame - 14-2 JWindow import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JApplet { JWindow window = new JWindow(); JMenuBar menubar = new JMenuBar(); JMenu filemenu = new JMenu("File"); JMenuItem quititem; public Test() { final Container contentpane = getcontentpane(); JButton button = new JButton("show window..."); JRootPane windowrootpane = window.getrootpane(); contentpane.setlayout(new FlowLayout()); contentpane.add(button); filemenu.add("new"); filemenu.add("open..."); filemenu.add("save"); filemenu.add("save As..."); filemenu.addseparator(); filemenu.add(quititem = new JMenuItem("Quit")); menubar.add(filemenu); window.getrootpane().setjmenubar(menubar); window.getrootpane().setborder(borderfactory.createraisedbevelborder()); button.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { Point pt = contentpane.getlocation(); SwingUtilities.convertPointToScreen(pt, contentpane); // window.setbounds(pt.x + 10, pt.y + 10, 200, 200); window.show(); quititem.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { window.dispose(); );
24 Chapter 14 ); JWindow JFrame JWindow JWindow - 14-1 JWindow java.awt.window - RootPane Container java.awt. Window A Accessible implements extends implements JRootPane rootpane JWindow rootpanecheckingenabled boolean 14-3 JWindow
14.1 JWindow 25 JWindow JWindow java.awt.window Accessible RootPaneContainer JWindow boolean protected JRootPane - JWindow 14-2 JWindow contentpane Container JPanel glasspane Component Component layeredpane JLayeredPane JLayeredPane rootpane JRootPane JRootPane contentpane JWindow glasspane layeredpane JLayeredPane rootpane JWindow JWindow AP I JWindow java.awt.window RootPaneContainer javax.accessibility.accessible
26 Chapter 14 public JWindow() public JWindow(Frame) public JWindow(GraphicsConfiguration) 1 public JWindow(Window) public JWindow(Window, GraphicsConfiguration) JWindow java.awt.frame JDialog JWindow SwingUtilities.getSharedOwnerFrame() protected void addimpl(component, Object, int) protected JRootPane createrootpane() protected boolean isrootpanecheckingenabled() protected void setrootpanecheckingenabled(boolean) protected void setrootpane(jrootpane) public void setlayout(layoutmanager) protected void windowinit() JWindow RootPaneContainer JWindow setrootpanecheckingenabled() isrootpanecheckingenabled() JWindow createrootpane() JRootPane JWindow protected setlayout() Window
14.1 JWindow 27 windowinit() JWindow JWindow protected RootPaneContainer public Container getcontentpane() public Component getglasspane() public JLayeredPane getlayeredpane() public JRootPane getrootpane() public void setcontentpane(container) public void setglasspane(component) public void setlayeredpane(jlayeredpane) RootPaneContainer RootPaneContainer JRootPane public AccessibleContext getaccessiblecontext() protected String paramstring() public void remove(component) 2 JWindow Accessible getaccessiblecontext() Accessible JComponent paramstring() JWindow java.awt.window Window Container remove()
28 Chapter 14 JWindow JDialog JRootPane JDialog JRootPane JDialog JOptionPane - - 14-4 JDialog ConstraintsPanel
14.2 JDialog 29 ConstraintsPanel - JPanel Frame null true JDialog getcontentpane() ConstraintsPanel pack() NOTE pack() public class Test extends JApplet { private ConstraintsPanel cp = new ConstraintsPanel(); private JPanel buttonspanel = new JPanel(); private JButton showbutton = new JButton("show dialog..."), okbutton = new JButton("OK"), applybutton = new JButton("Apply"), cancelbutton = new JButton("Cancel"); private JButton[] buttons = new JButton[] { okbutton, applybutton, cancelbutton, ; private JDialog dialog = new JDialog((Frame)null, // "Constraints Dialog",// true); // public Test() { Container contentpane = getcontentpane(); Container dialogcontentpane = dialog.getcontentpane(); contentpane.setlayout(new FlowLayout()); contentpane.add(showbutton); dialogcontentpane.add(cp, BorderLayout.CENTER); dialogcontentpane.add(buttonspanel, BorderLayout.SOUTH); dialog.pack(); //... for(int i=0; i < buttons.length; ++i) { buttonspanel.add(buttons[i]); addbuttonlisteners(); //...
30 Chapter 14 dispose() dispose() //... private void addbuttonlisteners() { showbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { // setlocationrelativeto() // dialog.setlocationrelativeto(test.this); dialog.show(); ); okbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { showstatus("ok button Activated"); dialog.dispose(); ); //... - - 14-3 JDialog import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JApplet { private ConstraintsPanel cp = new ConstraintsPanel(); private JPanel buttonspanel = new JPanel(); private JButton showbutton = new JButton("show dialog..."), okbutton = new JButton("OK"), applybutton = new JButton("Apply"), cancelbutton = new JButton("Cancel"); private JButton[] buttons = new JButton[] { okbutton, applybutton, cancelbutton, ; private JDialog dialog = new JDialog((Frame)null, // "Constraints Dialog",// true); // public Test() { Container contentpane = getcontentpane();
14.2 JDialog 31 Container dialogcontentpane = dialog.getcontentpane(); contentpane.setlayout(new FlowLayout()); contentpane.add(showbutton); dialogcontentpane.add(cp, BorderLayout.CENTER); dialogcontentpane.add(buttonspanel, BorderLayout.SOUTH); dialog.pack(); // // setlocationrelativeto() pack() // // setlocationrelativeto() // setlocationrelativeto() // dialog.setlocationrelativeto(this); for(int i=0; i < buttons.length; ++i) { buttonspanel.add(buttons[i]); addbuttonlisteners(); private void addbuttonlisteners() { showbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { // setlocationrelativeto() // dialog.setlocationrelativeto(test.this); dialog.show(); ); okbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { showstatus("ok button Activated"); dialog.dispose(); ); applybutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { showstatus("apply button Activated"); ); cancelbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { showstatus("cancel button Activated"); dialog.dispose(); ); JDialog -