Search code examples
javaswingjframejpanelgridbaglayout

Elements incorrectly aligned with GridBagConstraints


I've currently created a JFrame with the code below. I'm looking to have three JLabels one above the other, their corresponding JTextFields to the right, and four JButtons below them (I will eventually use insets to space them apart). However, the cells are aligning at the top of the frame in one line. Can anyone spot the error I am making. Thanks.

public class UploadFrame extends JFrame {
private GridBagConstraints gbc = new GridBagConstraints();

/**
 * Creates a JFrame for the file uploading panel to sit in
 * @throws HeadlessException
 */
public UploadFrame(){
    this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setTitle("Download file");
    setSize(700, 300);
    setLocationRelativeTo(null);
    this.setContentPane(new UploadPanel());
    setVisible(true);
}
}

public class UploadPanel extends JPanel implements ActionListener {
private static final Logger LOGGER = Logger.getLogger(UploadPanel.class.getName());

private JButton sendButton;
private JButton menuButton;
private JButton filePathButton;
private JButton youTubeButton;
private JTextField filePathField;
private JTextField jobNameField;
private JTextField youTubeField;
private JLabel jobNameLabel;
private JLabel filePathLabel;
private JLabel youTubeLabel;
private GridBagConstraints gbc = new GridBagConstraints();
private ServiceUpload serviceUpload = new ServiceUpload();
private String filePath = "No file path selected";
private File mp4 = null;

public UploadPanel() {

    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets.bottom = 1;
    gbc.insets.top = 1;
    gbc.insets.right = 1;
    gbc.insets.left = 1;
    setJLabels();
    setJTextField();
    setButtons();
    setAction();

}

/**
 * Method sets and add the JLabels to the panel
 */

void setJLabels() {
    jobNameLabel = new JLabel("Job name:");
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(jobNameLabel, gbc);

    filePathLabel = new JLabel("File path:");
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(filePathLabel, gbc);

    youTubeLabel = new JLabel("YouTube http:");
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(youTubeLabel, gbc);
}

/**
 * Method sets and add the JTextFields to the panel
 */
void setJTextField() {

    filePathField = new JTextField();
    filePathField.setText(filePath);
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(filePathField, gbc);

    jobNameField = new JTextField();
    jobNameField.setText("Default");
    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(jobNameField, gbc);

    youTubeField = new JTextField();
    youTubeField.setText("Default");
    gbc.gridx = 2;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(youTubeField, gbc);
}

/**
 * Method sets and add the JButtons to the panel
 */
void setButtons() {
    sendButton = new JButton("Send");
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(sendButton, gbc);

    menuButton = new JButton("Menu");
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(menuButton, gbc);

    filePathButton = new JButton("Select file");
    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(filePathButton, gbc);

    youTubeButton = new JButton("YouTube Download");
    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(youTubeButton, gbc);
}

/**
 * Method creates the actionListeners
 */
void setAction() {
    sendButton.addActionListener(this);
    menuButton.addActionListener(this);
    filePathButton.addActionListener(this);
}

/**
 * Method sets the actions that will take place when buttons are pressed
 *
 * @param actionEvent
 */
@Override
public void actionPerformed(ActionEvent actionEvent) {
    try {
        if (actionEvent.getSource() == sendButton) {

            String jobName = jobNameField.getText();
            serviceUpload.convertToBase64AndSend(jobName, mp4);
        }

        if (actionEvent.getSource() == menuButton) {
            new MenuFrame();
        }

        if (actionEvent.getSource() == filePathButton) {
            filePathField.setText(chooseFile().getAbsolutePath());
        }

        if (actionEvent.getSource() == youTubeButton) {
            filePathField.setText(serviceUpload.httpPath());
        }
    } catch (Exception e) {
        LOGGER.info(e.toString());
    }
}

/**
 * This method creates the JFileChooser that allows the selection of the file being sent to 
AWS
 * @return File
 */
private File chooseFile() {
    JFileChooser chooser = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter(null,
            "MP4");

    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.showOpenDialog(null);

    return chooser.getSelectedFile();
}
}

The JFrame it is currently producing is:

enter image description here

**I've realised I left setting the GridBagLayout in the panel so added the line:

uploadPanel.setLayout(new GridBagLayout());

This has now produced the window:

enter image description here


Solution

  • I've realised I left setting the GridBagLayout in the panel so added the line:

    uploadPanel.setLayout(new GridBagLayout());
    

    Where did you do that?

    There is no variaible "uploadPanel" in your posted code.

    In fact there is no need to define a variable because the layout manager of the panel needs to be set BEORE you add the components to the panel.

    So the layout manager should be set in the constructor of your "UploadPanel" class:

    public UploadPanel() 
    {
        setLayout( new GridBagLayout() );
        gbc.weightx = 1;
    

    Also, the structure of your code is confusing.

    Related components should be added to the panel at the same time. That would be the label and text field pair should be added together to the panel.

    It does not make sense to add all the labels and then all the text fields in separate methods.

    Also, your code adds a component to row 2, then 0, then 0. Lets be logical and do 0, then 1, then 2 to make the code easier to understand and maintain.