Reputation: 558
I just want to know if there is a way to resize the jpanels that has CardLayout as its layout. I have here a picture where you can see I have 5 panels.
Here is my code snippets:
JPanel container = new JPanel();
JPanel panel_1 = new JPanel(); // red
JPanel panel_2 = new JPanel(); // violet
JPanel panel_3 = new JPanel(); // violet
JPanel panel_4 = new JPanel(); // blue
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,1,1);
CardLayout cl = new CardLayout();
This one is for my fist panel which is the no. 3 in the picture.
panel_1 = new JPanel();
panel_1.setBackground(Color.WHITE);
panel_1.setBorder(new EmptyBorder(0, 0, 0, 0));
panel_1.setLayout(flow);
JButton btnGroup1 = new JButton("<html><p align=center>Group<br>01</p></html>");
btnGroup1.setIcon(new ImageIcon("more_buttons\\green.png"));
btnGroup1.setBorderPainted(false);
btnGroup1.setFocusPainted(false);
btnGroup1.setContentAreaFilled(false);
btnGroup1.setHorizontalTextPosition(JButton.CENTER);
btnGroup1.setFont(new Font("Calibri", Font.BOLD, 18));
btnGroup1.setPreferredSize(new Dimension(80, 80));
panel_1.add(btnGroup1);
And this is the rest of my panel.
panel_2 = new JPanel();
panel_2.setBackground(Color.BLUE);
panel_2.setBorder(new EmptyBorder(0, 0, 0, 0));
panel_2.setLayout(flow);
JButton button_2 = new JButton("<html><p align=center> </p></html>");
button_2.setIcon(new ImageIcon("more_buttons\\lightblue.png"));
button_2.setBorderPainted(false);
button_2.setFocusPainted(false);
button_2.setContentAreaFilled(false);
button_2.setHorizontalTextPosition(JButton.CENTER);
button_2.setFont(new Font("Calibri", Font.BOLD, 12));
button_2.setPreferredSize(new Dimension(80, 80));
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
cl.show(container, "4");
}
});
panel_2.add(button_2);
panel_3 = new JPanel();
panel_3.setBackground(Color.WHITE);
panel_3.setBorder(new EmptyBorder(0, 0, 0, 0));
panel_3.setLayout(flow);
JButton button_no_7 = new JButton("7");
button_no_7.setIcon(new ImageIcon("more_buttons\\gray.png"));
button_no_7.setBorderPainted(false);
button_no_7.setFocusPainted(false);
button_no_7.setContentAreaFilled(false);
button_no_7.setHorizontalTextPosition(JButton.CENTER);
button_no_7.setFont(new Font("Calibri", Font.BOLD, 30));
button_no_7.setPreferredSize(new Dimension(80, 80));
panel_3.add(button_no_7);
add(container);
container.add(panel_1, "1");
container.add(panel_2, "2");
container.add(panel_3, "3");
container.add(panel_4, "4");
What I'm trying to do is I have to change the 4th panel on the picture above with another panel that's why I used CardLayout but what I get is only 1, 2, and 3 showing up. It's taking the space of 4 and 5 panel. That's why I want to ask if there is in any way I can resize the panel for 3 so that panel 4 and 5 can be seen. I know that .setPreferredSize(new Dimension(355, 10)); is not working since it gets override by the layout manager. So is there anyone who can help me with this?
Upvotes: 1
Views: 1686
Reputation: 12738
@camickr has said it clearly. I think you have understand wrongly how to use CardLayout
.
Add this line:
panel_4.setLayout(new CardLayout());
and add the 2 panels you want to switch into panel_4
. Let's say they are A
and B
.
panel_4.add(A, "panelA");
panel_4.add(B, "panelB");
To show them, add a button to toggle.
CardLayout cc = (CardLayout)(panel_4.getLayout());
cc.show(panel_4, "panelA");
The first parameter of this method is the parent container, the second, the name of panel to show.
In your code, I see you want to make the container
to show panel_4
, but a panel with CardLayout
will show the panel in all of its space. It cannot show one panel in the left top corner, and remain the rest part intact. All, or nothing.
So, this line:
container.add(panel_4, "4");
will show in all its area, panel_4.
Upvotes: 1
Reputation: 324128
The point of using a CardLayout is that the panel all occupy the same space and only one panel can be displayed at a time.
If you want multiple panels to be visible, then you need to use a different layout manager that displays the multiple child panels in a parent panel.
Read the section from the Swing tutorial on Using Layout Managers for more information about layout managers. Then you can use nested panels to achieve your desired layout.
Also, your code should not be using setPreferredSize(). It is the job of the layout manager to set the size/location of a component.
So maybe you start with a main panel that has a BorderLayout. Then you add panel1 to the NORTH and pan2l2 to the WEST. Then you create another panel that you add to the CENTER. Then you set an appropriate layout for that panel and add panel 3, 4, to the panel. This is the way you logically nest panels to get your desired layout.
Upvotes: 3