Avatar of ChartTrack
ChartTrack

asked on 

Resizing a panel to fit components

I am dynamically creating and deleting comboboxes onto a panel.
My code works fine for the creating and deleting, however, I am having problems laying them out properley and can't figure out how to resize the panel to fit the components I have created.

I want the combos anchodered from the top left and filling downwards (using GridBagLayout.).

The following code shows what I have done so far.
private void addCombo(String name) {                                       
        java.awt.GridBagConstraints gridBagConstraints;
        JMappedComboBox newCombo = new anecho.gui.JMappedComboBox();
        newCombo.setSize(new Dimension(500,100));
        newCombo.setName(name);
 
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = comboPosition;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipady = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        
        newCombo.addItem(name,"1");
        newCombo.addItem("Two","2");
        newCombo.addItem("Three","3");
        newCombo.addItem("Four","4");
       
        jPanel2.add(newCombo, gridBagConstraints);
        //jPanel2.setl
        reloadLayout(true);
    }
 
    private void subCombo(String name) {                                       
        Component[] combos = jPanel2.getComponents();
        for (int i = 0; i < combos.length; i++)
        {
            if (name.equals(combos[i].getName())){
                jPanel2.remove(combos[i]);
                reloadLayout(false);
            }
        }
    }                                      
 
    private void reloadLayout(boolean insert){
    
        int width = 0;
        int widest = 0;
        if(insert){
            comboPosition = comboPosition + 1;
            Component[] combos = jPanel2.getComponents();
            for (int i = 0; i < combos.length; i++)
            {
                width = combos[i].getWidth();
                if(width > widest){widest = width;}
            }
            for (int i = 0; i < combos.length; i++)
            {
                combos[i].setSize(widest,combos[i].getHeight());
            }
        }
        else{
            // do nothing
        }
        
        jPanel2.revalidate();
        jPanel2.repaint();
    }

Open in new window

Editors IDEsJava

Avatar of undefined
Last Comment
sciuriware
Avatar of sciuriware
sciuriware

Use the .pack method.

;JOOP!
Avatar of sciuriware
sciuriware

That will be:

        jPanel2.revalidate();
        jPanel2.repaint();
        jPanel2.pack();

may be also on the whole frame there after:


        jFrame.pack();


;JOOP!
Avatar of ChartTrack
ChartTrack

ASKER

That works - it seems you need the panel to have DEFAULT dimensions, rather than be sized.
As an extra, how do I make the combos all the same size?
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo