Reputation: 55
i have 2 jbuttons in my java application. I have 2 jpanels too. these 2 jpanels have different contents i.e not related to eachother. now I want that whenever a user clicks a button then the corresponding jpanel should be displayed but at the same location( the jpanels have different width & height).
when i tried to do it in netbeans one jpanel is included inside another jpanel
private void button1ActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
button1panel.setVisible(true);
//all other panel's visibility is false
}
like that for button2 i have actionlistener like this
private void button2ActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
//all other panel's visibility is false
button2panel.setVisible(true);
}
i have placed these 2 panels one above another(button2panel over button1panel) so that i can access it in the same location
but as the button2panel is small in width & height when i call button2panel.setVisible(true) it actually not enabled.
my demonstration says that as the button2panel is inside the button1panel & the button1panel's visibility is false that's why its happening.
what can i do is just to take the button2panel outside the button1panel but if i do this then the panels location would be different
how to fix it??
or any better solution for this??
Upvotes: 1
Views: 119
Reputation: 83557
I suggest that you check out A Visual Guide to Layout Managers to help you choose the best LayoutManager to use for your particular situation. LayoutManagers are a powerful tool provided by the Swing framework. They calculate the location and size of GUI components for you based on a minimal number of parameters which you supply.
Upvotes: 1
Reputation: 324147
See the section from the Swing tutorial on How to Use Card Layout, which does what you want. The CardLayout wil make the panels the same size, which is better than having the frame size change as you swap panels.
Upvotes: 4