I'm trying to switch between two frames in my main program.
The trigger to switching to the second frame from the first one, is one condition to be met and one button to be pressed in the first frame.
This is one example that I've tried so far, but the actionPerformed method only accepts a parameterized local variable e for ActionEvent so I'm unable to use this .getEvent() method in my main which access an empty ActionEvent field.
Do you have any idea on how i could proceed to achieve this?
pb = progress bar
next = button
if(gui.getEvent().getSource()==gui.getNext()&&gui.getpb().getValue()==100){
gui.dispose();
TrainingGui tgui = new TrainingGui();
}
Related
So, this is really annoying, and an incredible magic show at the same time. So I have a global string called setAnagram. I'm making a GUI with Eclipse, and so what I'm trying to do, is have the user click a button, it start another thread, and on that thread it gets the value of a text field. The way I did this was to set setAnagram to the value of the text field.
However, that didn't actually work, and setAnagram had a blank value (""). So then I thought okay well what if I set the variable to null. Well then I get a NullPointerException.
I thought well, what if the variable actually doesn't change, and it starts the thread before it can change the variable? Well I made it so you press the button once and it changes it, then you press it again, and then it starts the thread. Here's where the weird stuff comes in. I print out the value of setAnagram on the first button click after it has been changed. However, when I start the thread on the next button click, it's back to the what it was initialized as ("test"). Here's the code for the button click:
decodeAnagramButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (buttonPresses == 0) {
setAnagram = chosenAnagram.getText(); // chosenAnagram is the text field
buttonPresses++;
decodeAnagramButton.setText("Click again");
System.out.println(setAnagram); // These two lines output the same thing
System.out.println(chosenAnagram.getText());
}
else {
titleText.setText("Decoding anagram...");
decodeAnagramButton.setVisible(false);
MainWindow thread = new MainWindow();
thread.start();
}
}
});
And then to minimize, I won't add the thread code because it's kinda long, just know that all it does with setAnagram is get the value. Any ideas on why this is changing unexpectedly?
MainWindow thread = new MainWindow();
thread.start();
So... you create a new instance of MainWindow (which is apparently a thread?!?). That means that new instance of MainWindow also has its own setAnagram member variable, right?
Try adding thread.setAnagram = this.setAnagram; between these two lines of code.
(Also, something about your design smells fishy, why is your main window a thread?)
In my application, i have a radio button field selected. When i am opening the page, the selected field has some delay. Example: I am opening the page, that radio button is in unselected state, after some time (say like hovering on the screen/scrolling) the radio button turns to selected state. Is there is any other way to solve this delay?
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
statementGroupSelectionModel.setSelected(item, profile.getSelected());
}
});
Here in the above code, "item" is widget field name and depending upon my "getselected" value, the radio icon will get selected. Here my "getSelected" value is true.
Any code inside scheduleDeferred will have its execution postponed until the browser finishes rendering the page and the flow control returns to the Javascript event loop, as explained in GWT's documentation:
Deferring some logic into the immediate future: the Scheduler class
Sometimes you want to break up your logic loop so that the JavaScript event loop gets a chance to run between two pieces of code. The Scheduler class will allow you to do that. The logic that you pass to Scheduler will run at some point in the future, after control has been returned to the JavaScript event loop. This little delay may give the interface a chance to process some user events or initialize other code. To use the Scheduler class in its simplest form, you create a subclass of the Command class, overriding the execute() method and pass it to Scheduler.scheduleDeferred
If what you want is for the radio button to be selected immediately, just remove the Scheduler wrapping, i.e. move statementGroupSelectionModel.setSelected(item, profile.getSelected()); out of scheduleDeferred.
// Put this here
statementGroupSelectionModel.setSelected(item, profile.getSelected());
// Remove the following code
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
...
});
If what you want is not that (sorry, your explanation is rather unclear), but just to be able to set the delay time yourself instead of having to wait for the browser, you can use the Timer class which is explained in the documentation linked above.
I am from system programming background in C and Java programming is Greek and Latin for me.
So my problem is:- I have 2 JFrame Objects
I have a parent Object A
which has child Objects B.
Object B has buttons on it.
If a button is clicked in B I want to update the UI of parent Object.
How can I can communicate to the parent - to update itself?
Do the updates need to be made 'live' while the values are adjusting and the 2nd GUI is open, or can they be delayed until it is closed?
They can be delayed until it is closed.
Use a modal dialog or JOptionPane instead.
How to Make Dialogs
How to Use Modality in Dialogs
Using a modal dialog, whatever code line comes immediately after setting it visible, is blocked from being processed until it is closed. That is where you examine (the return value of the option pane &) the value of the controls you put in the dialog. Note that the idea here is do not extend dialog. Instead just create an instance of one in the main code, create the controls, add them to it, and show it using the main frame as the parent.
Putting 'everything in one class' is not a good design, nor what I am suggesting in general. It is just that there is little cause here for the main GUI not to have references to controls that affect/update it.
Well normally you have a parent JFrame object into which you put your widgets like buttons, panels etc.
You can simply add an anonymous action listener with the button and then call any function of the parent class from inside it.
public class MyFrame extends JFrame{
private Button button 2 = new Button();
public void init(){
b = new Button("Click me");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//CALL ANY FUNCTION OF PARENT CLASS FROM HERE.
updateText();
}
});
}
updateText(){
this.button2.setText("new text");
}
}
You can find more info about it on this page http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
I have found one more way to solve this problem.
I guess it can solve a general delegation problem.
Parent Object composes of Child Object.
Parent Object passes this value to Child Object in constructor.
This way the Child can call a method on the Parent Object and notify any change.
In this case Child Frame calls Parent frame and asks the parent to refresh itself.
I am writing a GUI, and I have made custom buttons using null layouts, however whenever I am adding a action listener each time the button is pressed it is reloading the entire image, making it look different, is it possible to use an action listener several times without necessarily doing a new one?
You don't need to add a new ActionListener every time you want to handle an action. Just add one and it will keep working, everytime the button is pressed the actionPerformed method is called.. and I actually don't get why you add another one everytime (they will coexist so the actionPerformed is invoked many times)
In my code, two comboboxes are added to actionListener( this );
In another part of my code, I call a combobox function that sets an index to a certain value. This in turn calls actionPerfoemed again and so getSource == comboBox is true. Every time I call a set function it calls actionPerformed again, creating a stack of function calls that then unwinds down to the first.
Is there a way to prevent this?
If the problem is just the initial setting, you can defer adding the listener until after both have been initialized. There's more discussion here.
From the Swing tutorial,
Combo boxes also generate item events, which are fired when any of the items' selection state changes.
These events will be generated either when a user clicks on the items with the mouse, or when your software calls setSelectedIndex().
Perhaps you don't want your actionPerformed() method in this to be called when your software calls setSelectedIndex(). You may need a Boolean eventInitiatedBySoftware. In your main (this) class, you could say
synchronized(eventInitiatedBySoftware) {
eventInitiatedBySoftware=true;
comboboxeditor.setSelectedIndex(n);
}
and then in your listener:
public void actionPerformed(ActionEvent ae) {
synchronized(eventInitiatedBySoftware) {
if (eventInitiatedBySoftware) {
eventInitiatedBySoftware=false; // clear your flag.
return; // don't want to process this event.
}
// the rest of your method goes here
}
When your software wants to adjust the value, it will set the Boolean to true. The actionPerformed method will be called, but your test will realise that this event was initiated by the software, and return before doing any of your existing code. It will clear the Boolean, so that if a user now uses the mouse to perform a selection action, your code will realise that it wasn't softwareInitiated.
BTW, It's possible that you misunderstand the event concept. For example, I suspect you are actually adding "this" as an event listener for each combobox, rather than adding comboboxes as listeners to "this". You might like to look at the Writing Event Listeners trail.