Java component positioning on the top of the GridBagLayout

24 Views Asked by At

I have these components shown on the center vertically, I want to show them on the top of the panel, like it shown in the picture (the yellow arrow). NB: I tried to set the anchor on "NORTH" and fill to "BOTH" but nothing happened.

enter image description here

setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        Border border = BorderFactory.createTitledBorder(null, "Saisie", TitledBorder.CENTER, TitledBorder.TOP, new Font("times new roman",Font.BOLD,14),Color.GRAY);
        setBorder(border);
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.insets = new Insets(3, 3, 3, 3);
        add(JLabel_ID, gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(3, 3, 3, 3);
        add(JTextField_ID, gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.insets = new Insets(3, 3, 3, 3);
        add(JLabel_Nom, gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(3, 3, 3, 3);
        add(JTextField_Nom, gbc);

    }```


I tried to set the anchor on "NORTH" and fill to "BOTH" but nothing happened.
0

There are 0 best solutions below