Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a border around a specific JPanel in the JFrame?

I'm kinda bad at explaining but I'll do my best.

I've basically been trying to add a border around my JLabel, JTextField and JButton components within my JPanel, however the border is expanding according to size.

This is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoginPanel
{
    private JPanel loginPanel = new JPanel();
    private JFrame loginFrame = new JFrame();

    public LoginPanel()
    {
        loginPanel.setLayout(new GridBagLayout());

        GridBagConstraints gridBagConstraints = new GridBagConstraints();

        JTextField textLogin = new JTextField(10);
        JPasswordField password = new JPasswordField(10);

        JButton login = new JButton("Login");
        JButton register = new JButton("Register");

        gridBagConstraints.insets = new Insets(0,0,0,0);

        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridx = 0;

        loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));

        loginPanel.add(new JLabel("E-Mail"), gridBagConstraints);
        gridBagConstraints.gridy++;
        loginPanel.add(textLogin, gridBagConstraints);
        gridBagConstraints.gridy++;

        loginPanel.add(new JLabel("Password"), gridBagConstraints);
        gridBagConstraints.gridy++;
        loginPanel.add(password, gridBagConstraints);
        gridBagConstraints.gridy++;

        loginPanel.add(login, gridBagConstraints);
        gridBagConstraints.gridy++;
        loginPanel.add(register, gridBagConstraints);

        loginFrame.pack();
        loginFrame.add(loginPanel);
        loginFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        loginFrame.setLocationRelativeTo(null);
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.setVisible(true);
    }

    public static void main(String[] args)
    {
        try
        {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e)
        {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new LoginPanel();
            }
        });
    }
}

This is the outcome: Image 1: Outcome

This is what I want the border to be like: Image 2: What I want the look to be like.

Hope the images explain more, I'll respond and provide any more questions needed.

like image 745
Faisal Hoque Avatar asked Jan 05 '23 10:01

Faisal Hoque


1 Answers

A frame has a BorderLayout by default. A component added to a border layout with no constraint is put in the CENTER. The component in the center of of a border layout will be stretched to the full width and height of the available space.

It can be fixed by changing:

    loginFrame.pack();

To:

    loginFrame.setLayout(new GridBagLayout());
    loginFrame.pack();

Since a single component added to a GridBagLayout with no constraint will be centered.

Other tips:

The GUI does not appear here, like in your screenshot. Instead the components are closer together and the titled border is snug up against them.

The first can be fixed by changing:

    gridBagConstraints.insets = new Insets(0, 0, 0, 0);

To:

    gridBagConstraints.insets = new Insets(5, 5, 5, 5);

The 2nd can be fixed by making the border a compound border that combines an EmptyBorder with the TitledBorder.

    Border border = new CompoundBorder(
            new TitledBorder("Login"), 
            new EmptyBorder(40, 50, 40, 50));
    loginPanel.setBorder(border);
    //loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));
like image 111
Andrew Thompson Avatar answered Jan 25 '23 12:01

Andrew Thompson