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 8597617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:57:11+00:00 2026-06-12T00:57:11+00:00

I’ve a mainPanel with VerticalLayout (org.jdesktop.swingx.VerticalLayout) . main panel has several sub panels. One

  • 0

I’ve a mainPanel with VerticalLayout (org.jdesktop.swingx.VerticalLayout). main panel has several sub panels. One of them is a dynamically changing panel according to the user selection. So, I’ve set it’s layout as CardLayout which I think is the easiest (maybe best?) way to achieve that.

I’ll call that panel elasticPanel. As the name says, it should be elastic. Which means, it should be capable of both expanding and contracting. Let’s say it behave like this. If the user select 1, the elasticPanel should display one , say, JComboBox. If the user select 2 then, two JComboBoxs…

Ok, it works perfectly up to now. Now the user select 1 again when the elasticPanel is showing two JComboBoxs. What i need to happen now is the elasticPanel should display one JComboBox with it’s normal size. But since the elasticPanel is already expanded, what happens is it shows the JComboBox stretched to fit it’s size. So it gives a weird look.


Following screenshots show the problem i’m having with my interface.

Before the selection. NONE is selected.

enter image description here

An element is selected

enter image description here

NONE is selected again

enter image description here

I need the elasticPanel (Location of Fault) in the last screenshot to be as in the first screenshot. This is just a simple case. Imagine the look when going back to NONE after displaying about 5, 6 sub components.

I’ve tried the setSize() method. It doesn’t do anything..So how to fix the issue?

