Question

I am trying to make a layout for my applet, but I cannot handle one problem - some of the elements (e.g. JComboBox) are as big as they can be - take all place in applet. setSize function do not work. What can I do to make them normal sized? (some elements e.g. JButtons and JLabels have correct sizes).

My code:

JPanel cp=new JPanel();

String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";

JComboBox c = new JComboBox(s);

JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);

JLabel l=new JLabel("Name of product");

JButton b=new JButton("Send a message");
b.setEnabled(true);

cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);

GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);

layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(
   layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
           .addComponent(c)
           .addComponent(pb)
           .addComponent(l)
           .addComponent(b))
);
layout.setVerticalGroup(
   layout.createSequentialGroup()
      .addComponent(c)
      .addComponent(pb)
      .addComponent(l)
      .addComponent(b)
);

add(cp);
Was it helpful?

Solution

Try using the setPreferredSize() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top