Java GUI programming with Swing Tim McKenna Seneca@York
Java 2 Swing Components Java Foundation Classes(JFC) - Java 2 Swing components - Java2D graphics classes the class JComponent(API) http://java.sun.com/products/jfc Swing Tutorial Swing Connection
Swing Components button: JButton label: JLabel item selection: JCheckBox, JRadioButton, JToggleButton object selection: JComboBox, JList, JSpinner text: JTextField, JTextArea, JEditorPane, JFormattedTextField, JPasswordField menus: JMenuBar, JMenu, JMenuItem, ...
Swing Components and Events Event Source Event Type Generated JButton ActionEvent JTextField ActionEvent (but don't listen for it) JMenuItem ActionEvent JComboBox ItemEvent, ActionEvent JCheckBox ItemEvent JRadioButton ItemEvent (but don't listen for it) JList ListSelectionEvent
Swing Components Example(Online Reservation): Reservation.java Swing Components: JRadioButton, JCheckBox, JComboBox, JTextField, JButton, JPanel Event Handling JRadioButton, JCheckBox: addItemListener( ) ItemListener interface: itemStateChanged( ) ItemEvent: getStateChange( )
Swing Components Event Handling JComboBox: addActionListener( ), getSelectedItem( ), getSelectedIndex( ) JTextField: addActionListener( ) ActionEvent: getActionCommand( )
Swing Components Example: JListDemo.java. Swing Components: JList, JButton Event Handling JList: addListSelectionListener(), getSelectedIndex(), getSelectedIndices() ListSelectionListener interface: valueChanged() javax.swing.event.ListSelectionEvent
Swing Components Example: JTextDemo.java Swing Components: JTextField, JTextArea, JButton Event Handling: JTextField: addActionListener( ) vs. getText( ) JTextArea: NO listener is registered! append( ), insert( ), setText( ), getSelectedText( ), getText( )