import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class JComponentsTable extends JFrame { static final java.util.List frames = new ArrayList(); JComponentsTable(boolean scrollable) { super("Swing Components"); final JFrame f = this; frames.add(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { f.dispose(); } // Program ends when all the frames are closed. public void windowClosed(WindowEvent e) { super.windowClosed(e); frames.remove(f); if (frames.isEmpty()) { System.exit(0); } } }); // Create components // Components are interacting togather : // button with text area // text field with scrolled list JButton button = new JButton(Resources.BUTTON); // Uneditable text area with 3 lines and 20 colums. final JTextArea text = new JTextArea(Resources.TEXT_AREA, 3, 20); text.setEditable(false); text.setBackground(Resources.TEXT_BG); text.setBorder(BorderFactory.createLineBorder(Color.black)); // Button will change the text area. button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s = text.getText(); text.replaceRange(Resources.BUTTON_MSG, 0, s.length()); } }); // Label final JLabel label = new JLabel(Resources.LABEL); label.setBackground(Resources.LABEL_BG); label.setOpaque(true); label.setBorder(BorderFactory.createLineBorder(Color.black)); // Text field final JTextField textEntry = new JTextField(10); // Scrolled List final DefaultListModel listModel = new DefaultListModel(); JList list = new JList(listModel); JScrollPane listScrollPane = new JScrollPane(list); // Text field will change List and vice-versa textEntry.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { listModel.addElement(textEntry.getText()); } }); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { JList l = (JList) e.getSource(); String s = (String) l.getSelectedValue(); textEntry.setText(s); } }); // Drawing area JPanel graphics = new JPanel() { public void paintComponent(Graphics g) { g.drawOval(10, 10, 10, 10); g.drawLine(15, 20, 15, 45); g.drawLine(5, 30, 25, 30); g.drawLine(15, 45, 5, 55); g.drawLine(15, 45, 25, 55); } }; // The table of components JPanel mainPane = new JPanel(); mainPane.setLayout(new GridLayout(2,3,5,5)); mainPane.setBackground(Resources.FRAME_BG); mainPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); mainPane.add(button); mainPane.add(text); mainPane.add(label); mainPane.add(textEntry); mainPane.add(listScrollPane); mainPane.add(graphics); // Create Menu // The File menu allows to create a new table with same components, // to close a table, or to quit the application. // A dialog is used to confirm or cancel to quit. // The Color menu will change the color of the label in the // label component. JMenuBar menuBar = new JMenuBar(); // File menu JMenu menu = new JMenu(Resources.FILE_MENU); menu.add(new AbstractAction() { public Object getValue(String key) { return (key == NAME? Resources.NEW_ITEM : super.getValue(key)); } public void actionPerformed(ActionEvent e) { new JComponentsTable(true); } }); menu.add(new AbstractAction() { public Object getValue(String key) { return (key == NAME? Resources.CLOSE_ITEM : super.getValue(key)); } public void actionPerformed(ActionEvent e) { f.dispose(); } }); menu.addSeparator(); menu.add(new AbstractAction() { public Object getValue(String key) { return (key == NAME? Resources.QUIT_ITEM : super.getValue(key)); } public void actionPerformed(ActionEvent e) { tryToQuit(); } }); menuBar.add(menu); // Color menu menu = new JMenu(Resources.OPTION_MENU); ButtonGroup group = new ButtonGroup(); String [] colorsName = {Resources.RED, Resources.GREEN, Resources.BLUE}; Color [] colors = {Color.red, Color.green, Color.blue}; for (int i = 0; i < colors.length; i++) { ColorItem cItem = new ColorItem(colorsName[i], colors[i]); group.add(cItem); menu.add(cItem); cItem.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { ColorItem ci = (ColorItem) e.getSource(); if (ci.isSelected()) { label.setForeground(ci.getColor()); } } }); } ((ColorItem) group.getElements().nextElement()).setSelected(true); menuBar.add(menu); setJMenuBar(menuBar); // Add scrolls to the window if necessary if (scrollable) { JScrollPane scrollPane = new JScrollPane(mainPane); setContentPane(scrollPane); } else { setContentPane(mainPane); } pack(); setVisible(true); } // Dialog to confirm or cancel to quit private void tryToQuit() { Object[] options = {"Yes", "Cancel"}; int choice = JOptionPane.showOptionDialog(this, Resources.QUIT_MSG, Resources.QUIT_TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, // No special icon options, options[1]); // Default value if (choice != JOptionPane.OK_OPTION) { return; } Object [] allFrames = frames.toArray(); for (int i = 0; i < allFrames.length; i++) { ((JFrame) allFrames[i]).dispose(); } } public static void main(String [] args) { new JComponentsTable(true); } }