Handling User Events with Swing Program Swing: May re-do old Java program Update it using Swing
Swing Handles Different through use of classes of event listners ActionListener =click on button AdjustmentListener like scrollbar move FocusListener = keyboard (text field) ItemListener = like check box change KeyListener = enter text from keys
More Swing Handles MouseListener = clicks or moves out of area MouseMotionListener = mouse moves track it WindowListener = max, min, move, close it public class Suspense extends Jframe implements ActionListner, TextListener { …}
Setting up Components addActionListener( ) Jbutton…. addAdjustmentListener ( ) JScrollBar … addFocusListener( ) All Swing Components addItemListener ( ) ..Jbutton … addKeyListener ( ) All Swing Components addMouseListener( ) “ “ addMouseMotionListener( ) “ “
Last few addMouseMotionListener( ) All Swing Components addWindowListener( ) All Jwindow and Jframe Components Jbutton zap = new Jbutton( “Zap”); zap.addActionListener(this);
Program CHANGETITLE.JAVA Results of execution “Rosencrantz” “Guildenstern”
Working with Methods Action Events Jbutton, JCheckBox, JTextField, Jradio actionPerformed(ActionEvent evt) { ….}
Statements create JButton & JTextField Jbutton sort = new Jbutton(“Sort”); JTextField name = new JTextField( ); sort.setActionCommand(“Sort Files:); name.setActionCommand(“Sort Files”); Both cause same thing to happen
Adjustment Events Happen when JScrollBar component moved by means of arrows on the bar, box, clicks public void adjustmentValueChanged(AdjustmentEvent evt) { …}
AdjustmentEvent object’s getAdjustmentType( ) method UNIT-INCREMENT = +1 UNIT_DECREMENT = -1 BLOCK_INCREMENT = LARGER INCREASE BLOCK_DECREMENT = LARGER DECREASE TRACK = VALUE CHANGE - moving the box
WellAdjusted.java A scrollbar showing an integer of where the thumb button is in relation to the bar (position)
Null String In last program “ “ is a null String. The int value concatenated into a String “ “ + 73 is then a String “ 73” Focus Events = a gain/loss of focus on GUI The blinking field is in focus = text may then be entered
More Focus Events public void focusGained(FocusEvent evt) {…} public void focusLost(FocusEvent evt) { …} Determine which object has gained/lost focus
Item Events Occur when an item is selected/deselected (Jbutton, JCheckBox, JComboBox,JRadioButton) void itemStateChanged(ItemEvent evt) { …}
SelectItem.java Source code Produces output Which pick.additem(“ text of item”); Scroll down button click on item desired Text of item displayed
Key Events Occur when a key is pressed on the keyboard public void keyTyped(KeyEvent evt) {…}
Mouse Events A mouse click A mouse enters component’s area A mouse leaves “ “ MouseListener (5) Events: mouseClicked(MouseEvent) …ALSO mouseEntered, mouseExited, mousePressed, mouseReleased public void mouseReleased(MouseEvent evt) {…}.
More Mouse stuff getClickCount( ) getPoint( ) getX( ) getY( ) MOUSE-MOVEMENT EVENTS mouseDragged(MouseEvent evt) … mouseMoved(MouseEvent evt)...
Swing with old SPOTS & LINES Program A possible suggestion import java.awt.swing.*; With your old Spots/Lines program
Window Events WindowActivated(WindowEvent) windowClosed( ) windowClosing( ) windowDeactivated( ) windowDeiconified( ) windowIconified() windowOpened( )
Last Progam SwingColorTest.java Output Same as Old ColorTest program Red - Green - Blue OR Hue Saturation Brightness BUT with Swing Text official web site http://www.prefect.com/java21