Download presentation
Presentation is loading. Please wait.
1
Handling User Events with Swing
Program Swing: May re-do old Java program Update it using Swing
2
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
3
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 { …}
4
Setting up Components addActionListener( ) Jbutton….
addAdjustmentListener ( ) JScrollBar … addFocusListener( ) All Swing Components addItemListener ( ) ..Jbutton … addKeyListener ( ) All Swing Components addMouseListener( ) “ “ addMouseMotionListener( ) “ “
5
Last few addMouseMotionListener( ) All Swing Components
addWindowListener( ) All Jwindow and Jframe Components Jbutton zap = new Jbutton( “Zap”); zap.addActionListener(this);
6
Program CHANGETITLE.JAVA
Results of execution “Rosencrantz” “Guildenstern”
7
Working with Methods Action Events
Jbutton, JCheckBox, JTextField, Jradio actionPerformed(ActionEvent evt) { ….}
8
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
9
Adjustment Events Happen when JScrollBar component moved by means of arrows on the bar, box, clicks public void adjustmentValueChanged(AdjustmentEvent evt) { …}
10
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
11
WellAdjusted.java A scrollbar showing an integer of where the thumb button is in relation to the bar (position)
12
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
13
More Focus Events public void focusGained(FocusEvent evt) {…}
public void focusLost(FocusEvent evt) { …} Determine which object has gained/lost focus
14
Item Events Occur when an item is selected/deselected
(Jbutton, JCheckBox, JComboBox,JRadioButton) void itemStateChanged(ItemEvent evt) { …}
15
SelectItem.java Source code
Produces output Which pick.additem(“ text of item”); Scroll down button click on item desired Text of item displayed
16
Key Events Occur when a key is pressed on the keyboard
public void keyTyped(KeyEvent evt) {…}
17
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) {…}.
18
More Mouse stuff getClickCount( ) getPoint( ) getX( ) getY( )
MOUSE-MOVEMENT EVENTS mouseDragged(MouseEvent evt) … mouseMoved(MouseEvent evt)...
19
Swing with old SPOTS & LINES Program
A possible suggestion import java.awt.swing.*; With your old Spots/Lines program
20
Window Events WindowActivated(WindowEvent) windowClosed( )
windowClosing( ) windowDeactivated( ) windowDeiconified( ) windowIconified() windowOpened( )
21
Last Progam SwingColorTest.java
Output Same as Old ColorTest program Red - Green - Blue OR Hue Saturation Brightness BUT with Swing Text official web site
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.