Cs201 swing

722 views

Published on

Hariprasanna V (9843824677)

  • Be the first to comment

Cs201 swing

  1. 1. GUIs in Java Swing, Events CS201, SW Development Methods
  2. 2. Why Study GUIs in CS201 <ul><li>First, why not? </li></ul><ul><ul><li>Complex topic, complex library </li></ul></ul><ul><ul><li>Many classes, methods </li></ul></ul><ul><ul><li>Hard to do this well initially </li></ul></ul><ul><li>Reasons we study GUIs </li></ul><ul><ul><li>Again, example of inheritance in a framework, software reuse, etc. </li></ul></ul><ul><ul><li>Event-driven programming </li></ul></ul><ul><ul><ul><li>An important form of program-control </li></ul></ul></ul>
  3. 3. Swing <ul><li>Swing is a Java library (framework) for creating GUIs </li></ul><ul><ul><li>Part of a larger JFC (Java Foundation classes) </li></ul></ul><ul><ul><li>Replaces but uses an older library, AWT </li></ul></ul><ul><ul><li>Another, newer alternative: SWT </li></ul></ul><ul><ul><ul><li>Used in Eclipse </li></ul></ul></ul><ul><li>Swing apps will use look-and-feel of the system they’re running on </li></ul><ul><ul><li>Or can be set by the program </li></ul></ul>
  4. 4. Learning Swing <ul><li>Important things to learn </li></ul><ul><ul><li>Swing components </li></ul></ul><ul><ul><ul><li>E.g. buttons, text-fields, frames, etc. </li></ul></ul></ul><ul><ul><li>How to organize and arrange them </li></ul></ul><ul><ul><ul><li>Containers, layout managers </li></ul></ul></ul><ul><ul><li>How to make things change when something happens </li></ul></ul><ul><ul><ul><li>Event-based programming </li></ul></ul></ul>
  5. 5. Containment Hierarchy <ul><li>Top-level container: </li></ul><ul><ul><li>place for other Swing components to paint themselves </li></ul></ul><ul><ul><li>e.g., JFrame, JDialog, Japplet </li></ul></ul><ul><li>Intermediate container: </li></ul><ul><ul><li>simplify positioning of atomic components </li></ul></ul><ul><ul><li>e.g., JPanel, JSplitPane, JTabbedPane </li></ul></ul>
  6. 6. Components and Containers <ul><li>See pages 810-816 in text </li></ul><ul><li>All Swing GUI objects are Components </li></ul><ul><li>Some are also Containers </li></ul><ul><ul><li>Example: JFrame, JPanel, etc </li></ul></ul><ul><li>You place other Components inside Containers </li></ul><ul><ul><li>Example: a JFrame has buttons, text-fields, etc. </li></ul></ul><ul><ul><li>Example: a JPanel is part of a window, in which we organize GUI components </li></ul></ul>
  7. 7. What We Do with Containers <ul><li>Add components to them </li></ul><ul><li>Determine how these items will be arranged on the screen </li></ul><ul><ul><li>Layout control </li></ul></ul><ul><ul><li>We associate a Swing layout-manager with each container </li></ul></ul><ul><li>Layout is very hard to do at the beginning </li></ul><ul><ul><li>So we won’t sweat it in CS201 </li></ul></ul>
  8. 8. Non-Containers <ul><li>Atomic components: </li></ul><ul><ul><li>self-sufficient components that present information to and get input from the user </li></ul></ul><ul><ul><li>e.g., JButton, JLabel, JList, JComboBox, JTextField, JTable </li></ul></ul>
  9. 9. Swing <ul><li>Components and containers: </li></ul><ul><ul><li>superclasses and interfaces </li></ul></ul><ul><ul><li>extends and implements </li></ul></ul>© O’Reilly 1999
  10. 10. Top-Level Containers <ul><li>JFrame example: </li></ul><ul><ul><li>contains a single component JRootPane, which has a JMenuBar (optional) and a content pane </li></ul></ul><ul><ul><li>add non-menu components to its content panel </li></ul></ul><ul><ul><ul><li>theFrame.add( aButton ) </li></ul></ul></ul><ul><ul><li>Pre Java 5.0 </li></ul></ul><ul><ul><ul><li>theFrame.getContentPane().add( aButton ) </li></ul></ul></ul>
  11. 11. Events <ul><li>Two approaches to event handling </li></ul><ul><ul><li>read-evaluation loop (client-written loop) </li></ul></ul><ul><ul><li>notification-based (callbacks) </li></ul></ul><ul><li>Swing uses the 2nd approach </li></ul>
  12. 12. Events <ul><li>Swing: </li></ul><ul><ul><li>objects communicate by “firing” and “handling” events (event objects) </li></ul></ul><ul><ul><li>(conventional method call) </li></ul></ul><ul><ul><li>events are sent from a single source object to one or more registered listener objects </li></ul></ul>
  13. 13. Events <ul><li>Swing: </li></ul><ul><ul><li>different event sources produce different kinds of events e.g., a JButton object, when clicked, generates an ActionEvent object, which is handled by an ActionListener (an object whose class implements this interface) </li></ul></ul>
  14. 14. Events <ul><li>Handling: </li></ul><ul><ul><li>create a component </li></ul></ul><ul><ul><ul><li>e.g., a JButton </li></ul></ul></ul><ul><ul><li>add it to the GUI </li></ul></ul><ul><ul><ul><li>e.g., to a JPanel </li></ul></ul></ul><ul><ul><li>register a listener to be notified when the component generates an event </li></ul></ul><ul><ul><ul><li>e.g., interface ActionListener </li></ul></ul></ul><ul><ul><li>define the callback method </li></ul></ul><ul><ul><ul><li>e.g., actionPerformed() </li></ul></ul></ul>
  15. 15. Event Handling <ul><li>class MyListener implements ActionListener { … public void actionPerformed( ActionEvent event ) { // react to event … } } </li></ul><ul><li>… // instantiate event listener ActionListener listener = new MyListener(); … // instantiate event source JButton button = new JButton( “Hello” ); … // register event listener with event source button.addActionListener( listener ); </li></ul>

×