1

I am having issue where my JPanel is not setting up the size. I am not sure if is something to do with my JTab or JFrame. I am using GridBagLayout layout management. And for some reason are not able to set the size.

Here is a dummy code, following the same logic to my original source code:

FirstPanel.java

import javax.swing.*;
import java.awt.*;

public class FirstPanel extends JPanel {
    private JLabel label1 = new JLabel("Label 1");
    private JTextField textField1 = new JTextField();
    private GridBagConstraints c = new GridBagConstraints();

    public FirstPanel() {
        //Size is not overriding
        Dimension size = getPreferredSize();
        size.width = 100;
        setPreferredSize(size);
        setBorder(BorderFactory.createTitleBorder("Border Title");

        setLayout(new GridBagLayout());
        addComponents();
    }

    private void addComponents() {
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(5, 0, 0, 0);
        add(label1, c);

        c.gridx = 1;
        add(textField1, c);

        c.weightx = 1;
        c.weighty = 1;
        add(new JLabel(""), c);
    }
}

MainPanel.java

import javax.swing.*;
import java.awt.*;

public class MainPanel {
    private JFrame frame = new JFrame("App");
    private JPanel panel1 = new JPanel(new GridBagLayout());
    private GridBagConstraints c = new GridBagConstraints();
    private JTabbedPane tabPane = new JTabbedPane();

    public MainPanel() {
        addComponents();

        frame.add(tabPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 350);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
    }

    private void addComponents() {
        tabPane.addTab("Tab 1", new FirstPanel());
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        new MainPanel();
    }
}
IntFooBar
  • 174
  • 3
  • 18
  • 3
    The point of using a layout manager is that you don't set the size. The layout manager will set the size of each component based on the preferred size of the component and the rules of the layout manager. For example when you create a JTextField you should use something like: `new JTextField(10)`. The "10" will help the textfield calculate a preferred size. Then the size of the panel will be based on the preferred size of the label and text field added to it. – camickr Sep 30 '17 at 01:22
  • https://www.youtube.com/watch?v=YKaea4ezQQE I am using this YouTube video as a reference, and the person has set the size. I need to have a fixed size of my JPanel where I can add the components inside. – IntFooBar Sep 30 '17 at 01:51
  • 2
    The YouTube video is wrong. It is backwards. You add the components to the panel and the panel to the tabbed pane. The layout manager of the panel will set the size of the components. The size will then be fixed based on the preferred size of the components you add to the panel. This way instead of you choosing some random size that doesn't make sense the size will be calculated automatically and will be adjusted if you ever need to change the panel and add more components. Layout managers make development and maintenance of programs easier. Learn to use them. – camickr Sep 30 '17 at 02:01
  • Yes I already went through. You clearly missing the point here. I got some JTextField, JLabels on the left hand side. and want to have JTable taking 50% of the other side. That is the reason why I would like to have my JPanel to have a fixed size, so therefore that my JTable could fill in the other side of my JPanel. Or at least have two JPanels, having two different set of layout managers. – IntFooBar Sep 30 '17 at 02:14
  • *"want to have JTable taking 50% of the other side"* A single row `GridLayout` with two cells, one being the table, the other being everything else, will achieve that. *"You clearly missing the point here."* No, that describes yourself, not @camickr. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders .. – Andrew Thompson Sep 30 '17 at 04:39
  • .. for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 30 '17 at 04:40

1 Answers1

1

Or at least have two JPanels,

Exactly.

Frist you create a main panel using a BorderLayout that you add to the tabbed pane.

Then you have a second panel for your labels and text fields (using whatever layout manager you want). Then you add this panel to the BorderLayout.LINE_START.

Then you add your scrollpane containing the JTable to the BorderLayout.CENTER of the main panel.

Read the tutorial on Layout Manager. Nest panels with different layout managers as required.

want to have JTable taking 50% of the other side.

Picking a random number like 50% is not the way to design a GUI. What happens if the frame is made smaller/larger. What happens to the space? Design the layout with flexibility in mind, just like your browser window is designed. There are always fixed areas where the size is determined by the components added and there is a flexible area that grows/shrinks as desired.

camickr
  • 321,443
  • 19
  • 166
  • 288