Download presentation
Presentation is loading. Please wait.
1
Java GUI programming with Swing Tim McKenna Seneca@York
2
Java 2 Swing Components l Java Foundation Classes(JFC) - Java 2 Swing components - Java2D graphics classes l the class JComponent(API) l http://java.sun.com/products/jfc http://java.sun.com/products/jfc l Swing Tutorial Swing Tutorial l Swing Connection Swing Connection
3
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,...
4
Swing Components and Events Event SourceEvent Type Generated JButtonActionEvent JTextFieldActionEvent (but don't listen for it) JMenuItemActionEvent JComboBoxItemEvent, ActionEvent JCheckBoxItemEvent JRadioButtonItemEvent (but don't listen for it) JListListSelectionEvent
5
Swing Components l Example(Online Reservation): Reservation.java – listens to everything ReservationTM.java – listens when it matters l a key point is WHEN components should be listened to. l components often do not need a listener l components can be inspected for their state: JTextField.getText() JRadioButton.isSelected() JCheckBox.isSelected() JComboBox.getSelectedIndex()
6
Swing Components Event Handling JRadioButton, JCheckBox: addItemListener( ) ItemListener interface: itemStateChanged( ) ItemEvent: getStateChange( )
7
Swing Components Event Handling JComboBox: addActionListener( ), getSelectedItem( ), getSelectedIndex( ) JTextField: addActionListener( ) ActionEvent: getActionCommand( )
8
Swing Components l Swing Component: JListJList l Example: JListDemo.java l Inspect JList: getSelectedIndex() - returns -1 if there is no selection. getSelectedIndices() - empty array if nothing is selected getSelectedValue() – returns null or object referred to in the list isSelectionEmpty() l Event Handling: addListSelectionListener() javax.swing.event.ListSelectionListener interface valueChanged()
9
Swing Components Example: JTextDemo.java Swing Components: JTextField, JTextArea, JButton l Event Handling: l JTextField: addActionListener( ) vs. getText( ) l JTextArea: NO listener is registered! append( ), insert( ), setText( ), getSelectedText( ), getText( )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.