Any help is appreciated. Thanks!

  • 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-06-12T00:57:12+00:00Added an answer on June 12, 2026 at 12:57 am

    It’s hard to tell you are referring CardLayout to what thingy. Since CardLayout works in a different way. What you can do is simply place one JPanel say basePanel having GridLayout(0, 1) and place this JPanel on top of another JPanel say contentPanel, and now set this as the content pane for the JFrame and calling pack() when you add or remove an element from the view. Here is one example showing you what I mean.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ElasticPanel
    {
        private JFrame frame;
        private JPanel contentPane;
        private JPanel basePanel;
        /*
         * Array to hold the JComboBox
         * elements.
         */
        private JComboBox[] prodCombo;  
        private JComboBox[] temp;
        /* 
         * Counter to keep track
         * of the number of JComboBox 
         * present.
         */
        private int counter;  
        /*
         * Data for each JComboBox
         */
        private String[] data = {
                                    "None",
                                    "Sub Category"
                                };
    
        private ActionListener comboAction =
                new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                JComboBox cbox = (JComboBox) ae.getSource();
                String command = (String) ae.getActionCommand();
                int index = Integer.parseInt(command);
                String selection = (String) cbox.getSelectedItem();
    
                if (selection.equals("None"))
                {
                    /*
                     * i = index + 1, because, we want to
                     * remove all JComboBox after this one.
                     */
                    for (int i = (index + 1); i < prodCombo.length; i++)
                    {
                        temp = new JComboBox[prodCombo.length];
                        for (int j = 0; j < prodCombo.length; j++)
                            temp[j] = prodCombo[j];
                        basePanel.remove(prodCombo[i]); 
                    }
                    prodCombo = new JComboBox[index + 1];
                    for (int i = 0; i <= index; i++)
                    {                   
                        prodCombo[i] = temp[i];
                    }
                    counter = prodCombo.length;
                    System.out.println("Item Removed\nCounter : " + counter);
                }
                else if (selection.equals("Sub Category"))
                {
                    counter++;
                    temp = new JComboBox[counter];
                    for (int i = 0; i < prodCombo.length; i++)
                    {
                        temp[i] = prodCombo[i];
                    }
                    temp[counter - 1] = new JComboBox(data);
                    temp[counter - 1].setActionCommand("" + (counter - 1));
                    temp[counter - 1].addActionListener(this);
                    prodCombo = new JComboBox[counter];
                    for (int i = 0; i < counter; i++)
                        prodCombo[i] = temp[i];
                    basePanel.add(prodCombo[counter - 1]);  
                    System.out.println("Item Added\nCounter : " + counter);
                }
    
                //basePanel.revalidate();
                //basePanel.repaint();
                frame.pack();
            }
        };
    
        public ElasticPanel()
        {
            prodCombo = new JComboBox[1];
            counter = 1;
        }
    
        private void displayGUI()
        {
            frame = new JFrame("Elastic Panel Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            contentPane = new JPanel();
    
            basePanel = new JPanel(new GridLayout(0, 1, 5, 5));
    
            prodCombo[counter - 1] = new JComboBox(data);
            prodCombo[counter - 1].setActionCommand("" + (counter - 1));
            prodCombo[counter - 1].addActionListener(comboAction);
    
            basePanel.add(prodCombo[counter - 1]);
            contentPane.add(basePanel);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new ElasticPanel().displayGUI();
                }
            });
        }
    }
    

    *Latest Update : *

    More insight by adding more components and placing the elastic panel at some other location, and not on top of content pane.

    import java.awt.*;
    import java.awt.event.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.swing.*;
    
    public class VirtualViewGUI extends JFrame
    {
        private JPanel rightPanel;
        private ElasticPanel elasticPanel;
    
        public VirtualViewGUI()
        {
            super("Virtual View");
    
            JMenuBar jmenuBar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
            JMenu helpMenu = new JMenu("Help");
            JMenu feel = new JMenu("Look & Feel");
    
            JMenu layOutMenu = new JMenu("ConfigureCells");
            JMenuItem add_files = new JMenuItem("Select Directory.."); 
            JMenuItem minCellSize = new JMenuItem("height 260 X  width 260"); 
            JMenuItem moderateCellSize = new JMenuItem("height 320 X  width 320"); 
            JMenuItem maxCellSize = new JMenuItem("height 360 X  width 360"); 
            JMenuItem exit = new JMenuItem("Exit");
            JMenuItem help = new JMenuItem("Help Content");
    
            fileMenu.add(add_files);
            fileMenu.add(exit);
            layOutMenu.add(minCellSize);
            layOutMenu.add(moderateCellSize);
            layOutMenu.add(maxCellSize);
            helpMenu.add(help);
    
            jmenuBar.add(fileMenu);
            jmenuBar.add(layOutMenu);
            jmenuBar.add(helpMenu);
    
            ImageIcon myImage = null;
            try
            {
                myImage = new ImageIcon(
                    new URL("http://gagandeepbali.uk.to/" + 
                            "gaganisonline/images/swing/" + 
                            "stackoverflow/cow-cartoon.jpg"));
            }
            catch(MalformedURLException mue)    
            {
                mue.printStackTrace();
            }
    
            JLabel icon = new JLabel(myImage);
            icon.setIcon(myImage);
            setJMenuBar(jmenuBar); 
    
            rightPanel = new JPanel();
            elasticPanel = new ElasticPanel(this);
            rightPanel.add(elasticPanel);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout(5, 5));
            contentPane.add(icon, BorderLayout.CENTER);
            contentPane.add(rightPanel, BorderLayout.LINE_END);
    
            setContentPane(contentPane);
            pack();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationByPlatform(true);    
            setVisible(true);
            System.out.println("File Separator is : " + System.getProperty("file.separator"));
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new VirtualViewGUI();
                }
            });
        }
    }
    
    class ElasticPanel extends JPanel
    {
        private JFrame frame;
        private JPanel contentPane;
        /*
         * Array to hold the JComboBox
         * elements.
         */
        private JComboBox[] prodCombo;  
        private JComboBox[] temp;
        /* 
         * Counter to keep track
         * of the number of JComboBox 
         * present.
         */
        private int counter;  
        /*
         * Data for each JComboBox
         */
        private String[] data = {
                                    "None",
                                    "Sub Category"
                                };
    
        private ActionListener comboAction =
                new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                JComboBox cbox = (JComboBox) ae.getSource();
                String command = (String) ae.getActionCommand();
                int index = Integer.parseInt(command);
                String selection = (String) cbox.getSelectedItem();
    
                if (selection.equals("None"))
                {
                    /*
                     * i = index + 1, because, we want to
                     * remove all JComboBox after this one.
                     */
                    for (int i = (index + 1); i < prodCombo.length; i++)
                    {
                        temp = new JComboBox[prodCombo.length];
                        for (int j = 0; j < prodCombo.length; j++)
                            temp[j] = prodCombo[j];
                        remove(prodCombo[i]);   
                    }
                    prodCombo = new JComboBox[index + 1];
                    for (int i = 0; i <= index; i++)
                    {                   
                        prodCombo[i] = temp[i];
                    }
                    counter = prodCombo.length;
                    System.out.println("Item Removed\nCounter : " + counter);
                }
                else if (selection.equals("Sub Category"))
                {
                    counter++;
                    temp = new JComboBox[counter];
                    for (int i = 0; i < prodCombo.length; i++)
                    {
                        temp[i] = prodCombo[i];
                    }
                    temp[counter - 1] = new JComboBox(data);
                    temp[counter - 1].setActionCommand("" + (counter - 1));
                    temp[counter - 1].addActionListener(this);
                    prodCombo = new JComboBox[counter];
                    for (int i = 0; i < counter; i++)
                        prodCombo[i] = temp[i];
                    add(prodCombo[counter - 1]);    
                    System.out.println("Item Added\nCounter : " + counter);
                }
    
                //revalidate();
                //repaint();
                frame.pack();
            }
        };
    
        public ElasticPanel(JFrame frame)
        {
            this.frame = frame;
            prodCombo = new JComboBox[1];
            counter = 1;
    
            setLayout(new GridLayout(0, 1, 5, 5));
    
            prodCombo[counter - 1] = new JComboBox(data);
            prodCombo[counter - 1].setActionCommand("" + (counter - 1));
            prodCombo[counter - 1].addActionListener(comboAction);
    
            add(prodCombo[counter - 1]);        
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I have an array which has BIG numbers and small numbers in it. I
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

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.