3

I have 3 JPanel components. Panel 1 is set to BorderLayout.NORTH, panel 2 is set to BorderLayout.CENTER and panel 3 is set to BorderLayout.SOUTH.

Now panel 1 has a GridLayout(1,2) and panel 2 also has GridLayout(1,2). The thing that bothers me is in panels 1 and 2 the components are aligned as though they are in GridLayout combined.

To be specific I have a set of JTextField components in panel 1 in position at row=0 and column=1.

I have a JTable inside JScrollPane at position row=0 and column=1 in panel 2.

Both these components are being rendered with the same dimension no matter what I try.

The code is too long to post here and I would appreciate if someone can clarify how to conceptually solve this problem.

public class PanelTest extends JFrame{
    public static void main (String args[]) {
        new PanelTest();
    }
    public PanelTest() {
        this.setPreferredSize(new Dimension(800,200));
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1,2));
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,2));

        JTextField textField1 = new JTextField();
        textField1.setPreferredSize(new Dimension(500, 100));
        JTextField textField2 = new JTextField();
        textField2.setPreferredSize(new Dimension(300, 100));
        panel1.setMinimumSize(new Dimension(800, 100));

        panel1.add(textField1);
        panel1.add(textField2);

        JTable table = new JTable();
        //... 
        JScrollPane jps1 = new JScrollPane(table);
        jps1.setPreferredSize(new Dimension(200, 300));
        JTextField textField3 = new JTextField(10); 
        JScrollPane jps2 = new JScrollPane(textField3);

        panel2.add(jps1);
        panel2.add(jps2);
        setLayout(new BorderLayout());
        add(panel1, BorderLayout.NORTH);
        add(panel2, BorderLayout.SOUTH);
        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Using GridBagLayout:

  public PanelTest() {
    this.setPreferredSize(new Dimension(800,200));
    this.setMinimumSize(new Dimension(800,200));
   JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridBagLayout());

    JTextField textField1 = new JTextField();
    textField1.setPreferredSize(new Dimension(500, 100));
    JTextField textField2 = new JTextField();
    textField2.setPreferredSize(new Dimension(300, 100));
    panel1.setMinimumSize(new Dimension(800, 100));

    GridBagConstraints gbc = new GridBagConstraints();
    //      gbc.insets = new Insets(5,100,5,1);
    gbc.gridx=0;
    gbc.gridy=0;
    panel1.add(textField1, gbc);
    gbc.gridx=1;
    panel1.add(textField2, gbc);

    JTable table = new JTable();
    //... 
    JScrollPane jps1 = new JScrollPane(table);
    jps1.setPreferredSize(new Dimension(600, 300));
    jps1.setMinimumSize(new Dimension(600, 300));
    JTextField textField3 = new JTextField(); 

    gbc = new GridBagConstraints();
    //      gbc.insets = new Insets(5,100,5,1);
    gbc.gridx=0;
    gbc.gridy=0;
    panel2.add(jps1, gbc);
    gbc.gridx=1;
    panel2.add(textField3, gbc);
    setLayout(new BorderLayout());
    add(panel1, BorderLayout.NORTH);
    add(panel2, BorderLayout.SOUTH);
    this.setVisible(true);
    this.setVisible(true);
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
user2654788
  • 181
  • 2
  • 14
  • 3
    Consider providing a **[runnable example](https://stackoverflow.com/help/mcve)** which demonstrates your problem. **This is not a code dump**, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jan 12 '16 at 00:27
  • 1
    It is hard to imagine what layout you want to get, and moreover to reproduce your current code to analise the problem. So runnable example is needed. – Antoniossss Jan 12 '16 at 00:29
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Jan 12 '16 at 05:49
  • I have edited above question and added some code. Thanks – user2654788 Jan 12 '16 at 17:11

1 Answers1

1

The thing that bothers me is in panels 1 and 2 the components are aligned as though they are in GridLayout combined.

From the Java docs:

GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.

"It will display them in equal size", so, if you have 2 columns and your JFrame's size is 900px, you will have 2 components of 450px each, if you had 3 then it will be 3 components of 300px each.

That's why you should avoid the use of setPreferred|maximum|minimum size methods and let the Layout Manager choose the dimensions for you or if you really really need a fixed size, you should instead override the getPreferred|maximum|minimum size as said by @trashgod in his answer.

You probably want to use a GridBagLayout instead or a different Layout Manager such as FlowLayout or BoxLayout and combine them.

You also should avoid extending JFrame unless it's extremely necessary.

Since you didn't posted how the UI should look like, I can imagine you wanted something like this:

enter image description here

Which is done with the following code:

import java.awt.*;
import javax.swing.*;
public class LayoutManagerExample {
    JFrame frame;
    JPanel pane1, pane2, pane3;
    JTextField field1, field2, field3;
    JTable table;
    JScrollPane jsp;
    LayoutManagerExample() {
        frame = new JFrame("LayoutManagerExample");
        pane1 = new JPanel();
        pane2 = new JPanel();
        pane3 = new JPanel();
        field1 = new JTextField(10);
        field2 = new JTextField(10);
        field3 = new JTextField(20);
        table = new JTable();
        jsp = new JScrollPane(table);

        pane1.setLayout(new FlowLayout());
        pane2.setLayout(new GridLayout(1, 2));
        pane3.setLayout(new FlowLayout());

        pane1.add(field1);
        pane1.add(field2);
        pane2.add(jsp);
        pane2.add(field3);
        pane3.add(new JLabel("Something else"));

        frame.add(pane1, BorderLayout.NORTH);
        frame.add(pane2, BorderLayout.CENTER);
        frame.add(pane3, BorderLayout.SOUTH);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main (String args[]) {
        new LayoutManagerExample();
    }
}
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Using GridBagLayout didn't alleviate the problem. The panel2 components were centered even when using Insets. The code is posted in the question above. Thanks – user2654788 Jan 12 '16 at 18:00
  • Yo didn't posted how it should look like as @AndrewThompson said in his comment above – Frakcool Jan 12 '16 at 18:01
  • Thank you Frakcool for the pic. But I was trying to specify table.setMaximumSize(new Dimension(600,300)). I understand from your comment that GridLayout cannot do that. That's why I was using GridBagLayout. Thx. – user2654788 Jan 12 '16 at 18:12
  • @user2654788 I'm confused, what you mean? Yes, `GridLayout` won't allow you to set up your own dimensions. – Frakcool Jan 12 '16 at 19:04
  • Sorry I meant table.setMinimumSize(new Dimension(600,300). Basically, I wanted the table component's width to be 600 in its own panel, regardless of the GridBagLayout specified in the panel above. Please check the code I posted for GridBagLayout above. Thanks – user2654788 Jan 12 '16 at 22:49
  • Does it needs to be a `GridBagLayout`? – Frakcool Jan 12 '16 at 23:13