0

Is there any method I could implement that will ensure each button within the pane maintains a minimum height? I have attempted using itembutton.setSize() method but it has no effect.

JPanel itemPanel = new JPanel();

itemPanel.setLayout(new GridLayout(0,1));

for(final Item i: list){
    JButton itemButton = new JButton(i.getName());
    itemPanel.add(itemButton);
}

JScrollPane itemPane = new JScrollPane(itemPanel);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
rmp2150
  • 777
  • 1
  • 11
  • 22

1 Answers1

0

itembutton.setMinimumSize(minimumSize) ?

Edit: Just found that, as this java tutorial seems to tell, there is no way to do that with GridLayout.

Each component takes all the available space within its cell, and each cell is exactly the same size

So I guess you'll have to try another layout. I can suggest (don't know if it's well suited but it works) GridBagLayout. Example with 2 buttons:

itemPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.weightx = 0.5;
itemPanel.add(new JButton("A"), c);
c.gridx = 1;
c.weightx = 0.5;
itemPanel.add(new JButton("B"), c);

Have a look to http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

user978548
  • 711
  • 2
  • 7
  • 12
  • Note that I believe (not sure about that) it also depends on the choosen layout and his hints about sizes. – user978548 Apr 02 '12 at 02:14
  • Yeah, I think the problem originates from one of my chosen layouts. Thanks for your help anyway – rmp2150 Apr 02 '12 at 02:19
  • So setMinimumSize() has no effect ? – user978548 Apr 02 '12 at 02:21
  • Unfortunately, setMinimumSize() had no effect. – rmp2150 Apr 02 '12 at 04:38
  • 1
    @rmp2150 don't even think of using any of the setXXSize methods, ever http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519 - instead use a LayoutManager that supports your requirement – kleopatra Apr 02 '12 at 09:14