Change size of JPanel using CardLayout
Asked Answered
L

1

2

is it possible to change the size of Jpanels when using Java CardLayout?

Lancer answered 24/3, 2011 at 2:44 Comment(3)
Yes, it's possible. You can nest your JPanels for instance. If you want more detail in an answer though, you may need to do likewise with your question.Dispel
@Hovercraft Full Of Eels, Well, I have added 3 different JPanels to a CardLayout, but I want them to display as different sizes, I have tried using setSize() but it doesn't work and I am not sure what else to try.Lancer
Consider nesting them inside of other panels with the other panels using another layout such as a FlowLayout, and then swap the container panels. And don't use setSize with most layout managers. Instead use setPreferredSize.Dispel
D
3

shoot, something like this where the component (here a JLabel rather than a JPanel) has it's preferredSize set, then place it in another JPanel that uses an appropriate layout, here GridBagLayout which with default settings will center the component, and add the GridBagLayout using JPanel to the CardLayout using panel::

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

public class MultiSizedPanels {

   private static void createAndShowUI() {
      final CardLayout cardLayout = new CardLayout();
      final JPanel cardHolder = new JPanel(cardLayout);

      JLabel[] labels = {
         new JLabel("Small Label", SwingConstants.CENTER),
         new JLabel("Medium Label", SwingConstants.CENTER),
         new JLabel("Large Label", SwingConstants.CENTER)};

      for (int i = 0; i < labels.length; i++) {
         int padding = 50;
         Dimension size = labels[i].getPreferredSize();
         size = new Dimension(size.width + 2 * (i + 1) * padding, size.height + 2 * (i + 1) * padding);
         labels[i].setPreferredSize(size);
         Border lineBorder = BorderFactory.createLineBorder(Color.blue);
         labels[i].setBorder(lineBorder);
         JPanel containerPanel = new JPanel(new GridBagLayout());
         containerPanel.add(labels[i]);
         cardHolder.add(containerPanel, String.valueOf(i));
      }

      JButton nextButton = new JButton("Next");
      nextButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            cardLayout.next(cardHolder);
         }
      });
      JPanel btnHolder = new JPanel();
      btnHolder.add(nextButton);

      JFrame frame = new JFrame("MultiSizedPanels");
      frame.getContentPane().add(cardHolder, BorderLayout.CENTER);
      frame.getContentPane().add(btnHolder, BorderLayout.SOUTH);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
Dispel answered 24/3, 2011 at 4:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.