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.
Related
I want to create this element in swing:
As you can see the element is a small grid of buttons which appears when i click on another button. I've tried to use JComboBox to create this element. But as far as i know, JComboBox can just render an image of the some button, but it will not behave itself as a button. Also I couldn't set a GridLayout to JComboBox.
I also tried to create some JDialog, but I suppose that's bad idea.
So the question is: Which swing's component should I use to create mentioned element?
You could use dialog in the best way to achieve this.
JDialog dialog = new JDialog(owner);
dialog.setModalityType(Dialog.ModalityType.MODELESS);
dialog.setUndecorated(true);
You could set Modality type to Modeless to avoid parent frame lock and set undecorated true to make jdialog without close option. But note that you need to close the dialog from program.
I have two seperate JFrames but when i click the X in the topright of one, it will exit out of the other also. I have an "exit" button near the bottom to do setVisible(false), but i still have the tendency to use the x button. How would i make it so that it doesnt cancel out of the entire project?
Also, how would i make it so that the second JFrame locks out of the other JFrame untill the second JFrame is closed, like how a popup message works
Don't give your GUI two JFrames. The GUI ideally should have only one GUI. If a separate window is required, then make it a dialog such as a JDialog, and this won't happen.
Also, how would i make it so that the second JFrame locks out of the other JFrame untill the second JFrame is closed, like how a popup message works
You are perfectly describing the behavior of a modal JDialog or JOptionPane. Just use 'em.
Later we'll chat about using CardLayouts to swap views in a single GUI.
Edit, you state:
Im using Netbeans form editor to create them faster but I only see JFrame and JPanel. Can I edit them in Netbeans? I'd rather not do them through scratch Java
You've touched on another zealous belief of mine, that this is yet another reason not to use a code generator when learning a library as one can get too tied into the code generator, that it prevents one from learning the library. I strongly advise you to put aside your code-generation tool and create by hand, referring to the tutorials and API. Then later when you get more familiar with the library, sure use the tool. By the way, an answer to your direct question here is to gear your GUI's to create JPanels, and then use these JPanels where and how you want them -- in JFrames, or JDialogs, or JOptionPanes, or swapped in CardLayouts, or JTabbedPanes or nested in other JPanels,... etc...
You should be using a modal JDialog, not a second JFrame, because JDialogs provide certain functionality such as not adding another window bar to the taskbar, and automatically setting focus when the parent JFrame receives focus. Modal JDialogs prevent user input to the JFrame while it's open, useful for an "Are you sure you want to exit?" dialog, for example.
As for one JFrame exiting the other, you probably have their default close operation set to EXIT_ON_CLOSE. If you do this:
jframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jframe.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
handleUserWantsToCloseWindow();
}
});
Then you can control what happens when the user wants to close, such as popping up a "Save Changes?" modal JDialog or "Are you sure you want to quit?" modal JDialog. Note that you have to manually dispose of the JFrame if you use this method.
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 am putting a combobox component on the glasspane for users to select from a list of items. When the drop down list is clicked though the JPopupMenu is hidden behind other parts of the component on the glasspane since the popups are displayed on the LayeredPane.
I would like to find out how to make the popup display on the glasspane with the component. I have tried JPopupMenu.setDefaultLightWeightPopupEnabled(false) before the frame was initialized but it seems that makes the popup not display at all anywhere and I am not sure why.
Any advice on how to get the popup to display on the glasspane instead of the jlayeredpane would be helpful. I searched but most responses seem to related to pushing events down that are captured on the glasspane.
I am actually using a JideAutoCompletionComboBox which extends JComboBox.
Edit for question: I have a system wide (my app has a bunch of workspaces on tabs) popup type system. I would like to not use a Modal dialog for this and just use the glasspane. The component is basically for creating a message but one of the subcomponents is a combobox. Effectively you can think of the whole component like a popup though, but using the glasspane.
I don't like little floating windows that users can screw up by pushing around.
JDialog dialog = new JDialog(...);
dialog.setUndecorated(true);
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.