Search code examples
javaswingjframejpanelgridbaglayout

Why is GridBagConstraints gathering the objects in the centre of the JPanel?


In the following code I am creating a JFrame with a JMenuBar and JPanel, the later having three JTextFields and three JLabels. However, all the components of the JPanel are gathering in the center. I've set the layout before adding components so I don't think its this issue (which I've had before). If anyone has anybody ideas how to sort this out I would be much obliged.

public class InterfaceWindow {
private static final Logger LOGGER = Logger.getLogger(DownloadWindow.class.getName());

private JButton helpJButton;
private JButton copyToClipBoardJButton;
private JButton enterJButton;
private JButton selectFileJButton;
private JLabel pathJLabel;
private JLabel jobNameJLabel;
private JLabel emailJLabel;
private JTextField pathJTextField;
private JTextField jobNameJTextField;
private JTextField emailJTextField;
private JTextArea transcriptionJTextArea;
private JMenu accountJMenu;
private JMenu jobsJMenu;
private JMenu helpJMenu;
private JMenuBar toolBarJMenuBar = new JMenuBar();
private JMenuItem uploadYouTubeJMenuItem;
private JMenuItem uploadFileJMenuItem;
private JMenuItem downloadJMenuItem;
private JMenuItem helpJMenuItem;
private GridBagConstraints panelGbc = new GridBagConstraints();
private GridBagConstraints frameGbc = new GridBagConstraints();
private JPanel interfaceJPanel = new JPanel();
private String emptyField = "default";
private String filePath = "default";

public JButton getHelpJButton() {
    return helpJButton;
}

public JButton getCopyToClipBoardJButton() {
    return copyToClipBoardJButton;
}

public JButton getEnterJButton() {
    return enterJButton;
}

public JButton getSelectFileJButton() {
    return selectFileJButton;
}

public JFrame setInterfaceWindow() {
    JFrame interfaceJFrame = new JFrame();
    frameGbc.fill = GridBagConstraints.HORIZONTAL;
    interfaceJPanel = setInterfaceJPanel();

    interfaceJFrame.getContentPane().setLayout(new GridBagLayout());
    interfaceJFrame.setLayout(new GridBagLayout());
    interfaceJFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    interfaceJFrame.setTitle("Audio Transcribe");
    interfaceJFrame.setSize(800, 800);
    interfaceJFrame.setLocationRelativeTo(null);
    interfaceJFrame.setJMenuBar(setToolBar(toolBarJMenuBar));
    interfaceJFrame.add(interfaceJPanel, frameGbc);
    interfaceJFrame.setVisible(true);

    return interfaceJFrame;
}

private JPanel setInterfaceJPanel() {
    panelGbc.fill = GridBagConstraints.HORIZONTAL;
    panelGbc.insets.bottom = 1;
    panelGbc.insets.top = 2;
    panelGbc.insets.right = 1;
    panelGbc.insets.left = 1;
    panelGbc.weightx = 1;
    panelGbc.weighty = 1;

    interfaceJPanel.setLayout(new GridBagLayout());
    interfaceJPanel.setSize(800, 700);
    setJobName(interfaceJPanel);
    setPath(interfaceJPanel);
    setEmail(interfaceJPanel);
    setButtons(interfaceJPanel);
    setTextArea(interfaceJPanel);
    setAction();

    return interfaceJPanel;
}

public JMenuBar setToolBar(JMenuBar toolBarJMenuBar) {
    accountJMenu = new JMenu("Accounts");
    jobsJMenu = new JMenu("Jobs");
    helpJMenu = new JMenu("Help");

    toolBarJMenuBar.add(accountJMenu);
    toolBarJMenuBar.add(jobsJMenu);
    toolBarJMenuBar.add(helpJMenu);

    downloadJMenuItem = new JMenuItem("Download");
    uploadFileJMenuItem = new JMenuItem("Upload file");
    uploadYouTubeJMenuItem = new JMenuItem("Upload from YouTube");
    helpJMenuItem = new JMenuItem("Help");

    jobsJMenu.add(downloadJMenuItem);
    jobsJMenu.addSeparator();
    jobsJMenu.add(uploadFileJMenuItem);
    jobsJMenu.addSeparator();
    jobsJMenu.add(uploadYouTubeJMenuItem);
    helpJMenu.add(helpJMenuItem);

    return toolBarJMenuBar;
}

private void setEmail(JPanel interfaceJPanel) {
    JLabel emailLabel = new JLabel("Email:");

    panelGbc.gridx = 1;
    panelGbc.gridy = 3;
    panelGbc.gridwidth = 1;
    panelGbc.gridheight = 1;
    interfaceJPanel.add(emailLabel, panelGbc);

    emailJTextField = new JTextField();

    emailJTextField.setText(emptyField);
    panelGbc.gridx = 2;
    panelGbc.gridy = 3;
    panelGbc.gridwidth = 3;
    panelGbc.gridheight = 1;
    interfaceJPanel.add(emailJTextField, panelGbc);
}

private void setPath(JPanel interfaceJPanel) {
    JLabel filePathLabel = new JLabel("File path:");

    panelGbc.gridx = 1;
    panelGbc.gridy = 0;
    panelGbc.gridwidth = 1;
    panelGbc.gridheight = 1;
    interfaceJPanel.add(filePathLabel, panelGbc);

    pathJTextField = new JTextField();
    pathJTextField.setText(filePath);
    panelGbc.gridx = 2;
    panelGbc.gridy = 0;
    panelGbc.gridwidth = 2;
    panelGbc.gridheight = 1;
    interfaceJPanel.add(pathJTextField, panelGbc);
}

private void setJobName(JPanel interfaceJPanel) {
    JLabel jobNameLabel = new JLabel("Job name:");

    panelGbc.gridx = 1;
    panelGbc.gridy = 2;
    panelGbc.gridwidth = 1;
    panelGbc.gridheight = 1;
    interfaceJPanel.add(jobNameLabel, panelGbc);

    jobNameJTextField = new JTextField();
    jobNameJTextField.setText(emptyField);
    panelGbc.gridx = 2;
    panelGbc.gridy = 2;
    panelGbc.gridwidth = 2;
    panelGbc.gridheight = 1;
    interfaceJPanel.add(jobNameJTextField, panelGbc);
}

private void setAction() {
}

private void setTextArea(JPanel interfaceJPanel) {
}

private void setButtons(JPanel interfaceJPanel) {
}
}

enter image description here


Solution

  • It's because you're nesting two JPanels with GridBagLayout, and when adding the inner JPanel to the outer panel your frameGbc constraint does not have weightx or weighty set, centering the inner JPanel. The inner JPanel will size to its preferredSize, meaning, size as small as allowed, and will be centered.

    Best not to do this. Instead let the JFrame continue to use BorderLayout, and add the GridBagLayout using JPanel BorderLayout.CENTER, or don't nest JPanels unnecessarily and just give the contentPane a GridBagLayout.