Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce Spaces in a GridLayout Java Swing

Tags:

java

swing

Good day, please would like to know how to reduce the spaces between the labels and the Textboxes in this picture below and also how to create some spaces between the labels and the frame.Thank you.

login page

My code for this:

private void initUI(JFrame parent) {
    // private void initUI() {
   myPanel = new JPanel(new GridLayout(3,2,1,1));
   buttons_panel = new JPanel(new FlowLayout());

   username_label = new JLabel("Username: ");
   password_label = new JLabel("Password: ");
   username = new JTextField(20);
   password = new JPasswordField(20);

   ok = new JButton("Ok");
   ok.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

        }
    });

    cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
           dispose();
        }
    });

    myPanel.add(username_label);
    myPanel.add(username);
    myPanel.add(password_label);
    myPanel.add(password);

    buttons_panel.add(ok);
    buttons_panel.add(cancel);

    getContentPane().add(myPanel, BorderLayout.CENTER);
    getContentPane().add(buttons_panel, BorderLayout.PAGE_END);

    pack();
    setResizable(false);
    setLocationRelativeTo(parent);
}

Should i be using GridBagLayout for this instead?..

like image 298
irobotxx Avatar asked Jul 27 '26 10:07

irobotxx


1 Answers

All cells in GridLayout have equal size. You have to use GridBagLayout or SpringLayout or BoxLayout.

like image 173
KV Prajapati Avatar answered Jul 28 '26 23:07

KV Prajapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!