1 / 37

Java SWING (GUI ) And Event Driven Programming

Java SWING (GUI ) And Event Driven Programming. Atif Aftab Ahmed Jilani. AWT to Swing. AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends AWT New improved components Standard dialog boxes, tooltips, … Look-and-feel, skins

viho
Download Presentation

Java SWING (GUI ) And Event Driven Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java SWING (GUI) And Event Driven Programming AtifAftab Ahmed Jilani

  2. AWT to Swing • AWT: Abstract Windowing Toolkit • import java.awt.* • Swing: new with Java2 • import javax.swing.* • Extends AWT • New improved components • Standard dialog boxes, tooltips, … • Look-and-feel, skins • Event listeners

  3. Portability • The java.awt library contains heavyweight classes • Implemented via the Java Virtual Machine • Dependent on target platform • The javax.swing library contains lightweight classes • Implemented in Java • Independent of target platform

  4. GUI Class Hierarchy

  5. Container Classes

  6. GUI Helper Classes

  7. Swing Components

  8. Core Swing Components

  9. AWT Classes

  10. Frames • A frame is a window not contained inside another window

  11. Content Frame Delegation • In Java 1.4, required to add GUI elements to content panes: frame.getContentPane().add(new JButton(“OK”)); • In Java 1.5, frame delegates automatically to the content pane: frame.add(new JButton(“OK”));

  12. Anatomy of a GUI GUI Internal structure JFrame JFrame JPanel containers JPanel JButton JButton JLabel JLabel

  13. Using a GUI Component 2 • Create it • Configure it • Add children (if container) • Add to parent (if not JFrame) • Listen to it orderimportant

  14. Build from bottom up • Create: • Frame • Panel • Components • Listeners • Add: (bottom up) • listeners into components • components into panel • panel into frame Listener JButton JLabel JPanel JFrame

  15. Example • Create it • Instantiate object: b = new JButton(“press me”); • Configure it • Properties: b.text = “press me”; • Methods: b.setText(“press me”); • Add it • panel.add(b); • Listen to it • Events: Listeners JButton

  16. Application Code import javax.swing.*; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); } } press me

  17. Layout Manager FlowLayout GridLayout null none, programmer sets x,y,w,h Left to right, Top to bottom GridBagLayout BorderLayout CardLayout n c One at a time JButton w e s

  18. Create a new JTextField Example

  19. Create a new JTextField Create a new JPasswordField Create event handler Register event handler Make this JTextFielduneditable Create event handler class by implementing the ActionListener interface Declare actionPerformed method

  20. Test if the source of the event is the first text field Test if the source of the event is the second text field Test if the source of the event is the third text field Test if the source of the event is the password field Get text from text field Get text from text field Get text from text field Get password from password field

  21. Events and Listeners • An event can be defined as a type of signal to the program that something has happened. • The event is generated by external user actions such as mouse movements, mouse button clicks, and keystrokes, or by the operating system, such as a timer. • Events are responded to by event listeners

  22. Selected User Actions Source Event TypeUser Action Object Generated Click a button JButtonActionEvent Click a check box JCheckBoxItemEvent, ActionEvent Click a radio button JRadioButtonItemEvent, ActionEvent Press return on a text field JTextFieldActionEvent Select a new item JComboBoxItemEvent, ActionEvent Select an item from a List JListListSelectionEvent Window opened, closed, etc. WindowWindowEvent Mouse pressed, released, etc. Any ComponentMouseEvent Key released, pressed, etc. Any ComponentKeyEvent

  23. Java AWT Event Listener Interfaces • ActionListener • AdjustmentListener • ComponentListener • ContainerListener • FocusListener • ItemListener • KeyListener • MouseListener • MouseMotionListener • TextListener • WindowListener • ListSelectionListener All are in the java.awt.event or javax.swing.event package All are derived from EventListener in the java.util package NOTE: any object that will respond to an event must implement a listenerinterface.

  24. Selected Event Handlers Event Class Listener Interface Listener Methods (Handlers)ActionEventActionListeneractionPerformed(ActionEvent) ItemEventItemListeneritemStateChanged(ItemEvent) ListSelectionListSelectionvalueChanged Event Listener (ListSelectionEvent)

  25. Adapters • Listeners are typically interfaces, therefore requiring all methods be implemented • Use an adapter class to provide convenience implementations of unused methods: addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent event) { System.out.println("Window activated"); } });

  26. Event-Driven Programming (iii) • Listener classes respond to actions • e.g. override the java.awt.event.actionPerformed() method • Listener classes must be registered with the GUI objects they are listening for: • e.g. via the okButton.addActionListener() method

  27. Mouse Events (i)

  28. Mouse Events (ii)

  29. Keyboard Events • Capture keyboard events via the KeyListener interface: keyPressed(KeyEvent e) keyReleased(KeyEvent e) keyTyped(KeyEvent e) Home VK_HOME Page Up VK_PGUP Page Down VK_PGDN … Arrow Keys VK_UP VK_DOWN VK_RIGHT VK_LEFT

  30. Timer Events • Use timers (the java.awt.Timer class) to control animations, trigger other events, etc.

  31. The method for responding to an Action event. Implementing the listener interface Registering the frame to be a listener for action events generated by the two buttons Handling Simple Action Events

  32. actionPerformed is a method required for all ActionListeners Handling Simple Action Events – a closer look at the event-handling method An Event object’s getSource() method returns a reference to the Component object that generated the event.

  33. Alternative Approaches to Listening • Implement the listener with the main application class, and have the one listener assigned to all components generating the events • Advantage: simplicity for beginner programmers • Disadvantage: event-handler method may require if-statement or switch with several branches when multiple components generate the event • Use inner classes to implement the listeners and create a different instance as each component’s listener. • Named inner class or anonymous inner class (This is the approach used in the textbook most of the time) • Advantage: no need to test within the listeners for determining which component sent the event. Each component has its own dedicated listener • Disadvantage: harder to understand for novice programmers

  34. Inner class has direct access to all members (even private) of the outer class Example with named inner classes, one for listening to each button

  35. Example with anonymous inner classes, one for listening to each button

  36. Questions?

More Related