0

I have a problem with getting the GridBagLayout work as expected.

The gridy of the GridBagConstraints didn't seem to function as expected at all for atomIndexLabel (JLabel), atomIndexExample (JLabel) and atomIndexAddField (JTextField). I want to align them to approximately the middle of the table, but when running, they stick to the top, just like gridy is set to 0, 1 and 2.

I tried to add an emptyElement, but it didn't work. My code is below. Any suggestions?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

public class Test extends JPanel {
    private static final long serialVersionUID = -7810531559762481489L;

    private JLabel atomIndexLabel, atomIndexExample, atomIndexNotification;
    private JTextField atomIndexAddField;
    private JButton atomIndexAddButton, atomIndexRemoveButton;
    private AtomIndexButtonListener atomIndexButtonListener;
    private JTable atomIndexTable;
    private JScrollPane atomIndexTableScrollPane;
    private DefaultTableModel atomIndexTableModel;

    public Test() {

        this.atomIndexLabel = new JLabel("Input one or multiple atom indexs:");
        this.atomIndexExample = new JLabel("Example: 1 2 3 4 5");
        this.atomIndexAddField = new JTextField(20);
        this.atomIndexAddButton = new JButton("Add Atom Index");

        this.atomIndexTable = new JTable(1, 1);
        this.atomIndexTableModel = new DefaultTableModel() {
            private static final long serialVersionUID = -6458638313466319330L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        this.atomIndexTable.setModel(atomIndexTableModel);
        this.atomIndexTableModel.addColumn("Atom_Index");
        this.atomIndexTableScrollPane = new JScrollPane(atomIndexTable);
        this.atomIndexTableScrollPane.setPreferredSize(new Dimension(100, 170));

        this.atomIndexRemoveButton = new JButton("Remove Selected");
        this.atomIndexNotification = new JLabel("No input atom index");
        this.atomIndexNotification.setHorizontalAlignment(JLabel.CENTER);

        setupLayout();

        this.atomIndexButtonListener = new AtomIndexButtonListener();
        this.atomIndexAddButton.addActionListener(atomIndexButtonListener);
        this.atomIndexRemoveButton.addActionListener(atomIndexButtonListener);

        this.setBorder(new TitledBorder("Atom Index Input"));

    }

    private void setupLayout() {
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        /*JComponent emptyElement = new JLabel("");
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 6;
        gbc.anchor = GridBagConstraints.LAST_LINE_START;
        this.add(emptyElement, gbc);*/

        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        this.add(atomIndexLabel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.gridwidth = 2;
        gbc.gridheight = 1;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        this.add(atomIndexExample, gbc);

        gbc.gridx = 0;
        gbc.gridy = 8;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        this.add(atomIndexAddField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 11;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(10, 0, 0, 0);
        this.add(atomIndexAddButton, gbc);

        gbc.gridx = 5;
        gbc.gridy = 0;
        gbc.gridwidth = 6;
        gbc.gridheight = 12;
        gbc.insets = new Insets(10, 10, 0, 0);
        this.add(atomIndexTableScrollPane, gbc);

        gbc.gridx = 5;
        gbc.gridy = 12;
        gbc.gridwidth = 6;
        gbc.gridheight = 1;
        gbc.insets = new Insets(10, 10, 0, 0);
        this.add(atomIndexRemoveButton, gbc);

        gbc.gridx = 0;
        gbc.gridy = 13;
        gbc.gridwidth = 10;
        gbc.gridheight = 1;
        gbc.insets = new Insets(20, 0, 0, 0);
        this.add(atomIndexNotification, gbc);


    }

    private class AtomIndexButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("For Test only");

        }
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new Test());
        frame.setSize(1000, 900);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.setVisible(true);
    }

}
Whymarrh
  • 13,139
  • 14
  • 57
  • 108
  • [Take a look at this](http://stackoverflow.com/questions/21010799/looking-for-general-method-for-gridbaglayout-component-creation/21031737#21031737) Specifically, look for @Splungebob's Answer. My point is that GridBagLayout doesn't have to be a nightmare. I'm using Splungebob's code gratefully today. It doesn't make GridBagLayout simple, but it does simplify it. There's a small learning curve, well worth the effort. – DSlomer64 May 15 '14 at 00:28
  • Somewhere about here, I would divide your UI into groups and focus on their individual requirements, placing them into their own panels with their own layouts and then build it back up into the final layout – MadProgrammer May 15 '14 at 00:48
  • Thanks DSlomer64 for your link and MadProgrammer for the suggestion. I finally figured out an easy way, though not sure if this is safe: add some html tags. For example: new JLabel("


    Input one or multiple atom indexs:");
    – user3638653 May 15 '14 at 03:04

0 Answers0