Now the JButton element of graphical user interface (GUI) from the Java’s language Swing Visual Components Library, when held pressed, triggers a multiple repetition of the same action as when it was clicked once. You can include a ready-made class that inherits from JButton in your Java project and simply use it.
In Java programming language the JButton-“button” of the Swing visual component library is, may be, the most commonly used element of the Graphical User Interface (GUI): when you click or “press” on it with the mouse, some action is performed. But how to make holding the “pressed” JButton-“button” trigger the multiple repetition of such an action, similar to how a button on a computer keyboard works, is described below.
The new jButton class, which inherits from the JButton class, implements the new functionality of the “button”-element of GUI:
public class jButton extends JButton { ... }
The jButton class constructor, detailed below, creates a button with an initial text, a user “action listener” as a event handler, a delay between the first action taken and the start of its multiple repetition, and the subsequent repetition period:
public jButton( String text, ActionListener listener, int delay, int period ) { ... }
Parameters:
The second parameter listener is a reference to an ActionListener object that implements the ActionListener interface. Therefore, the actionPerformed method must be defined in this class, in which the actions to be performed when the mouse is pressed on the button should be implemented, for example:
public class execOnPressed implements ActionListener { public void actionPerformed( ActionEvent event ) { ... // it has to do when the button is clicked or pressed } }
Now, when adding a jButton button to the graphical user interface, a reference to an instance of the execOnPressed class should be passed as a parameter (argument) to the jButton class constructor, for example:
add( new jButton("Button", new execOnPressed(), 600, 100) );
As a result, a button labeled “Button” will appear, similar in appearance to a button of the JButton class. When you click on such a button, the actions defined in the actionPerformed method of the execOnPressed class will first be performed once, but if the button is held down, after 0.6 seconds, they will begin to be performed again with a frequency of 0.1 seconds.
The above information is enough to start using the jButton class in your programs, which implements the repetition of an action while the button is held down. To do this, you just need to include the jButton.java file with the source code for the implementation of the jButton class in your Java project.
Let’s take a closer look at the implementation of the jButton class:
1 package myButtonRepeating; 2 3 import java.awt.event.*; 4 import javax.swing.*; 5 6 public class jButton extends JButton 7 { 8 protected ActionListener actionHandler; 9 protected Timer repeaTimer; 10 11 protected class repeaTimerListener implements ActionListener 12 { 13 public void actionPerformed( ActionEvent event ) 14 { 15 if( actionHandler != null ) actionHandler.actionPerformed( null ); 16 } 17 } 18 19 protected class MouseKeepTracker extends MouseAdapter 20 { 21 public void mousePressed( MouseEvent mouseEvent ) 22 { 23 if( actionHandler != null ) actionHandler.actionPerformed( null ); 24 repeaTimer.start(); 25 } 26 27 public void mouseReleased( MouseEvent mouseEvent ) 28 { 29 repeaTimer.stop(); 30 } 31 } 32 33 public jButton( String text, 34 ActionListener listener, 35 int delay, 36 int period 37 ) 38 { 39 super(text); 40 actionHandler = listener; 41 42 if( delay < 200 ) delay = 1000; 43 if( period < 20 ) period = 200; 44 45 repeaTimer = new Timer( delay, new repeaTimerListener() ); 46 repeaTimer.setDelay( period ); 47 addMouseListener( new MouseKeepTracker() ); 48 } 49 }
The repeated execution of the action assigned to the jButton button is the result of a repeated call to the actionPerformed method of the button click handler class, the reference to which was passed to the constructor of the jButton class as the listener parameter and which is now stored in the actionHandler field of the jButton class (8 and 40). To organize this re-call, the constructor of the jButton class creates an instance of the Timer class from the Swing visual component library, a reference to which is now stored in the repeatTimer field of the jButton class. The constructor of the Timer class is passed as a parameter a reference to an instance of the repeatTimerListener class, which also implements the ActionListener interface and which also has its own actionPerformed method. Now this method is called to handle timer events, in other words, it is called at the specified frequency. Inserted into this method is a call to the actionHandler.actionPerformed(…) method with actions to be performed when the button is pressed once and which must be repeated periodically while the button is held down (9, 45 and 11-17).
To fix the moments of “pressing” the mouse on the button and “release” the button, the mousePressed and mouseReleased methods of the MouseKeepTracker class are used, respectively, an instance of which is also created in the constructor of the jButton class and added as a handler for mouse events associated with the button (19-31 and 47). When the button is “pressed” with the mouse, the actionHandler.actionPerformed(…) method is immediately called once in the mousePressed method and the action required by pressing the button is performed once (23), then the timer is started (24) and the timer’s event handler periodically calls to this actionHandler.actionPerformed(…) method. Releasing the button simply stops the timer (29).
For a project created in the Eclipse IDE with an example of using the jButton class, see the archive file myButtonRepeating.zip. In this example, clicking on the button increments the value of the integer counter counter, which is displayed next to the button:
Below is the source code of the module with the main program class myButtonRepeater:
1 package myButtonRepeating; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 import javax.swing.*; 7 8 public class myButtonRepeater extends JFrame 9 { 10 protected JLabel label; 11 protected Integer counter; 12 13 public class execOnPressed implements ActionListener 14 { 15 public void actionPerformed( ActionEvent event ) 16 { 17 counter++; 18 label.setText( counter.toString() ); 19 } 20 } 21 22 public myButtonRepeater() 23 { 24 counter = new Integer( 0 ); 25 26 setLayout(new FlowLayout( FlowLayout.CENTER, 10, 10) ); 27 add( label = new JLabel( counter.toString() ) ); 28 29 add( new jButton(«Button», new execOnPressed(), 600, 100) ); 30 31 setTitle(«myButtonRepeater testing!»); 32 setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 33 setMinimumSize(new Dimension(320, 72)); 34 setLocation( new java.awt.Point( 320, 128 ) ); 35 setResizable(false); 36 setVisible(true); 37 } 38 39 public static void main( String[] args ) 40 { 41 SwingUtilities.invokeLater 42 ( 43 new Runnable() 44 { 45 public void run() 46 { 47 new myButtonRepeater(); 48 } 49 } 50 ); 51 } 52 }
Everything is very simple here. Only two visual components are added to the created application window of the JFrame type – this is a text label implemented in the JLabel class and the corresponding reference to the instance of which is stored by the label variable (10 and 27), and our instance of jButton class (29). The actionPerformed (15-19) method of the execOnPressed (13-20) class handles button presses, a reference to an instance of which is passed to the jButton class constructor as the second parameter (29).
Copyright © Sergii Zadorozhnyi, 2019
Please give us your valuable comment
You must be logged in to post a comment.