calling actionPerformed() of a button explicitly - java

I have one class where there are certain swing components.
In second class (as per requirement), I need to simulate the button click event ultimately calling the actionPerformed(ActionEvent ae). This button is in the first class(described in the first line).
How do I do so?
I have tried to go through the fireActionPerformed(ActionEvent ae) . However, I am unable to come to a solution.
A small snippet (as an example) would be very much helpful.
EDIT
Note: I also need to disable the button, when I click it or simulate its click.
Pardon me if this has already been asked before. Though I have searched for similar question before.
Thanks and Regards.

JButton has doClick() method but it's better to define one doMyAction() method and call it from both places.

Related

Is there a possibility to customize the behavior of the buttons for ControlsFX wizard

Current GUI with the buttons in question
My problem is that I need to prevent the user from clicking the Next Button when specific conditions are not met, I thought the best way for this would be to overwrite the onClickEvent for this Button but it seems impossible to the required field access to these buttons.
I looked through the available methods in the documentation as well as the source code but I was not able to find anything which would help to achieve my goal.

How to make a button stay depressed when clicked in a Swing Java GUI app

This seems like something really simple and yet I can't find any good solutions online. I am making a simple game and in the main frame I have JButtons. What I would like is for when I click a button it stays looking visually depressed and then when I click it again it "pops" back up. I know I need an ActionListener on the button but I can't figure out the code inside of it. I've seen some discussions referencing "setPressed(true);" but as far as I can tell that is not a method on a JButton object, so I don't know how it's useful. Thanks.
You can use a JToggleButton.
Read the section from the Swing tutorial on How to Use Buttons for more information.

how to reuse a jframe without making it new?

I was wondering if there was a way to reuse a jframe without making it new. You might be wondering why. I have two JFrames (actually more, but for this question's purpose, two). One contains a radio button(agree) with the terms and conditions written in a jtextarea. THe other JFrame contains a passwordtxtarea(password), jtextarea(username) and a radio button(read terms and conditions), as well as a "TermsAnd Conditions" button.I forgot to mention that the first jtextarea contains a "back" jbutton, that if i press on, I get JFrame2, when I press "Terms And Conditions", I get JFrame1. The problem is, that my code requires both "Agree" and "read the Terms" radio buttons to be clicked on, but whenever I press "back" or "Terms And Conditions", any input I had put in (username, password, clicks on radio button other than default) is lost. Therefore I cannot proceed in my program.
I think it has to do with the fact that I have to make a NEW JFrame Form. Maybe it sets it back to default? Anyway, how do I fix this problem? I haven't seen a question like this, so is there a blatantly obvious answer I'm unable to see, except for "it's impossible"?
You state/and I reply:
I was wondering if there was a way to reuse a jframe without making it new.
Yes, it is quite possible to re-use components (the generalization of your question).
You might be wondering why. I have two JFrames (actually more, but for this question's purpose, two).
As has been stated, this is generally not a goood idea. Most Swing GUI applications use only one main window, the JFrame, and then either swap views such as with CardLayout or JTabbedPane, or show modal or non-modal dialog windows.
One contains a radio button(agree) with the terms and conditions written in a jtextarea. THe other JFrame contains a passwordtxtarea(password), jtextarea(username) and a radio button(read terms and conditions), as well as a "TermsAnd Conditions" button. I forgot to mention that the first jtextarea contains a "back" jbutton,
It's most unusual for a JTextArea to have a button of any kind. Also, there is no such thing as a "passwordtxtarea", perhaps you mean JPasswordField? If so, please be precise with your terms when asking questions here. It's hard enough to guess what someone's program is like based on a description, that you don't want to make it harder on us. Also, it's very unusual to use a JTextArea for a user name field, since usually you'd use a JTextField. Again, precision really matters. Else we'll likely give you the wrong advice.
that if i press on, I get JFrame2, when I press "Terms And Conditions", I get JFrame1. The problem is, that my code requires both "Agree" and "read the Terms" radio buttons to be clicked on, but whenever I press "back" or "Terms And Conditions", any input I had put in (username, password, clicks on radio button other than default) is lost. Therefore I cannot proceed in my program.
Yes, you should not be creating new components here but rather re-using previously created components. It's all do-able if you make your component a class field and if you make sure to create it only once. It's all how you code it.
I think it has to do with the fact that I have to make a NEW JFrame Form. Maybe it sets it back to default? Anyway, how do I fix this problem? I haven't seen a question like this, so is there a blatantly obvious answer I'm unable to see, except for "it's impossible"?
Again it's possible. The solution will all depend on the structure of your program.
A word of advice: gear your GUI code toward making JPanels, not JFrames. This way you can place them anywhere they are needed -- in a JFrame, a JDialog, another JPanel, or swapped with a CardLayout,... anywhere. It greatly increases the flexibility of your program.

How to stop program until a button is pressed in JAVA?

How do I make it so that the program doesn't keep reading in code until the button is clicked?
Why?: I have a 10x10 grid with buttons in each part and then code running depending on what is clicked. However, my program keeps reading in code so there is never a choice being made and it gives me error. I tried giving it a infinite loop until a button is pressed, but that doesn't work out so well
-edit
I'm a complete beginner with Java.
This is a picture of the GUI
http://imageshack.us/content_round.php?page=done&l=img843/5351/sascp.png
What I want is for the code to not keep running step by step until I click a button.
E.G.:
create gameGUI
wait until and check which button is pressed
if(buttonClicked[i][k] == something){
System.out.println("lool");
}
But what's happening in my code is that it creates the gameGUI and then because the user isn't fast enough to click it just skips over the if statement or gets a run-time error because nothing was pressed.
In both Android & Swing (& I'd expect J2ME), buttons fire events when told to do so (by activating them). You would generally just wait for that to happen before doing anything, and not bother with what the rest of the GUI is doing (or not doing) at the time.
Or in other words:
Add an ActionListener to the buttons.
In the actionPerformed() method, insert the code that you have above.
Also
The code snippet provides almost no useful information. For better help sooner, post an SSCCE.
That GUI looks like Swing to me. If it is not, then what is it?
Please always copy/paste run-time errors.

JButton inside JMenuItem

I'm trying to build a particular JMenu.
I want a JMenuItem with JMenu functionality, I.E. when we click it the item should do something (like opening a dialog). But the JMenuItem should also contain a button (or other component) that when we click it, should open a popup with a couple of options.
So, till now I have something like this:
That is what I have before click the arrow.
My problem is that, when I press the button (arrow), the sub-menu is actually opened, but the menu item that contains that button closes because loses focus.
That is the result after clicking in the arrow button.
Is there any way to manage this? Or a better way to have this behavior?
We can guide you if we know exactly what you are trying to implement. If you just want to select an option, you can implement that in better way with the JRadioButtonMenuItem,so you dont really need to implement a button and then select an option.But it depends on what you really want.
That's not what a menu is intended for. Use a ribbon instead, and these things will be easy and natural.
Why not just use simple nested JMenuItem instead?
Something like this (First screen from the top).
On the other hand you can benefit from a similar solution described here.
Couldn't actually find a solution for this particular problem.
As a workaround, I used just a simple button that toggle between the option 1 and option 2, instead of having the button (arrow) that open a new popup.
Thanks a lot for your help.

Categories

Resources