Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How do I prevent BorderLayout EAST from hugging the side of the screen?

Tags:

java

swing

If I add components like JButtons on the East or West side, how do I prevent it from hugging the side of the screen? I want some space between the JButtons and the edge of the screen.

like image 660
ShrimpCrackers Avatar asked Feb 06 '10 09:02

ShrimpCrackers


People also ask

How many areas can you use if your layout is BorderLayout?

Class BorderLayout. A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center.

How do I set border layout?

Each region can contain only one component and is identified by a corresponding constant as NORTH, SOUTH, EAST, WEST, and CENTER. Constructors: BorderLayout(): It will construct a new borderlayout with no gaps between the components.

How do I set border layout in JPanel?

JPanel panel = new JPanel(); panel. SetLayout(new BorderLayout(100,100)); panel. add(pic1,BorderLayout. CENTER); panel.


2 Answers

call setBorder on your JButton like this:

setBorder( new EmptyBorder( 3, 3, 3, 3 ) )

From the JavaDoc, an EmptyBorder is a "transparent border which takes up space but does no drawing". In my example it shall take 3 pixels respectively top, left, bottom and right.

Complete code whose purpose is only to show how to use EmptyBorder:

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.EmptyBorder;

public class ALineBorder {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Line Borders");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button1 = new JButton("Button1");
        button1.setBorder( new EmptyBorder( 8, 8, 8, 8 ) );
        JButton button2 = new JButton("Button2");
        JButton button3 = new JButton("Button3");
        button3.setBorder( new EmptyBorder( 16, 16, 16, 16 ) );
        Container contentPane = frame.getContentPane();
        contentPane.add(button1, BorderLayout.WEST);
        contentPane.add(button2, BorderLayout.CENTER);
        contentPane.add(button3, BorderLayout.EAST);
        frame.pack();
        frame.setSize(300, frame.getHeight());
        frame.setVisible(true);
    }

}
like image 195
SyntaxT3rr0r Avatar answered Oct 22 '22 20:10

SyntaxT3rr0r


Most likely you have (or soon will have) more than one button in the container, and want to align them horizontally. So consider putting the buttons within in a nested JPanel with a GridBagLayout:

class ButtonPanel extends JPanel {
    ButtonPanel() {
        setLayout(new GridBagLayout());
    }

    @Override
    public Component add(Component button) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = nextGridY++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(3, 3, 3, 3);
        super.add(button, gbc);
        return button;
    }

    int nextGridY;
}

Then add this panel to the parent frame or panel (with a BorderLayout.)

like image 23
finnw Avatar answered Oct 22 '22 20:10

finnw