actionPerformed has empty logic - java

I have a screen which has list. By right click, I can open a small pop up and add new records to the that list by choosing some record and clicking OK button from pop up.
OK button which is on pop up has an action listener like below:
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
And that is all the actionPerformed method does and I do not understand how this method is adding new record to the list on main window. There should be another part of code which is connected to this part but I do not know what is that. Do you have any idea what I do not see on that logic?

The ActionListener is not empty and in fact it is changing the state of the window that holds the JButton, making it no longer visible.
No one can say with 100% confidence what logic is being used here since you've yet to show enough code for that, but our guess is that this button is being held within a modal JDialog -- a window that freezes code flow in the calling code once the dialog window is visible, and (here's the key) that releases the block on code flow once this dialog is no longer visible. So in this situation, making the dialog no longer visible will allow the calling code, the code that initially told the dialog to display itself, to resume flow of its logic. Presumably in the subsequent code, it will query the dialog for data that was entered, and extract it, again the details of which are in code not yet shown to us.

Related

create dialog from listener

Faced the following situation. I have a list of projects in JCombobox. Changing the line in the combo box causes my itemStateChanged method to be called from the ItemListener interface. And it is right. But here I need to ask the user if he/she is sure of this, because he/she changed something in the old project, and it would be nice to save these changes (or not, since this is an accidental error, or not leave the old project at all). I went beyond a simple JOptionPane.showQuestionMessage and built my own complex dialog. With different fields, trees, tables and others, allowing the user to make an informed choice between "Yes", "No" and "Cancel". And here is an ambush!
The dialog after pack() and setVisible(true) opens but doesn't get focus! The focus is still on the combo box, in which the current line was changed. Probably, this is also correct, since after confirming and possibly saving the changes, the user should be given the opportunity to continue clicking projects.
Is it possible to call modal dialog window without affecting the drop-down list?
Even if we accept the loss of focus in the combo box and the closing of the drop-down list in it, then how transfer focus to the dialog after setVisible(true)? Using dialog.requestFocus before dialog.setVisible (true) doesn't help. Now the "Yes" button must be clicked twice. The first click is to transfer focus, the second is to press the button itself.
Or am I missing something simple? I would be grateful for any suggestions!
Following the #camickr 's advice the solution is:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
appData.askAndSaveCurProject(project);
}
});

Java JFrame popup window

So I'm fairly new to Java Swing, and I stumbled upon a certain difficulty. I have a main Frame running (main part of the application that is visible throughout the app's execution) which has a button that once clicked, invokes a popup Frame (window) to collect user's information, and that frame has some Components. The problem being is that I don't really know the right approach to invoking the popup window and the main window freezing the execution and waiting until OK, or cancel button is clicked on the popup. Once this happens the main window code collects the returned values from the popup and resumes. I tried using synchronization to accomplish this, however the popup components don't even load, just the JFrame and JPanel (white background) and the popup freezes up on the wait() condition. I know that there is a way of doing it with JDialog and others, my main concern however, is to discover why the popup frame doesn't load the components and freezes up before the wait() condition. (when I get rid of wait() everything loads properly).
//in Main window Class:
frame.setEnabled(false);
Test test = getNewTest(); //should freeze on wait() in popup window
frame.setEnabled(true);
//in Popup Window Class
public Test getNewTest() {
addPanel.setVisible(true);
addFrame.setVisible(true);
synchronized(flag) {
try {
flag.wait();
} catch (InterruptedException e) {}
}
addPanel.setVisible(false);
addFrame.setVisible(false);
if(success)
return new Test(testName, date);
else return null;
}
//When OK or Cancel button clicked appropriate handler sets
//success value and invokes flag.notify();
Get all that synchronized and wait stuff out of your code. All that will do is freeze the Swing event thread, rendering your application useless.
You don't want to use a second JFrame, since an application typically has only one main window or JFrame, not multiple.
You want instead to use a modal dialog such as a JOptionPane (which can hold complex GUI's), or a modal JDialog (which also can hold a complex GUI). Be sure to associate the modal dialog with the parent JFrame. The Swing GUI library will then freeze the main window until the dialog has been dealt with, and the code from the main GUI will resume from the place the dialog was made visible after the dialog is no longer visible.

How to show the Open File dialog box when a user clicks on a JTextField?

I want to show the Open File dialog box when a user clicks on a JTextField. When I added the following code (which I removed for now)...
this.textField.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent event) {
// Show the Open File dialog box.
// Same as lines 86-93 in the link below.
}
public void focusLost(FocusEvent event) {
// Do nothing.
}
}
(Code here.)
...it seems that after the user selects a file and then clicks on the OK button, the Open File dialog box will appear again, because I assume that the focus is still on the JTextField. The same thing happens when the user clicks on the Cancel button.
How do I fix this problem? Your advice will be greatly appreciated!
The problem is when the file chooser dialog appears, it takes focus. When it closes (I assume) you refocus the textfield (or the focus manager is returning focus to it), which triggers the focus event again.
I can think of two solutions. One, if you only want the file dialog to appear when the user "clicks" the field, use a mouse listener instead.
Two, use a internal flag to monitor that the current operational state. This might be more difficult to implement given the nature of the events processing

Is Dialog.show() a non-blocking method?

I've got a button that kicks off a background thread to do some work and I am trying to use a ProgressDialog to prevent the user from double clicking that button (or any other ui elements) while that work is being done. The first thing I do in my buttons onClick code is to display the progress dialog, which takes over the screen. The problem I am seeing is that if I rapidly tap this button, sometimes two or more presses will register before the ProgressDialog is shown. This leads me to assume that ProgressDialog.show() is returning before the ProgressDialog is actually visible.
Can anybody confirm this? Also, is there a way to change this behavior, or at least get a notification of when the dialog is actually visible? I saw Dialog.onStart() but given the javadoc, this appears to be called before the Dialog is actually visible...
UPDATE:
While it appears that there is no good way of solving this problem in general, the following works for my situation where my work is done by an external thread and the amount of work to do takes longer than the time it takes for all the button clicks to be processed:
void myOnClickHandler() {
if(myButton.isEnabled()) {
myButton.setEnabled(False);
// do work here
// setEnabled(true) is invoked at the end of my spawned thread's run().
}
}
No.
The problem is you clicked many times before the click event is delivered. (i.e. it is queued before you run ProgressDialog.show().)
From what I've noticed in Android you can double click on a button rapidly and have the onClick listener fire twice (or even more) regardless of the code in the listener (even if you disable the button immediately).
I reported a bug a while ago here: http://code.google.com/p/android/issues/detail?id=20073 but of course these things tend to go "unnoticed" by Google. Feel free to star it in hopes of getting Google's attention

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.

Categories

Resources