I need to display a dialog box which contains radio buttons; and when I select the appropriate radio button, the dialog box should disappear?
Try to use TaskDialog framework. It helps to accomplish just the things you ask for in few lines of code. For your case using Command Links is the best solution.
Radio buttons are possible but not a better solution from usability point of view.
In your radio button listener, use setVisible(false), as discussed in the articles How to Use Radio Buttons and Creating and Showing Simple Dialogs.
I agree with the others that having a "dialog disappear" on radio button click is not a very good UI design. Users generally expect that a "dialog disappears" when they select a button at the bottom (e.g. OK, Cancel, Yes, No, etc.).
In any event, if I am to assume that by "dialog disappears" you mean the window closes, then the way to do that is to call dispose on the dialog.
Also, you may want to consider using JOptionPane.
Related
I have a JList with a setSelectedIndex(-1). I have a JButton , "More Details" which only works if an option is selected from the JList. If no option from the JList is selected and "More Details" is pressed, nothing happens, which is expected. But as a user you want to know when that button should be pressed.
A simple JLabel with the instruction saying "Select an option to obtain more details" would suffice. However, is there a way to get the JLabel to appear on the gui with the instruction after "More Details" is clicked? I do not want the instruction to be on the GUI all the time, as the "More Details" JButton will be rarely used. As a result of this I was wondering if it is possible to place text on the same GUI the button is located on with the instructions?
It might depend a little bit on the LayoutManager you use, but all JComponents have a method setVisible(boolean). With this, you could in your ActionListeners for the JButton just call myLabel.setVisible(true).
This is quite possible. You could actually add your JLabel directly in your GUI and call setVisible(false) before showing it. When you hit the button and you want to show it, call setVisible(true)
Also, another way you can do this... Easier in many ways - is to use JOptionPane to pop up a warning / error message
nothing happens, which is expected. But as a user you want to know when that button should be pressed.
Use ListSelectionListener to monitor when the selection changes in the JList and enable/disable the button as required.
I Have a java program that does sort of this:
It starts off with dialog boxes, then after user clicks OK/Cancel or X or whatever, it goes to JFrames or dialog boxes. The JFrames also have buttons like Next/Ok, etc. As the program goes on, one JFrame (lets call it "Status Bar") is always visible on the screen and never goes away (that's what I want). (I don't want to dispose it because they hold important information that the user needs to see while making choices on future dialog boxes and other JFrames).
Now my problem occurs..when the future dialog box appears, I can't click on that JFrame "Status Bar". For some reason, I have to do something on the dialog box first. Like I have to click Ok/Cancel on the dialog box, if I get another dialog box (depends where on the program), I have same issue. Until I am blessed with another JFrame, then I can click on the "Status bar" JFrame, click buttons on it and all the good things presented on that JFrame.
One solution is to convert all my remaining dialog boxes to JFrames, but that would take a lot of time since I have all sort of dialog boxes. And then linking everything together will be time consuming.
So is there a function or code that I can tell Java...to give the user the power to interact with JFrame "Status Bar" while a dialog box is presented on the screen.
I Hope I am making sense. Please ask if something is not clear. I appreciate the help.
If you're using Java 6, you can use the new modality settings.
Modality in dialogs
Depending on your GUI design, you may wish to make the dialogs Document modal as opposed to modeless(equivalent to setModal(false)). Note that this will only work if the dialogs are shown in a different root container than the Progress JFrame.
Another option is to set a modal exclusion on the JFrame you want to be always visible. This way your dialogs can still block other frames and you don't need to remember to setModal(false) everytime you add a new dialog:
progressFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
Call setModal(false) method on all dialogues so that you will be able to go to JFrame while JDialog is open.
I would like to create a modal input dialog with some textboxex/labels. It's supposed to something like "Add client dialog" with name, surname, etc..
I've been searching the net for almost an hour and nothing.. JOptionPane can't handle the task, JFrame has no ShowDialog method etc. How can I accomplish the task in Swing?
JOptionPane can't handle the task,
As you've realized you can add any component to a JOptionPane, including a panel.
One problem is that focus will be placed on the buttons, not the panel.
You can check out Dialog Focus for a simple solution to this problem.
You just need to add a plain old JDialog. Make it modal and use its content pane for your widgets.
I have a JCheckBox in a program (labeled "Use MiniTimer") that, when right-clicked, shows a JPopupMenu with options "Show on Close", "Show on Minimize", "Show on Close or Minimize", and "Do not use MiniTimer". How can I make this JPopupMnu appear below the JCheckBox when it is left-clicked, too?
Note that I tried setting the actionPerformed method of the JCheckBox to miniTimerPopupMenu.setVisible(true);, but that merel makes the JPopupMenu appear in the top-left corner of the screen, and even then, it will not register any interactions with it. Does anyone have any experience or suggestions they would like to share?
Read the section from the Swing tutorial on Bringing Up a Popup Menu for a working example. The tutorial uses popup.show(...). Don't know if that is the difference.
If you need more help post your SSCCE (http://sscce.org) that demonstrates the problem.
I think you should set the location of the miniTimerPopupMenu using the setLocation() method, I think the following code does the trick
miniTimerPopupMenu.setLocation((int)jCheckBox.getLocation().getX(),(int)jCheckBox.getLocation().getY()-10);
miniTimerPopupMenu.setVisible(true);
Then you can play with y and x location of the top popupmenu.
Hope this works
I am creating an options dialog using JOptionPane.showOptionDialog(...)
When I click one of the buttons added to this dialog, I need a label to apear underneath on the dialog (and this label should be scrollable if necessary). I have written event handlers for the buttons, but I am not sure how to get this label to appear on the dialog.
Any help would be great.
Update: I realized that it would be ok if I somehow called JOptionPane.showOptionDialog(...) with an initial message, and then when one of the buttons was clicked I would change the message. Is this possible?
JOptionPane static methods are only shortcuts to easily create a dialog with option buttons and a fixed message. If you check the source from it, you will see that all is wrapped in this purpose. It's only a convenience class over a frequent use case of dialogs.
The suggestion from comment is correct, if you want more than this, you will have to create your own JDialog, as it will be easier than trying to change something from this generated dialog.
Edit: You can create your own JDialog yourself, using layout managers. A more simple way, suggested as well in the previous link, is to use a GUI builder, like the one included in Netbeans.