1> I have a JButton in Jframe.
2> The click of JButton opens new instance of another JFrame.
The problem is when a Key is pressed very fast on the above Jbutton .Two instances of the same JFrame opens up.
I have to open these frames. I knows there are other options also not using the Jframes as I read.
I managed to solve this problem for Doulbl click of Mouce by setMultiClickThreshHold('time in miliseconds'). But it worked only for mouse.
Tried some other stuffs which I got in google, But none worked.
Is there any other way to solve this issue?
For full control of how often/quickly again an Action is triggered, implement it to disable itself in its actionPerformed. Something like:
singlePerform = new AbstractAction("DoSomthing") {
#Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
doSomething();
}
};
JButton button = new JButton(singlePerform);
When it's safe for doSomething to be triggered again, simply re-enable the Action:
singlePerform.setEnabled(true);
Related
I am designing Reversi game in java, and the structure is as follows:
a. A JFrame with 64 buttons in it. The buttons are stored in an array.
b. The JButtons will have black circles or white circles.
So whenever a move is to be made, the program will highlight those boxes where a move can be made, but how can I know which button (I want to know the index of that button) has been clicked when all are highlighted the same way?
From my understanding, you are attempting to detect when a specific JButton is pressed.
The simplest way to do this is by implementing an ActionListener.
public class ExampleClass implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonNameOne)
System.out.println("Button One was pressed");
else if (e.getSource() == buttonNameTwo)
System.out.pringln("Button Two was pressed);
}
}
Detecting an action
The actionPerformed(ActionEvent e) method will activate whenever any button is pressed.
Recording source of action
When it is pressed, it automatically detects the source of this action (the button) and stores it in parameter "e".
Using recorded source of action
By simply doing e.getSource() you are able to get the component which invoked this method and compare it to pre-existing components in your program.
Customized arguments
With each if statement, you are able to customize and personalize the result of the condition (which is if the button being pressed is equal to a specific button). Do this by putting arguments within the body of each conditional statement:
if (e.getSource == sayHiButton)
System.out.println("Hi");
You probably have one ActionListener added to all buttons. Then the ActionEvent getSource passed to performAction has info. That is ugly, like testing the button text.
What is more normal is to use Action (take a look) and setting different actions bearing the 64 states.
public BoardAction extends AbstractAction {
public BoardAction(int x, int y) { ... }
#Override
public void actionPerformed(ActionEvent e) {
...
}
}
JButton button = new JButton(new BoardAction(x, y));
In an Action you can also specify the button caption, and an Action can also be (re)used in a JMenuItem and such.
Because of the extra indirection needed, most examples use an ActionListener,
but swing interna use Action quite often. For instance having an edit menu with cut/copy/pase and a toolbar with cut/copy/paste icons, context menus.
I write desktop application using Swing. I have JFrame and many JButton and JTextField inside frame.
Using KeyEventDispatcher I catch spacebar press event Keyevent.VK_SPACE and click in cancelButton
new KeyEventDispatcher(KeyEvent.VK_SPACE) {
#Override
public void performAction(KeyEvent event) {
cancelButton.doClick();
cancelButton.requestFocus();
}
}
It works, but on cancelButton other event change his state to cancelButton.setEnabled(false) and when I press spacebar after disable this cancelButton then other button in the same frame is clicked. I think this is option of system but maybe can I blocked this ? I care about this that the pressing spacebar do one thing which is declared in KeyEventDispatcher.
I think about change this spacebar to other letter from keyboard but I want to know that I can solved this problem using spacebar.
Any idea how to resolve this problem?
EDIT
I try with CTRL key but at every press this keys other button is highlighted like pressing TAB. The same action is with C key also when set at JTextField it changes value of this field.
SOLVED
I solved it by adding event.consume() and remove cancelButton.requestFocus()
new KeyEventDispatcher(KeyEvent.VK_SPACE) {
#Override
public void performAction(KeyEvent event) {
event.consume();
cancelButton.doClick();
}
}
I am developing a Java Swing App, and I want to use JRadioButton objects to show state. I don't want the user to have the ability to select them. If I use the button's .setEnabled(false) method, the radio button is greyed out.
I don't want the Radio Button to grey out. Is there a way to override this?
Well You Could Do Something Like:
boolean rButtonEnabled = false;
JRadioButton rButton = new JRadioButton();
rButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
rdbtnNewRadioButton.setSelected(rButtonEnabled);
}
});
It's not the prettyist solution, but I hope it helps.
To store the new value perhaps you can create a new class extending JRadioButton.
While this does not use the .setEnabled(), it is effectively the same
I cant seem to find a way to prevent a SwingWorker from being called twice on a double click.
The issue is that simply setting the JButton to setEnabled(false) doesn't prevent someone from double clicking fast enough to run it more than once.
startButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
makeItSo();
}
});
private void makeItSo () {
startButton.setEnabled(false);
myWorker myW = new myWorker();
myW.execute(); // Executes allot of work. But errors if this is running more than once.
}
Don't use a MouseListener for buttons, you should be using an ActionListener
See How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener for more details
Use ActionListener instead of MouseListener
why you use ActionListener, An ActionListener is used to handle the logical click of a button.
A click happens
- when the mouse is pressed then released on a button,
- when the keyboard shortcut of that button is used,
- when the button has the focus and the space bar is pressed,
- when the button is the default button and Enter is pressed,
- when the button's click() method is called programmatically
A MouseListener only handles low-level mouse events.
I'm trying to implement a simple window that contain two buttons Yes and No.
When clicking on Yes I want to disable the No button and when pressing on No I want to disable the Yes button.
I've implemented:
JButton btnYes = new JButton("Yes");
contentPane.add(btnYes);
btnYes.setActionCommand("Yes");
btnYes.addActionListener(this);
...the same for the No button...
Now I'm catching the event in this method:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Yes"))
{
//I know how to get the button that caused the event
//but I don't know how to disable the OTHER button.
JButton source = (JButton)e.getSource();
//Handle the source button...
}
}
In the above method I have an access to the button that caused the event, but not to the other button.
What is the best way of getting the buttons?
You should just implement ActionListener as a nested class of your Dialog's class, in this case you will have full access to all fields of outer class (in which you should store reference to buttons when your create them).
The bad dirty solution (that should NOT be used) still exists: to navigate to battens through getParent() of JButton and then through getChildren() of parents childrens. Just to show that it is possible anyway.
You could use a JButton array as class member variable and to check which instance didnt cause the event:
for (JButton button: buttonArray) {
if (button != source) {
button.setEnabled(false); // disable the other button
}
}