How can I keep executing work while a button is pressed? - java

I want to keep executing work while a button is pressed, using Java. When the button is released, the work should stop. Something like this:
Button_is_pressed()
{
for(int i=0;i<100;i++)
{
count=i;
print "count"
}
}
How might I achieve this?

One way:
Add a ChangeListener to the JButton's ButtonModel
In this listener check the model's isPressed() method and turn on or off a Swing Timer depending on its state.
If you want a background process, then you can execute or cancel a SwingWorker in the same way.
An example of the former:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ButtonPressedEg {
public static void main(String[] args) {
int timerDelay = 100;
final Timer timer = new Timer(timerDelay , new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed!");
}
});
JButton button = new JButton("Press Me!");
final ButtonModel bModel = button.getModel();
bModel.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent cEvt) {
if (bModel.isPressed() && !timer.isRunning()) {
timer.start();
} else if (!bModel.isPressed() && timer.isRunning()) {
timer.stop();
}
}
});
JPanel panel = new JPanel();
panel.add(button);
JOptionPane.showMessageDialog(null, panel);
}
}

I want to keep executing work while a button is pressed
Execute that process in another thread and then your form is not block and you can press the button to cancel or stop the execution.
see :
How to stop threads of a Java program?
Stop/cancel SwingWorker thread?
Control thread through button

You may need to use mousePressed event to start the action
And use mouseReleased event to stop the action (This is neccesary)
For more information refer here

For Android Apps
I know this question is old, but you can use:
while (yourButton.isPressed()) {
// Do Stuff
}

Related

Is it possible to automatically perform an event in java continuously?

For example, I want a JTextfield to display different random numbers continuously with start, stop and resume buttons. What is the possible solution to automatically update the JTextField continuously when the start button is pressed?
I tried using while loop inside the start button's action listener but it just makes the button stuck in the while loop.
This is the part of the code that I tried.
startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
while(true){
textField.setText(String.valueOf(random.nextInt()));
}
}
});
Read Concurrency in Swing.
You can use a javax.swing.Timer to change the text of the JTextField.
A tiny example:
public class TimerExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
JTextField field = new JTextField(10);
Timer timer = new Timer(100, e->{
field.setText(String.valueOf(Math.random()));
});
timer.start();
JOptionPane.showMessageDialog(null,field);
});
}
}
If you use while(true) in the Thread that runs the UI (this thread is called EDT - event dispatch thread), the thread won't be able to handle events since it is stucked inside the while loop.

JOptionPane.showMessageDialog - Why breaking down checkboxes?

I'm very interested, how to explain this, it's sth like hack? Or there is nothing to be excited about?
Maybe, you had some other, more intresting, experiences like this, when you were a beginer?
Warning! Its not a problem (It was realy helpful!), I just looking for more sense in my code :)
Sooo, in brief: Last if statment bans one checkbox...
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
class NotMain {
public static void main(String[] args) {
X x = new X();
}
}
class X implements ItemListener{
X() {
JCheckBox jCheckBox[] = new JCheckBox[2];
JPanel panel = new JPanel();
JFrame frame = new JFrame();
jCheckBox[0] = new JCheckBox("1");
jCheckBox[1] = new JCheckBox("2");
jCheckBox[0].addItemListener(this);
jCheckBox[1].addItemListener(this);
panel.add(jCheckBox[0]);
panel.add(jCheckBox[1]);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private int i;
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
i++;
} else i--;
if (i == 2) {
//if you comment next line, you'll be able to select two checkboxes
JOptionPane.showMessageDialog(null, "nope.");
}
}
}
The state of the check box changes when a mousePressed and mouseReleased event is generated for the check box. This is easy to demonstrate. Just do a mousePressed on the check box and then drag the mouse off the check box before releasing the mouse. The state doesn't change.
In your example the check box knows that a mousePressed and mouseReleased has been done so it generates and itemStateChanged event which causes the JOptionPane to be displayed.
The problem is that the ItemStateChange code executes before the mouseReleased code. Now when the mouseReleased code is executed the option pane has the focus so the state of the check box is not changed.
Change your code to:
if (i == 2)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JOptionPane.showMessageDialog(null, "nope.");
}
});
}
Now the display of the option pane is added to the end of the Event Dispatch Thread (EDT) which means both the mousePressed and mouseReleased events are handled by the check box before the JOPtionPane is displayed, so the state of the check box changes to selected.

Disable a Key Entirely Temporarily with Corresponding Button Java

I have a program that, put in short, advances upon the pressing of a button. During certain execution phases the button is temporarily deactivated to prevent it from firing in code at the wrong point in time. I have now created some Key Bindings to act as shortcuts for the pressing of the buttons, but need to disable them during the same aforementioned times, or else they will cause my array to be trashed and wiped before I even use it.
Any tips, methods, or Java methods I can use to [very] easily but a hold via disablement?
Have the bound key press the JButton with doClick(). Then when the button needs to be deactivated, call setEnabled(false) on the button.
As an aside, I suppose your button and key binding could share the same action, but I don't know if calling setEnabled(false) on the Action will prevent the key from running the Action's actionPerformed method. Time to test.... Be right back...
Edit: yep you can just have the JButton and the bound key share the same Action that is enabled/disabled:
import java.awt.event.*;
import javax.swing.*;
public class TestBoundAbstractActions {
public static void main(String[] args) {
final MyAction myAction = new MyAction();
final JButton actionButton = new JButton(myAction);
JRadioButton enableRadioButton = new JRadioButton("Enabled", true);
enableRadioButton.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
myAction.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
}
});
JPanel panel = new JPanel();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
String mKey = "m key";
panel.getInputMap(condition).put(KeyStroke.getKeyStroke(KeyEvent.VK_M, 0), mKey);
panel.getActionMap().put(mKey, myAction);
panel.add(new JLabel("Press \"m\" to activate key-bound action"));
panel.add(actionButton);
panel.add(enableRadioButton);
JOptionPane.showMessageDialog(null, panel);
}
}
class MyAction extends AbstractAction {
public MyAction() {
super("My Action");
putValue(MNEMONIC_KEY, KeyEvent.VK_M);
}
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("boo!");
}
}

