12.07.2015 Views

Swing Widgets

Swing Widgets

Swing Widgets

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Swing</strong> <strong>Widgets</strong>10 Feb 2010CMPT166Dr. Sean HoTrinity Western University


Outline for today Event handling:● Types of events● Kinds of event handler interfaces <strong>Swing</strong> widgets:● Text: JLabel, JTextField, JPasswordField● Buttons: JButton● JCheckBox, JRadioButton, JComboBox Use ItemListener interface,itemStateChanged() method, andItemEvent object See JavaSE API docs or Google “javase (class)”CMPT166: widgets 10 Feb 20102


Event handlingwidget event listener Window (JFrame) creates widgets in constructor● JButton quit = new JButton(“Quit”);● Assigns listeners to each widget● quit.addActionListener( handler ); <strong>Widgets</strong> generate Events upon user interaction● Or create synthetically, e.g., timers Event is passed to corresponding listener● public void actionPerformed(ActionEvent e)● Listener acts accordingly● Screen is refreshed when listener returnsCMPT166: widgets 10 Feb 20103


Which widget fired the event? If all the widgets use the same listener, how canthat actionPerformed() method tellwhich widget generated an event?● public void actionPerformed(ActionEvent e) e.getSource() returns the widget (as an Object) e.getActionCommand() returns a string namefor the event (default: title of button) Can set the action command string directly:● JButton quitButton = new JButton(“Quit”);● quitButton.setActionCommand(“q”);CMPT166: widgets 10 Feb 20104


Using inner classes as listeners Another way: one inner class for each listener Each widget uses its own listener object Each listener is an instance of its own class● public MyWin extends JFrame { public MyWin() { }● JButton q = new JButton(“Quit”);● q.addActionListener( new QListener() ); private class QListener implementsActionListener {● public voidactionPerformed( ActionEvent e );CMPT166: widgets 10 Feb 20105


Types of events Event classes are in package java.awt.event The ActionListener interfaceuses the actionPerformed() method onan ActionEvent objectj a v a . l a n g . O b j e c tA c t i o n E v e n tC o n t a i n e r E v e n tj a v a . u t i l . E v e n t O b j e c tA d j u s t m e n t E v e n tF o c u s E v e n tj a v a . a w t . A W T E v e n tI t e m E v e n tC o m p o n e n t E v e n tP a i n t E v e n tW i n d o w E v e n tK eyC l a s s n a m eI n p u t E v e n tIn t e r f a c e n a m eK e y E v e n tM o u s e E v e n tCMPT166: widgets 10 Feb 20106


Other EventListener interfaces ActionListener is but one of manyinterfacesj a v a . u t i l . E v e n t L i s t e n e rfor handling events KeyListener: KeyEvent● Listen for keypresses MouseListener: MouseEvent● Press/release, enter/exit MouseMotion: MouseEvent● Move, dragK e yC la s s n a m eI n te rf a c e n a m eA c t i o n L i s t e n e rA d j u s t m e n t L i s t e n e rC o m p o n e n t L i s t e n e rC o n t a i n e r L i s t e n e rF o c u s L i s t e n e rI t e m L i s t e n e rK e y L i s t e n e rM o u s e L i s t e n e rM o u s e M o t i o n L i s t e n e rT e x t L i s t e n e rW i n d o w L i s t e n e rCMPT166: widgets 10 Feb 20107


JLabel Intended to be a text/image widget describinganother component Label1 = new JLabel( “Rotation” );● Change the text: label1.setText( “Rot” );● Add a tooltip: label1.setToolTipText( “Rotation in degrees” );● Add an icon: Icon rotIcon = new ImageIcon( “rot.gif” ); label1.setIcon( rotIcon );CMPT166: widgets 10 Feb 20108


Text fields JTextField:● Single-line widget for user to type in text text1 = new JTextField( 10 ); // field width text2 = new JTextField( “Type your name here” );● Read or change the text in the box with.getText() and .setText(String s)● Disable user editing: text1.setEditable( false ); JPasswordField: subclass that shows only dots JTextArea: allows multiple lines, word-wrapCMPT166: widgets 10 Feb 20109


JButton User clicks to trigger an ActionEvent Several types:● Command button, check box, toggle, radio Abstract superclass: javax.swing.AbstractButtonj a v a x . s w i n g . J C o m p o n e n tj a v a x . s w i n g . A b s t r a c t B u t t o nj a v a x . s w i n g . J B u t t o nj a v a x . s w i n g . T o g g l e B u t t o nj a v a x . s w i n g . J C h e c k B o xj a v a x . s w i n g . J R a d i o B u t t o n Icon rotIcon = new ImageIcon( “rot.png” ); Icon rotIconDown = new ImageIcon( “rotdn.png” ); rotButton = new JButton( “Rotate”, rotIcon ); rotButton.setRolloverIcon( rotIconDown );CMPT166: widgets 10 Feb 201010


JCheckBox and ItemListener JCheckBox uses a different listener interface: wireframe = new JCheckBox( “Wireframe” ); MyItemHandler handler = new MyItemHandler(); wireframe.addItemListener( handler ); ItemListener interfaceuses itemStateChanged() methodon an ItemEvent object: private class MyItemHandler implementsItemListener {public void itemStateChanged( ItemEvent event ) {if ( event.getSource() == wireframe ) {if ( event.getStateChange() ==ItemEvent.SELECTED ) {...CMPT166: widgets 10 Feb 201011


JRadioButton triButton = new JRadioButton( “Triangles”, false ); quadButton = new JRadioButton( “Quads”, true ); tristripButton = new JRadioButton( “Tristrips”,false ); Also uses ItemListener: MyItemListener handler = new MyItemListener(); triButton.addItemListener( handler ); Usually put radio buttons into a ButtonGroup: geomGroup = new ButtonGroup(); geomGroup.add( triButton ); geomGroup.add( quadButton ); geomGroup.add( tristripButton ); This is in addition to add()ing to the windowCMPT166: widgets 10 Feb 201012


JComboBox Drop-down list for user to choose one entry private String geom[] = { “Triangles”, “Quads”,“Tristrips” }; geomCombo = new JComboBox( geom ); Show only three rows at a time: geomCombo.setMaximumRowCount( 3 ); Also uses ItemListener interface See which entry user selected (0, 1, 2, etc.): geomCombo.getSelectedIndex()CMPT166: widgets 10 Feb 201013

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!