Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6873299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:02:57+00:00 2026-05-27T04:02:57+00:00

I have a JFrame containing a set of JPanels in a CardLayout . Each

  • 0

I have a JFrame containing a set of JPanels in a CardLayout. Each JPanel has a different size and I want the JFrame to adapt to the size of the currently displayed JPanel (not the JPanel to adapt to the size of the JFrame).

How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:02:58+00:00Added an answer on May 27, 2026 at 4:02 am

    The general is: if you have a layout problem, always solve it with an appropriate LayoutManager. Never tweak a component’s sizing hint to reach your goal.

    In this case, it’s particularly easy to adjust the CardLayout. By default, it calculates its prefSize to the max of prefSizes of all cards. Simply subclass and implement to return the prefSize (plus insets) of the currently visible card:

    public static class MyCardLayout extends CardLayout {
    
        @Override
        public Dimension preferredLayoutSize(Container parent) {
    
            Component current = findCurrentComponent(parent);
            if (current != null) {
                Insets insets = parent.getInsets();
                Dimension pref = current.getPreferredSize();
                pref.width += insets.left + insets.right;
                pref.height += insets.top + insets.bottom;
                return pref;
            }
            return super.preferredLayoutSize(parent);
        }
    
        public Component findCurrentComponent(Container parent) {
            for (Component comp : parent.getComponents()) {
                if (comp.isVisible()) {
                    return comp;
                }
            }
            return null;
        }
    
    }
    

    Using that (borrowing @mKorbel’s example), the main method cleanly shrinks down:

    private static void createAndShowUI() {
        final CardLayout cardLayout = new MyCardLayout();
        final JPanel cardHolder = new JPanel(cardLayout);
        final JFrame frame = new JFrame("MultiSizedPanels");
        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 * (i + 1);
            Border lineBorder = BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.blue),
                BorderFactory.createEmptyBorder(padding, padding, padding, padding));
            labels[i].setBorder(lineBorder);
            JPanel containerPanel = new JPanel();
            containerPanel.add(labels[i]);
            cardHolder.add(containerPanel, String.valueOf(i));
        }
        JButton nextButton = new JButton("Next");
        nextButton.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.next(cardHolder);
                frame.pack();
            }
        });
        JPanel btnHolder = new JPanel();
        btnHolder.add(nextButton);
    
        frame.add(cardHolder, BorderLayout.CENTER);
        frame.add(btnHolder, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame with a CardLayout set as its layout manager. This has
I have a JFrame(frm) in which I've added a JPanel (pnl1)..which in turn has
I have a JFrame which has 3 JPanels in GridBagLayout .. Now, when I
I have a JFrame containing three JPanel. The first JPanel contains a JTextField and
I have created a JFrame subclass containing 9 JPanels using a GridLayout (3x3). I'm
I have a JFrame with a JPanel on it. I want to add another
I'm using Swing and I have a JFrame containing a few JPanels, and inside
I have a JFrame containing a JScrollPane containing a JPanel (with only graphics) and
I am using swing to create my GUI. J have a JFrame containing one
I have a JFrame with 1 panel for drawing and set KyeListener for JFrame.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.