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.
Related
I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.
I want to fill values of multiple jTextBox from a jFrame into another, using accessor methods like
String getNameVal()
{
return jTextBox1.getText();
}
How to call these methods from another jFrame?
Suggestions:
It sounds like your GUI code is geared towards making JFrames, and if so, you will want to avoid this. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this.
More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
This question has direct bearing on your problem. I will guess that your main problem isn't how to give classes getter methods, and how to have other classes call the getter methods. More often then not, when faced with the issue of extracting information from one GUI view to another, the issue is one of when to extract the information. If you displayed your second window as a non-modal JFrame, and then had the calling class immediately extract the data from that second JFrame, you'd get nonsense data, because you'd be extracting data before the user would have time to interact with the 2nd window and enter data.
One possible solution to this when using non-modal windows to get information from the user is to use a WindowListener so you can be notified when the user has completed his dealing with the second window, and so now data can be safely extracted.
Often better is for the 2nd window not be non-modal, as JFrames are, but instead to be a modal window such as a modal JDialog. When the calling code displays a modal dialog, all code flow in the calling code stops until the dialog is no longer visible. In this situation, no WindowListener is needed since you will know exactly when the dialog has been dealt with -- on the code line immediately after you set it visible -- and so can extract your data from it with ease.
A nice variant on this has already been mentioned in by Andrew Thompson in comments -- use a JOptionPane. Don't poo-poo this option since JOptionPanes are powerful tools, likely much more powerful than you realize as they can hold fully formed complex JPanel views, and behave just as described above, as modal dialogs.
If you need more specific help, then please don't hesitate to comment to this answer. Also if so, then consider creating and posting a Minimal, Complete, and Verifiable Example Program where you condense your code into the smallest bit that still compiles and runs, has no outside dependencies (such as need to link to a database or images), has no extra code that's not relevant to your problem, but still demonstrates your problem.
Edit
For my mcve code examples of the above suggestions, please my answers to the following StackOverflow Questions:
Using a modal JDialog to extract information
Using a JOptonPane to extract information
I assume the textfields are present in frame1 and you want to access them in frame2. The following can be a way to achieve this:
First create getters for all JTextFields that you have in your frame1. Alternatively you can have them in a panel and call getComponents() method.
Create a private variable of JFrame type in your frame2.
Modify the constructor of frame2 to receive the frame1 object and assign it to the private variable of JFrame type.
Now you can create a close() method in frame2 which disposes the frame2 and sets frame1 to visible.
But in my opinion you should create a class which handles the data in these textfields. Initialize the class object in any button click of frame1 and check for any inconsistency in the input. I can guess there is something wrong with your design.
I am somewhat new to coding and I need a little help. I am making a program where you start in the main menu. In the main menu there is an options button you can click on that will open a new jframe with the options. I was wondering if there was a way so that you would have to use the new JFrame before using the older (or main) one.
Take a look at How to Make Dialogs.
A modal dialog is one that must be dismissed (closed) before you can interact with program again (an example might be the open/save dialog of Word or your web browser)
But you should also have a read through The Use of Multiple JFrames: Good or Bad Practice? first...
Alternatively, you could use a CardLayout, which would allow you to switch between views from within the same window
You could also use a JTabbedPane which will allow you to have your views presented on the screen and accessible via a "tab", meaning the user can only interact with a single view, but they can navigate to a different view by clicking the tab they want.
I don't know what should I say, There is a JFrameOne has show button. When I click show Button, The New JFrame (JFrameTwo) will show up. It works well. But the problem is, in the task bar ( the bottom of the your desktop ) , new task added. JFrameTwo . Finally, I can see 2 tasks (JFrameOne, JFrameTwo) in the task bar. This is not what I'm going to do. This is abnormal I think, You know, When I click the button in Ecilpse or anything usual program, they nothing create new task in task bar. They just show frame without create any task.
So, I read some tutorials about Swing, related creating new window, People say I should use JDesktopPane+JInternalFrame or Card Layout. But, the JFrameTwo is bigger than JFrameOne. In this situation, what should I do?
Use JDialog instead of a JFrame. A JDialog will not create new items in the task bar. Possibly even make the dialog modal to make it easier on the user.
If your first Frame is much smaller than the second frame you want to open, you might want to rethink your GUI and possibly make the first frame larger and use a JDesktop like you mention or just update the content of a single frame.
The recommended approach is to use a JDialog. This will not create a new task in your taskbar.
Use CardLayout for switching between components, or use JDialog instead of new JFrame's.
Also read about using of multiple frames.
Try This example.
Also see JInternalFrames
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.