views:

280

answers:

1

I have a component that is made up of various components such as a checkbox a slider and some buttons. i want to add this to a scrollpane and have the slider grow to fill all the remaining space. This is no problem as this code demonstrates :

 public static void main(String[] args) {
    JFrame f = new JFrame("Test");
    JPanel c = new JPanel(new MigLayout(
           "",          
           "[]5[]10[grow]10[]0[]0[]0[]",
           "[]"
    ));
    c.add(new JCheckBox(""));
    c.add(new JLabel("Name"));
    c.add(new JSlider());
    c.add(new JButton("1"));
    c.add(new JButton("2"));
    c.add(new JButton("3"));
    c.add(new JButton("4"));
    f.getContentPane().add(new JScrollPane(c));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
 }

If you run this and resize the frame, the slider fills all the space. My problem lies in the fact that i want to add my component (the one with the slider) in another component that contains different instances of this one. So for instance it will contain 3 or 4 components with sliders one below the other. I thought this would work :

 public static void main(String[] args) {
    JFrame f = new JFrame("Test");
    JPanel c = new JPanel(new MigLayout(
           "",          
           "[]5[]10[grow]10[]0[]0[]0[]",
           "[]"
    ));
    c.add(new JCheckBox(""));
    c.add(new JLabel("Name"));
    c.add(new JSlider());
    c.add(new JButton("1"));
    c.add(new JButton("2"));
    c.add(new JButton("3"));
    c.add(new JButton("4"));
    JPanel a = new JPanel(new MigLayout("wrap 1"));
    a.add(c);
    f.getContentPane().add(new JScrollPane(a));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
 }

But it doesn't. Any thoughts as to why and how to fix it?

+1  A: 

Try changing the JPanel a declaration to:

JPanel a = new JPanel(new MigLayout("wrap 1", "[grow,fill]"));
CarlG
Aha! Thanks... i need the "fill" parameter on both mig layouts but it works. :)
Savvas Dalkitsis