Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science mluckner@mini.pw.edu.pl http://www.mini.pw.edu.pl/~lucknerm
} Abstract Window Toolkit Delegates creation and behavior to the native GUI toolkit Portable, but on a lowest common denominator Extended by Swing Swing it is built on top of the AWT architecture mluckner@mini.pw.edu.pl 11/30/16 2
} Swing is part of the Java Foundation Classes (JFC) } All components are created and managed by Java } May be slower than AWT } Has a rich and convenient set of user interface elements } less prone to platform-specific bugs } gives a consistent user experience across platforms 3
} We will focus on Swing, but } The Eclipse integrated development environment uses a graphics toolkit called SWT } Oracle developed an alternate technology, called JavaFX, that may at some point become a replacement for Swing. 4
} Most Swing component classes start with a J JFrame JButton } They cam be easy mistaken with AWT components: Frame Button } Mixing AWT and Swing should be ommited However, it should works with Java 1.6 version 2 and later 5
} Create the frame. JFrame frame = new JFrame("FrameDemo"); } Allow the user to close the frame frame.setdefaultcloseoperation(jframe.ex IT_ON_CLOSE); } Create components and put them in the frame. frame.getcontentpane().add(emptylabel) } Size the frame. frame.pack(); } Show it. frame.setvisible(true); 6
7
} All Swing components whose names begin with "J" descend from the Jcomponent class } Selected features Tool tips Painting and borders Application-wide pluggable look and feel Support for layout Support for drag and drop Double buffering Key bindings 8
} JButton A common button } JCheck Box A check box button } JRadioButton One of a group of radio buttons. } JToggleButton Implements toggle functionality inherited by JCheckBox andjradiobutton. 9
} Menu Items have similar structure as buttons JMenuItem JCheckBoxMenuItem JRadioButtonMenuItem 10
} In Java you completely control how events are transmitted from the event sources (such as buttons or scrollbars) to event listeners. } You can designate any object to be an event listener } You can create more than one listener for the same event 11
MyListener listener = new MyListener (); JButton button = new JButton("Ok"); button.addactionlistener(liste ner); MyListener listener2 = new MyListener (); button.addactionlistener(liste ner2); class MyListener implements ActionListener { public void actionperformed(actioneven t event) { // reaction to button click goes here } } JButton ActionListener 12
} Not restricted to buttons } Examples an item is selected from a list box with a double click a menu item is selected the Enter key is clicked in a text field mluckner@mini.pw.edu.pl 11/30/16 13
} EventObject getsource() } AWTEvent extends EventObject } ActionEvent extends AWTEvent getactioncommand() 14
} Window Listeners } Mouse Listeners } Item Listeners } Property Change Listeners } and more 15
} Opening a window Showing a window for the first time. } Closing a window Removing the window from the screen. } Iconifying a window Reducing the window to an icon on the desktop. } Deiconifying a window Restoring the window to its original size. } Focused window The window which contains the "focus owner". } Activated window (frame or dialog) This window is either the focused window, or owns the focused window. } Deactivated window This window has lost the focus. } Maximizing the window Increasing a window's size to the maximum allowable size. 16
} WindowListener opening and closing the window, activation and deactivation of the window, iconification and deiconification of the window } WindowFocusListener becomes the focus owner loses the focus owner status } WindowStateListener iconified, deiconified, maximized, restored to normal. 17
} MouseListener mousepressed mousereleased mouseentered mouseexited mouseclicked } MouseMotionListener mousemoved mousedragged } MouseWheelListener mousewheelmoved 18
} MouseEvent getclickcount() getbutton() getx() gety() getpoint() } MouseWheelEvent extends MouseEvent getwheelrotation() 19
} Item events are fired by components that implement the ItemSelecable interface. check boxes, check menu items, toggle buttons, combo boxes. } Only one method public void itemstatechanged(itemevent e) 20
} getitem() Returns the item affected by the event. } getstatechange() Returns the type of state change (selected or deselected). } getitemselectable() Returns the originator of the event. 21
public class MyBean { private int value; private PropertyChangeSupport mpcs = new PropertyChangeSupport(this); public int getvalue() { return value;} public void setvalue(int value { mpcs.firepropertychange("value", this.value, value); this.value=value; } public void addpropertychangelistener(propertychangelist ener listener) { mpcs.addpropertychangelistener(listener); } public void removepropertychangelistener(propertychange Listener listener) { mpcs.removepropertychangelistener(listener); } } class MyListener implements PropertyChangeListener { public void propertychanged( PropertyChangeEvent e) } { } //action 22
} Swing's architecture enables multiple L&Fs CrossPlatformLookAndFeel Java build-in SystemLookAndFeel native Synth Defined by XML file 23
24
} BorderLayout } BoxLayout } CardLayout } FlowLayout } GridBagLayout } GridLayout } GroupLayout } SpringLayout 25