Make method that returns when JButton is pressed
Date : March 29 2020, 07:55 AM
With these it helps As you asked for an implementation with a mutex, here's what it would be like. I'm using an ActionListener though, but there's no busy wait in it. If that isn't what you desire, you atleast saw what Burkhard meant ;) public class MyButton extends JButton implements ActionListener
{
private Semaphore sem = new Semaphore(1);
public MyButton(String text) throws InterruptedException
{
super(text);
addActionListener(this);
sem.acquire();
}
@Override
public void actionPerformed(ActionEvent e) {
sem.release();
}
public void waitForPress() throws InterruptedException {
sem.acquire();
//do your stuff
sem.acquire();
//or just
//waitForPress()
//if you dont want it to end.
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame();
MyButton m = new MyButton("test");
frame.add(m);
frame.pack();
frame.setVisible(true);
m.waitForPress();
//another time, if you only want it to block twice
m.waitForPress();
}
}
|
How to make JButton reference object when pressed?
Tag : java , By : Senthil
Date : March 29 2020, 07:55 AM
wish helps you Add the Restraunts to a List Group them together (by their type) using a Map Use Collections.shuffle to randomise the List and select the first one Take a look at
|
JButton repeating action depending on how many times pressed
Tag : java , By : Valentine
Date : March 29 2020, 07:55 AM
To fix this issue The problem is called out in the comment section. You are declaring multiple listeners which are calling the respective deposit() or withdraw() method every time you perform an action. To avoid this. You can set one listener class to all of your buttons like this. private class MySpecialListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
if(e.getSource == btnDesposit) // do stuff and so on
}
}
MySpecialListener myListener = new MySpecialListener();
btnDeposit.addActionListener(myListener);
for(ActionListener al : btnEnter.getActionListeners())
btnEnter.removeActionListener(al)
btnEnter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
//convert text area into double
double depositNumber = Double.parseDouble(ta.getText());
sav.deposit(depositNumber);
btnWithdraw.setEnabled(true);
btnBalance.setEnabled(true);
}
});
|
JButton should change background when pressed works ONLY when pressed and mouse not over button
Tag : java , By : Ryan Adriano
Date : March 29 2020, 07:55 AM
may help you . Change color to red and you will observe that the problem is not what you think but a matter of look and feel. Background color of JButton is not what you think. The problem is that Swing uses an MVC model, and that the painting is not made by the JButton but by its associated ButtonUI. This ButtonUI (actually a subclass of) is dependent on the look and feel, so that the button is painted in compliance with the current look and feel. class MyUI extends javax.swing.plaf.basic.BasicButtonUI {
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
super.paintText(g,b,textRect,text);
}
}
public class SampleForm extends JFrame implements ActionListener, ChangeListener{
public SampleForm() {
super("Window");
fooButton = new JButton("jkljkl");
fooButton.addActionListener(this);
fooButton.addChangeListener(this);
fooButton.addMouseListener(this);
fooButton.setUI(new MyUI());
|
How to make the jButton action work not in just one run?
Tag : java , By : dummyadresse
Date : March 29 2020, 07:55 AM
|