0

I have a frame with the following layout of 3 JPanels.

----------------------------
|   |                      |
| l |                      |
| e |  //toppanel          |
| f |                      |   
| t |----------------------|
| p |                      |
| n |  //bottompanel       |
| l |                      |
|   |                      |
----------------------------

I achieved this with the use of GridBagLayout

This is the code for that layout

public class UITests extends JFrame{
    public UITests(){
        setLayout(new GridBagLayout());

        //the three main panels
        JPanel leftPanel = new JPanel();
        JPanel topPanel = new JPanel();
        JPanel bottomPanel = new JPanel();

        leftPanel.setBackground(Color.YELLOW);
        topPanel.setBackground(Color.GREEN);
        bottomPanel.setBackground(Color.PINK);

        //position the three main panels
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.gridwidth = 1;
        gbc.fill = gbc.BOTH;
        gbc.weightx = .2;
        gbc.weighty = 1;
        getContentPane().add(leftPanel, gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.fill = gbc.BOTH;
        gbc.weightx = .8;
        gbc.weighty = .5;
        getContentPane().add(topPanel, gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.fill = gbc.BOTH;
        gbc.weightx = .8;
        gbc.weighty = .5;
        getContentPane().add(bottomPanel, gbc);


        setSize(600,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args){
        new UITests();
    }
}

Problem is, when I add a JTable in the top panel, the bottom panel's height is shrinked, like some of its height is taken by the top panel. Here is the code for the table in the top panel , added after setting the background of three main panels.

Object[][] contents = {{"haha","hehe", "hihi", "hoho", "huhu"},
        {"haha","hehe", "hihi", "hoho", "huhu"},
        {"haha","hehe", "hihi", "hoho", "huhu"},
        {"haha","hehe", "hihi", "hoho", "huhu"}};
Object[] heads = {"Haha Head", "Hehe Head", "Hihi Head", "Hoho Head", "Huhu Head"};
JTable table = new JTable(contents, heads);
JScrollPane scrollTable = new JScrollPane(table);

//add components to topPanel
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = gbc.BOTH;
topPanel.setLayout(new GridBagLayout());
topPanel.add(scrollTable, gbc);

The frame will now look like

----------------------------
|   |                      |
| l |                      |
| e |  //toppanel          |
| f |  //new table created |   
| t |                      |
| p |                      |
| n |                      |
| l |----------------------|
|   |  //bottompanel       |
----------------------------

So, how can I make the space created by 'weightx' or 'weighty' fixed?

Bnrdo
  • 5,325
  • 3
  • 35
  • 63

1 Answers1

2

weightx/weighty are used to distribute extra space. So in fact container asks children for their preferred size and if it's smaller than available the extra pixels are distributed according to the weights.

So you can define preferred size of the JScrollPane which holds the JTable

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I understand but according to [this thread](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) `setPreferredSize()` should be avoided and instead let `LayoutManager` handle the resizing. I'm wondering if I can achieve this without using that method. – Bnrdo Mar 25 '13 at 01:29
  • You can place both top and bottom panels to a container with GridLayout (2 rows 1 col). Or you can override bottomPanel's preffered size to return topPanel's height as preferred. – StanislavL Mar 25 '13 at 05:58
  • I appreciate your suggestion. But I want to achieve this using `GridBagLayout` only. I've heard that this layout is very flexible, so I'm wondering if it can be achieved using `GridBagLayout` only, without interfering with the component's `setPrefferedSize`. – Bnrdo Mar 25 '13 at 06:11
  • Ok so looks like theres no other way but to follow your suggestion, so answer accepted. – Bnrdo Mar 26 '13 at 02:10