create dialog from listener - java

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);
}
});

Related

How can I create a popup menu that allows user to choose a certain elements from a list on Java?

I am writing a Java program to record some events and their status, i.e., done or not. I have implemented the recording and calculations, but I also want to have a button that will open another window that will list the events with a checkbox corresponding to each of them, so I can pick which ones' statuses I will change to done. I have only used Swing libraries so far.
I want this window to contain one event per line, with a checkbox next to the event. Each checkbox should determine whether the status of the even should be changed or not. I had trouble even making such a window open and list the items, let alone put the checkbox.
Note: The design is not too important. This won't be a published project, I will mostly use it for personal work. I can do with buttons instead of checkboxes, where any click changes the status of the event next to it.
Thank you in advance for your valuable responses.

actionPerformed has empty logic

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.

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.

modal parentless JDialog does not grab focus

We have an application which, as its first UI action, displays a modal JDialog without a parent frame.
public LoginDialog(Frame owner, Config config, Object... params) {
super((Frame)null, true);
It unfortunately has the annoying characteristic that when it appears, although it comes to the front, it does not grab the focus.
So the user, after launching the application by double-clicking on the start menu, has to use the mouse to select the "login" dialog and type in information.
Is there a way to make this JDialog grab the focus when it appears in the front?
I've tried calls to "requestFocus" before, after and via invokeLater "during" the call to setVisible(true) - but none of these seems to have any effect.
How do we make a modal dialog grab the focus?
UPDATE: The issue was the code used to try to present a background "wait window". This window was displayed "behind" the login dialog as a hack so that when the dialog disappeared the user would see the "Please wait" message. As it was the first window created by the application, it got the focus. I am not sure if there would have been a way to make the dialog gain the focus again inside the event dispatch thread with this hack - but I fixed it by un-hacking it and doing things properly.
First, it a little strange that modal dialog is parent-less. The point in modal dialog is that it is displayed on its parent and does not allow to access parent.
So, the first recommendation is to make it non-modal. I believe it will work.
BTW I have just tried to create such dialog and have not problems with focus. Try probably to simplify your code:
JDialog d = new JDialog();
d.setSize(200, 200);
d.setVisible(true);
This works for me and I believe will work for you. Now start changing this simple code towords your real application code. At some point it will stop working and you will see where the problem is.
If nothing helps try to use the trick I described in this article. Look for title "Portable window activation". I hope it will help.
See Dialog Focus for a potential fix using a RequestFocusListener. I have used it successfully for setting focus in JOptionPane dialogs.
1) you have to create JDialog contents and showing container wrapped inside invokeLater()
or best and safiest way is
2) you have to set for ModalityTypes or Modal for parent
3) only one from containers could be Modal in applications lifecycle

Java Menu issue

I have a menu with a few JCheckBoxMnuItems. How do I ensure that the Menu stays open until I have done all my selections (i.e. checked the menuitems) and does not close on just clicking one of them?
I'd rather not try to change the normal menu behavior for an application or for a part of the menu tree. A User expects that the menu closes automatically after a menu item is clicked. And, if you kept the menu expanded, what kind of action would you invent to close it manually after you've done your last selection?
If there's a requirement to change more then one setting within one use case, then you should consider to provide a small dialog where the use can apply the changes and confirm them at once. I believe, that's more consistent with typical behaviors of UIs.
And it declutters the menu bar, you'll have just one 'setup' menu item instead of a dozen (?) check box actions :)
I guess menu's aren't supposed to allow multi-selection.
But you may offer keyboard shortcuts to set the menuitems without using the menu at all.
If the set-operation of your flags is a central aspect in your application, I would tend to use a dialog here. These are all suggestions which do not require to change the internal implementation of the existing controls, even though I know, that it would be possible in swing.
I agree that it is better to do this with standard UI. However, if do you want to add checkboxes that do not close the menu it is surprisingly easy:
JCheckBox checkBox = new JCheckBox("Text");
checkBox.setOpaque(false);
checkBox.setRequestFocusEnabled(false);
menu.add(checkBox);
This may not work on every look and feel and the check boxes will not line up with menu items in the same manner as JMenuItems but it seems to be a reasonable place to start.

Categories

Resources