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.
Related
I am a beginner and I am trying to make a text editor and I want to create a pop up window for text format when I press a menu button where I can put all things like font face, font size , font style etc. Can you tell me how I can make this new window? Thanks for your patience!
For example Notepad:
I think what you're after is a dialog of some kind.
Take a look at How to Make Dialogs for more details.
What I would do is design the basic UI onto a JPanel. I would then add this JPanel to an instance of a JDialog (possibly even using a JOptionPane) and show this dialog, making sure to make it modal, so you can easily retrieve the values set by the user.
This means that you can decide how best to show the user interface or even show it in a number of different ways as it's not constrained to a single top level container
You can simply create a brand spanking new JFrame and it will still be counted as the same application.
Tip: Use Eclipse Window Builder
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've been struggling with some problem while creating my app based on Swing. I have a main JFrame which consists of:
JMenu
JPanel containing a JButton and a JLabel
JButton is linked with ActionListener. Clicking JMenu (MenuListener) brings up a JDialog with some form. The problem is, when the JDialog is closed (it doesn't make difference whether I do it with dispose() or rather showVisible(false)) I need to click the JButton two times before it triggers for the first time. From now it normally works with one click.
Every time the JDialog is in front, the problem appears.
PS. The JDialog is set to be modal, with JFrame as parent.
It sounds like a focus issue.
The first click restores focus to the app and the second clicks the button. Typically, I have seen this when the JDialog has the wrong parent and focus can not be returned.
Thank you for your answers.
I have considered posting some code, but it involves 4 classes so will be quite long.
I have also tried things with focus before, but nothing helped. What is interesting: if I display the JDialog by new myDialog.showVisible(true) it behaves like I've described. But if I use construction like this:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDialog.setVisible(true);
}
});
it closes normally and parent frame doesn't need to be clicked before responding, but on the other hand the displayed Dialog needs so. Additonally, what I do not understand, after opening the Dialog cursor is placed in the text field and I can write normally, but to click some button on it I must click once on the Dialog, only second and next clicks behave like I want to.
PS. Closing the dialog like in the second included example changes nothing.
I have a modeless dialog box being generated which prompts users to open a new window. The box can be opened in two ways, either directly from the file menu for the frame I'm writing or indirectly via the framework my panel is plugging into.
When I make the call directly via the file menu the dialog box comes up with focus exactly as I want. But when I have the framework indirectly open the dialog box it does not have focus as it should.
There doesn't seem to be a difference between the two methods of opening the dialog, in both cases a load function is called and it's not until 5 method calls later the dialog box is opened. In both cases the frame which generates the dialog box is realized at the time the box is generated. I've tried calling requestFocus after making the dialog box visible but it doesn't seem to do anything.
Any suggestion why the dialog box wouldn't have focus, or how I can give it focus as a separate window from the window that usually has focus?
in some cases is hard to set Focus to the expected top-level container as are demonstrated here , but for excelent workaround would be better look at camickr's Dialog Focus
When you create the dialog, try setting the main GUI as parent of the dialog.
In the first case, when you click from menu, it automatically sets the main GUI as the parent of the dialog, but it doesnt in the second case.
So make sure when you create the dialog, you are setting the main GUI/ window as parent always.
It should help most times.
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.