Does not work GridBagConstraints.NORTH in Configurable

Answered

Does not work GridBagConstraints.NORTH in Configurable. Panel elements are always centered vertically

@Override
public @Nullable JComponent createComponent() {
    rootPanel = new JPanel(new GridBagLayout());
    Color color = new Color(55, 85, 134);
    rootPanel.setBackground(color);

    GridBagConstraints gc = new GridBagConstraints();
    gc.anchor = GridBagConstraints.NORTH; 
    gc.fill = GridBagConstraints.HORIZONTAL;
    gc.gridx = 0;
    rootPanel.add(new JTextField(), gc);
    rootPanel.add(new JTextField(),gc);
    return rootPanel;
}
0
1 comment

Solution

@Override
public @Nullable JComponent createComponent() {
    rootPanel = new JPanel(new BorderLayout());
    JPanel mainPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    gc.anchor = GridBagConstraints.NORTH; 
    gc.fill = GridBagConstraints.HORIZONTAL;
    mainPanel.add(new JTextField(), gc);
    mainPanel.add(new JTextField(),gc);
    rootPanel.add(mainPanel, BorderLayout.NORTH);
    return rootPanel;
}
0

Please sign in to leave a comment.