This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How can I stack/overlay jPanels in Java?
I want to create a panel over another so that after clicking next button from first panel second panel gets open above it in same frame.
How can I do this using Netbeans GUI builder?
What you really want is a CardLayout , Have a look at this : How to Use CardLayout.
If you are using the Matisse feature of Netbeans, which uses GroupLayout, you may have to write some code to overcome your problem.
While creating your GUI keep both the panels on top of each other, keep the default visibility of your 2nd panel as false. Then when the button is clicked, set the 1st panel's visibility to false and change the 2nd panel's visibility to true.
Related
This question already has an answer here:
Only one component shows up in JFrame
(1 answer)
Closed 4 years ago.
I am creating a tabbed pane with tabs in the WEST or a borderlayout and the content in the middle. This is working great the first time the menu item is click setting the content in the pane I want. But once I have clicked a menu item once, that menu item with no longer repopulate the pane in the middle. Below is my set active function;
public void setActive()
{
panelShowLocation.setAllMenuItemsAsInActive();
active = true;
setBackground(color_panelHover);
menuText.setForeground(color_textHover);
panelShowLocation.add(content, BorderLayout.CENTER);
//content.setVisible(true);
panelShowLocation.revalidate();
}
panelShowLocation.add(content, BorderLayout.CENTER);
panelShowLocation.revalidate();
When you add a component to the panel the existing component is not removed.
Swing painting logic paints the last component added first. So the newly added component gets painted, but then the old components gets painted over top of the newly added component
So you need logic like:
panel.remove( theOldPanel );
panel.add(theNewPanel, BorderLayout.CENTER)
panel.revalidate();
panel.repaint();
The other option is to use a CardLayout. The CardLayout allows you to add multiple components to the same panel. Only one of the components is ever visible at the same time. Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
i have 5 jFrames in my java project. And i want to make like a Main Menu.
I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame.
And if i click other button the main frame will clean and charge other jframe.
It is possible? im programming with java jdk 8 and netbeans.
Thanks
Edit:
I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated
i have 5 jFrames in my java project.
And that's a problem.
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
But this extra gymnastics is bulky, prone to bugs and unnecessary. 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.
For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.
Other useful links:
For rationale on why to avoid manually swapping please see: What's so special about CardLayout vs manual adding/removal of JPanels?
For using a CardLayout to help control a "multi-page" application with multiple classes, please see: How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes
This question already has answers here:
Set Cursor On A JTextField
(2 answers)
Closed 7 years ago.
I am making a application in Java that uses a JTextField. Now, I want, as soon as I run the app, the cursor to be put automatically in that so that the user doesn't have to click on it and then write the text. I have tried requestFocusInWindow() but it doesn't work.The focus goes to the Panel present in the Jframe first but I have to click on the text field to edit it. Can anyone help me solve this,really appreciate it. Thanks a lot
Check what requestFocusInWindow() is returning. It returns true if it is likely to succeed, and false if it definitely fails.
Also, try waiting until JTextField's parents are visible when you call requestFocusInWindow(). In my experience, calling the method before parents are visible usually doesn't work.
Here's a paragraph from the documentation:
If you want to ensure that a particular component gains the focus the
first time a window is activated, you can call the
requestFocusInWindow method on the component after the component has
been realized, but before the frame is displayed.
And then provides this code sample:
frame.pack(); // Realize the components.
button.requestFocusInWindow();
frame.setVisible(true); // Display the window.
I suspect that when the components are "realized" (whatever that means), Java automatically delegates the focus. I also suspect that if you do not call pack(), the components are not "realized" until you call setVisible(true)
This question already has answers here:
setDefaultCloseOperation to show a JFrame instead
(3 answers)
Closed 8 years ago.
I want to write a code to default close button in JFrame. I just want to ask whether "to save the content or not?" using another JDialog or JOptionPane when click on close button.
Where do I have to write the code? As default close button is not in design of JFrame, and no such event found for JFrame window too, how can I add event listener to that button?
Check out Closing an Application for a couple of solutions:
using a WindowListener and overriding the windowClosing(...) event to display your option pane.
using a simple API so you only need to provide a message or write an ActionListener for more complicated processing.
This question already has answers here:
Implementing back/forward buttons in Swing
(3 answers)
Closed 9 years ago.
I am facing a issue with implementing back button functionality.
I have 2 frames (Main Frame and Second Frame), so when i press a button on Main frame it takes me to second frame. On the second frame i have a back button it takes me back to the main frame. This is working as expected. But the problem is that once i am back on the main frame after pressing the back button all the alignment of the main frame goes away.
On Main frame i have below code to goto second frame:
frame.dispose();
frame.setVisible(false);
WebAppTest object = new WebAppTest();
object.createAndShowGUIWebAppTest();
On the second frame i am using below code to go back to main frame:
MainLanding object = new MainLanding();
object.createAndShowGUIMainLanding();
frame1.dispose();
frame1.setVisible(false);
You would be better off using something like a single JFrame containing a CardLayout and selecting the correct panel that you need within the CardLayout rather than switching between completely different frames. So you have one window and then multiple different panels within that window and you choose the one to display at any given time.
If you do still decide to go down the separate-frames route you shouldn't dispose the first frame as well as hiding it. Just hide it and then reveal it again when you want to go back.