1

I have two JList controls side by side. And few buttons in between them. The layout looks fine when I run from eclipse.

enter image description here

But if I run the exported jar file, the left occupies more space. I don't know why it's doing this.

enter image description here

I place them in a GridBagLayout like this.

mLstMdlTestSuitesAvailable = new DefaultListModel<String>();
mLstTestSuitesAvailable = new JList<String>();
mLstTestSuitesAvailable.setModel(mLstMdlTestSuitesAvailable);
mLstTestSuitesAvailable.setMinimumSize(new Dimension(100, 100));
JScrollPane scrollPaneTestSuitesAvailable = new JScrollPane(mLstTestSuitesAvailable);
GridBagConstraints gbcLstTestSuitesAvailable = new GridBagConstraints();
gbcLstTestSuitesAvailable.fill = GridBagConstraints.BOTH;
gbcLstTestSuitesAvailable.weightx = 10;
gbcLstTestSuitesAvailable.gridwidth = 10;
gbcLstTestSuitesAvailable.weighty = 8;
gbcLstTestSuitesAvailable.gridheight = 8;
gbcLstTestSuitesAvailable.insets = new Insets(0, 11, 10, 11);
gbcLstTestSuitesAvailable.gridx = 0;
gbcLstTestSuitesAvailable.gridy = 1;
panel.add(scrollPaneTestSuitesAvailable, gbcLstTestSuitesAvailable);

mLstMdlTestSuitesSelected = new DefaultListModel<String>();
mLstTestSuitesSelected = new JList<String>();
mLstTestSuitesSelected.setModel(mLstMdlTestSuitesSelected);
GridBagConstraints gbcLstTestSuitesSelected = new GridBagConstraints();
gbcLstTestSuitesSelected.fill = GridBagConstraints.BOTH;
gbcLstTestSuitesSelected.weightx = 10;
gbcLstTestSuitesSelected.gridwidth = 10;
gbcLstTestSuitesSelected.weighty = 8;
gbcLstTestSuitesSelected.gridheight = 8;
gbcLstTestSuitesSelected.insets = new Insets(0, 11, 10, 11);
gbcLstTestSuitesSelected.gridx = 15;
gbcLstTestSuitesSelected.gridy = 1;
JScrollPane scrollPaneTestSuitesSelected = new JScrollPane(mLstTestSuitesSelected);
panel.add(scrollPaneTestSuitesSelected, gbcLstTestSuitesSelected);

Can anyone please help me figure out what's going wrong here?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Karthik Andhamil
  • 848
  • 1
  • 13
  • 22

3 Answers3

1

As shown here for JTable, you can override the list's getPreferredScrollableViewportSize() method to specify the initial, preferred size. Uisng a multiple of getRowHeight() offers a nice appearance.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

The GridBagLayout respects the preferred sizes of the contained components. And the preferred size of a JList is determined by its contents. By looking at your screenshots it becomes clear that your JLists have different contents depending on the environment you are running in, therefore it’s not surprising that the preferred sizes differ.

If you want to have a more predictable behavior, have a look at setPrototypeCellValue. This allows you to set a “typical value” which determines the cell size. The advantage over setFixedCellWidth is that it still respects fonts, display resolution, etc.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • My bad. actually both of them launches with the same values. But user has options to change the values. So i took screen shots with different value. But, even if they are same, the size differs. You can look at the edited question now. – Karthik Andhamil Sep 26 '14 at 16:50
  • What’s interesting is that the height of the pictures differs. So you seem to enforce a height, e.g. via `JFrame`’s size. Then my guess is that the `GridBagLayout` switches between preferred and minimum size strategy. Try to change the frame’s size a bit and look what happens. – Holger Sep 26 '14 at 17:09
0

I figured out why. A banner image at the top is not exported and so that made the mess in jar's layout. When i added the image to a folder where jar is exported to, then, it shows exactly same as the one run from eclipse. Thank you all.

Karthik Andhamil
  • 848
  • 1
  • 13
  • 22