View unanswered posts | View active topics It is currently Tue Apr 25, 2017 11:39 am



Reply to topic  [ 1 post ] 
 JComboBox resizes when i set new ComboBoxModel 
Author Message

Joined: Wed Aug 27, 2008 3:04 am
Posts: 1
Post JComboBox resizes when i set new ComboBoxModel
Hi:
i am new with MigLayout, i trying, and replace all GridBagLayout in my Framework (a little one).
My english is bad, then i'll be concise:

behaviour expected in GridBagLayout:
Code:
        JComboBox cmb1 = new JComboBox();
        cmb1.setPreferredSize(new Dimension(80, 25));
        ...
        c = new GridBagConstraints();
        c.gridx=1;c.gridy=1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx=1;
        p.add(cmb1,c);
       


1. when i set a new ComboBoxModel (with text longer than previous), the JComboBox dont resize,
2. when I resize de Dialog, that cointains The panel, the JComboBox grows, as i expect

In the Other hand, with MigLayout:

Code:
        JComboBox cmb1 = new JComboBox();
        cmb1.setPreferredSize(new Dimension(80, 25));
        ...
        c = new CC().growX().pushX();
        p.add(cmb1,c);

1. when i set a new ComboBoxModel (with text longer than previous), the JComboBox grows (even more that the panel).

2. when I resize de Dialog, that cointains The panel, the JComboBox grows, as i expect

The preferred size is not respected, when i change de ComboBoxModel , as if the Preferredsize Was null.

How, i can reach the expected behaviour , like with GridBagLayout?

I use un Generic Dynamic JDialog, in my framework, then if I fix this problem with Miglayout, then i resolve hundreds Dialogs :)

this is my Complete Test Code
with GridBagLayout
Code:
package test;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Ing. Jose Arturo Munive Solis
 */
public class PruebaCombosGBL {

    public static void main(String[] args) {
        final JComboBox cmb1 = new JComboBox();
        cmb1.setPreferredSize(new Dimension(80, 25));
        JButton btn1 = new JButton("short model");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DefaultComboBoxModel mdl = new DefaultComboBoxModel();
                mdl.addElement("aa");
                mdl.addElement("bb");
                mdl.addElement("cc");
                cmb1.setModel(mdl);
            }
        });
       
        JButton btn2 = new JButton("long model");
        btn2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DefaultComboBoxModel mdl = new DefaultComboBoxModel();
                mdl.addElement("zzzzzzzz");
                mdl.addElement("abcdefghijklmnopqrstuvwxyz");
                mdl.addElement("ww");
                cmb1.setModel(mdl);
            }
        });
       
        JButton btn3 = new JButton("reset Preferred Size");
        btn3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cmb1.setPreferredSize(null);               
            }
        });

        JFrame f = new JFrame("GRID BAG LAYOUT");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel(new GridBagLayout());
        GridBagConstraints c;
        c = new GridBagConstraints();
        c.gridx=0;c.gridy=0;
        p.add(btn1,c);
       
        c = new GridBagConstraints();
        c.gridx=1;c.gridy=0;c.anchor = GridBagConstraints.LINE_START;
        p.add(btn2,c);
       
        c = new GridBagConstraints();
        c.gridx=0;c.gridy=1;
        p.add(btn3,c);
       
        c = new GridBagConstraints();
        c.gridx=1;c.gridy=1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx=1;
        p.add(cmb1,c);
       
       
        f.setContentPane(p);
        f.pack();
        f.setVisible(true);
    }
}




with MigLayout
Code:
package test;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Ing. Jose Arturo Munive Solis
 */
public class PruebaCombosML {

    public static void main(String[] args) {
        final JComboBox cmb1 = new JComboBox();
        cmb1.setPreferredSize(new Dimension(80, 25));
        JButton btn1 = new JButton("short model");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DefaultComboBoxModel mdl = new DefaultComboBoxModel();
                mdl.addElement("aa");
                mdl.addElement("bb");
                mdl.addElement("cc");
                cmb1.setModel(mdl);
            }
        });
       
        JButton btn2 = new JButton("long model");
        btn2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DefaultComboBoxModel mdl = new DefaultComboBoxModel();
                mdl.addElement("zzzzzzzz");
                mdl.addElement("abcdefghijklmnopqrstuvwxyz");
                mdl.addElement("ww");
                cmb1.setModel(mdl);               
            }
        });
       
        JButton btn3 = new JButton("reset preferred size");
        btn3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cmb1.setPreferredSize(null);               
            }
        });

        JFrame f = new JFrame("MIG LAYOUT");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        JPanel p = new JPanel(new MigLayout(new LC().debug(200)));
        CC c;
        c = new CC();
        p.add(btn1,c);
       
        c = new CC().wrap();
        p.add(btn2,c);
       
        c = new CC();
        p.add(btn3,c);
       
        c = new CC().growX().pushX();
        p.add(cmb1,c);
               
        f.setContentPane(p);
        f.pack();
        f.setVisible(true);
    }
}



Thanks for all


Wed Aug 27, 2008 3:33 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.