Thread sleep in actionPerformed

I am trying to make a tiny program that has 3 buttons, all of them of white color. Pressing the first button (that has the text "Go!") will cause the second button to become orange for 3 seconds and then, after that time, it will become white again AND the third button will become permanently green.
However, in my following code, I have a problem achieving this: When hitting the button "Go!", it causes my program to somewhat freeze for 3 seconds and then the third button becomes green. Can you please help me?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Example extends JFrame
{
public Example(String title)
{
super(title);
GridLayout gl = new GridLayout(3,1);
setLayout(gl);
final JButton b1 = new JButton("Go!");
final JButton b2 = new JButton();
final JButton b3 = new JButton();
b1.setBackground(Color.WHITE);
b2.setBackground(Color.WHITE);
b3.setBackground(Color.WHITE);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b2.setBackground(Color.ORANGE);
try
{
Thread.sleep(3000);
} catch (InterruptedException ie) {}
b2.setBackground(Color.WHITE);
b3.setBackground(Color.GREEN);
}
});
add(b1);
add(b2);
add(b3);
setSize(50,200);
setVisible(true);
}
public static void main(String[] args)
{
Example ex = new Example("My Example");
}
}
Swing is single threaded. Calling Thread.sleep in the EDT prevents UI updates. Use a Swing Timer instead.
You're calling Thread.sleep(3000) on the main thread. Hence why your program freezes for three seconds. As #MarounMaroun suggested, you should use a SwingWorker. Here is the documentation.

How to wait for radio button to get selected in lost focus event

I have a Swing program that does a search based on the contents of some text fields and settings of a pair of radio buttons (in a button group). The program will automatically search when certain of the text fields lose focus. The problem comes in when the lose focus event is triggered by a click on one of the radio buttons. The lost focus event on the text field is getting processed before the radio button isSelected() values have changed, so the search is done with the "wrong" (i.e. old) parameters, instead of the parameters based on the new setting of the radio buttons.
I tried invoking the search using my own invokeWhenIdle method (shown below) to run the search after the event queue had settled down, but it still is using the old setting of the radio buttons.
My only working solution is to delay for 250 milliseconds in the lost focus event before running the search, so that the radio buttons have time to change. This works, but it makes the UI seem sluggish.
Any better ideas?
public static void invokeWhenIdle(final int a_max_retry, final Runnable a_runnable) {
if (a_max_retry <= 0) {
throw new IllegalStateException("invokeWhenIdle: Could not run " + a_runnable);
}
// get the next event on the queue
EventQueue l_queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
AWTEvent l_evt = l_queue.peekEvent();
if (l_evt == null) {
// nothing left on the queue (but us), we can do it
SwingUtilities.invokeLater(a_runnable);
} else {
// still something in the queue, try again
SwingUtilities.invokeLater(new Runnable() {
public void run() {
invokeWhenIdle(a_max_retry - 1, a_runnable);
}
});
}
}
Not an answer, but an explanation about what is happening. Maybe it will spark an idea...
The problem is that a mousePressed arms the button model and the mouseReleased actually changes the selected value of the model.
When you execute the FocusListener code the radio button the model is in an undefined state. Even if you add the FocusListener code to the end of the EDT by using invokeLater the code will still execute before the mouseReleased event is generated.
The following shows how you might code the listener to handle this. It assumes the state of the button is about to change:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FocusSSCCE extends JPanel
{
public FocusSSCCE()
{
final JRadioButton radio = new JRadioButton("Radio");
add( radio );
radio.setMnemonic('R');
JTextField textField = new JTextField(10);
add( textField );
JButton button = new JButton("Button");
add( button );
textField.addFocusListener( new FocusAdapter()
{
public void focusLost(FocusEvent e)
{
boolean isSelected = radio.isSelected();
// Assumes selected state will change
if (radio.getModel().isArmed())
isSelected = !isSelected;
System.out.println( isSelected );
}
});
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("FocusSSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new FocusSSCCE() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
However, even this approach can't be guaranteed to work. If for some reason the user generates the mousePressed event on the radio button and them moves the mouse away from the radio button before releasing the mouse, then the selected state of the radio button is not changed.
Similiarly, even your original implementation to sleep for 250ms can not be guaranteed to work because the user could theoretically hold down the mouse for more than 250ms which would also generate the wrong value.
My workaround for this was to make the radio buttons non focusable
I can't think of any better approach.
Edit:
I just thought of a wild solution.
textField.addFocusListener( new FocusAdapter()
{
public void focusLost(FocusEvent e)
{
if (e.getOppositeComponent() instanceof JRadioButton)
{
final JRadioButton radio = (JRadioButton)e.getOppositeComponent();
MouseListener ml = new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
System.out.println( radio.isSelected() );
radio.removeMouseListener(this);
}
};
radio.addMouseListener( ml );
}
else
System.out.println( radio.isSelected() );
}
});
Basically your processing code won't execute until the mouse has been released when you click on the radio button.

Categories