i've made a menu bar with netbeans.
in the menubar i've got
file >exit
Help >Help F1
>about
the problem is i don't know how to link up either help or about to another frame that has everything i want the user to see.
can someone please tell me how to go to a new frame once eiher help or about is clicked?
thanks
For About, you would typically use a modal dialog, i.e. a JOptionPane - using those is pretty straightforward. For Help, you don't want a modal dialog, but a new separate JFrame. But you don't have to "go to" it - just create it and call show() - that's all you need to do. Like modern GUIs in general, Swing does not have an explicit control flow through or between masks. The GUI is shown, and only when the user interacts with it is your code in the various event handlers called.
Related
I am new to Java GUI I deigned and window and menu item using java design tool.But when I want to create window for menu item say New contact I did not find an option to do that in event handler so I did it manually by coding it.But when I go to design part and click on New Contact it does not show the window I created via code.
Here is the screen shot of deign view -when I click on New Contact nothing happens.
Now in the source code when I run it I get the window I coded
Is there any possible way I can make it work in design part? I did not find any option to do it in Add Event Handler
You do not want a JFrame to show another JFrame -- that's a bad GUI design since it means that your application is actually two applications. Better to show a dialog window such as a JDialog. Please see The Use of Multiple JFrames, Good/Bad Practice?
If you want to design a 2nd window, create a new Java program in NetBeans, one that creates this second window (again, better for it to be a JDialog, not a JPanel)
Give it a constructor that allows passing in the parent window, and then pass that to the JDialog super constructor
And then in your ActionListener code above, create a new object of this new program, passing in the current JFrame.
In the future, please post code as code-formatted text, not as an image, since this way we can copy, paste, compile and run it if we want, allowing us to better understand your code and your problem.
I am attempting to learn Java (at this point GUI programming in Swing and as a concept in general) and I have managed to create a basic login page. What I want to do however is to have it change what is displayed somehow to a 'home' page, as it were. I can think of only two ways of doing this, the first being opening a new window and closing the old one and the second being somehow changing the frame the login page is in to whatever I want to display. How is this usually done in real-world applications?
There are a number of ways that it can be done including the two that you mentioned. It really depends on what you are trying to achieve. Something like a login form could be done with a JDialog that pops up over the main window such that you start the main window and the main window simply pops open a JDialog for credentials etc.
Sometimes you create multi document interfaces using JInternalFrame. Where the various windows that you need all appear to be within a bigger main window.
So if you were creating a simple UI then simply switching out the content of the JFrame as you suggested would suffice. You can use an apprpriate LayoutManager to assist such as a CardLayout.
If you have a login frame, a better way is to make it a modal dialog, which is:
1) more user-friendly.
2) making your coding job easier.
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
I need to make a popup box with with a combo box and a couple of buttons. Please could someone advice on the best way to achieve this? I've had a look around and all I can find is alert boxes. Is this possible or will I need to create a whole new frame?
You can use the JOptionPane to achieve this. Please refer to the link below which explains this with sample code:
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
JOptionPane.showInputDialog may be good enough if you are willing to leave how exactly the options are presented up to the UI.
I need to make a popup box with with a combo box and a couple of buttons
1) don't use another JFrame as popup window, use JFrame with JOptionPane/JDialog/JWindow these container are same as JFrame, but can take parent and owner
2) don't forget to setParent
3) depends if you needed decorated window then use JDialog, don't forget look for setModal() or ModalityTypes, if undecorated then use JWindow
4) don't create lots of JOptionPane/JDialog/JWindow on fly, becasue there Object are still in JVM memory, create this Container once and re-use that (by removing child) for another Action
I have a Java network application and this is what I want to do:
After user logs out, his interface window closes.
Now, I want for another window to show up that will say something like: "Thank you for using our application".
This final window should be borderless and without any available option, more like a plain picture (jpeg? why not?). By clicking on it, user will be sure to close this final window.
I googled and couldn't fin anything on this matter, it's hard to phrase the question.
I hope someone helps me...
https://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JWindow.html
A JWindow is a borderless, undecorated JFrame (no titlebar or buttons).
This should be what you need.
This should help:
http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html
You're interested in the windowClosing and windowClosed events
You have various possibilities, depending on when you want this dialog to display :
if you want it to display juste before the app closes, use addShutdownHook
if you want it to display when the last window closes, use addWindowListener
You can then use a JWindow with your image inside, and use addMouseListener to wait for the user to click on it.