Put a JButton in the center of the screen with BorderLayout - java

How to add a JButton into the center of a JFrame with BorderLayout()? I tried using BorderLayout.CENTER, but instead of the center of the screen, it gave the top-center of the screen. Or do I have to use another layout manager?

Put a JPanel in the CENTER and set the layout to GridBagLayout or BoxLayout as seen in this answer to Set component at center of the page.
The GridBagLayout is used to center a label containing the yellow/red gradient image seen in the Nested Layout Example.

It may take some time to learn, but SpringLayout is worth looking into. It will let you position elements on the GUI where you wish. You can look here for examples of different layouts.

try this
frame.getContentPane().setLayout(new BorderLayout(0, 0));
JButton btnNewButton = new JButton("New button");
frame.getContentPane().add(btnNewButton, BorderLayout.CENTER);

Related

How to instance a JPanel into panel in Jframe

Sorry if I might mix terms up. So I have a basic application where I would press the button and Jpanel1 with a label in it, would then switch/replace to JPanel2 that'll have a picture in it, all within the panel.
The JPanel inside the box would change from Jpanel1 to JPanel 2 after pressing the button. So is their a way to instance a JPanel in a panel or JFrame? I can't find the method on how to fill the panel with the JPanel.
frame.getContentPane().add(panel, /*layout.location*/);
You mean add
The exact way to do this depends on the LayoutManager you use. I suggest checking out the Visual Guide to LayoutManagers. For example, if you use a BorderLayout, you can add a panel to the center and then replace it with a different panel when the user clicks a button.
I ended using layeredPane with a card layout. Then I placed a panel using the center Border Layout, then instanced the JPanel as the panel from a different class.
Picture of Windowbuilder layout
panelPPBrowser = new JPanel();
layeredPane.add(panelPPBrowser, "name_216881785358769");
panelPPBrowser.setLayout(new BorderLayout(0, 0));
panelBrowser panelBrowserCon = new panelBrowser();
panelPPBrowser.add(panelBrowserCon, BorderLayout.CENTER);

How can I change the size of a Panel in Java?

I'm trying to create a simple login screen consisting of different panels. I want help in resizing panels. The Panel I want resized is coloured in Green. I want to make it a bit smaller. The Panel in Green is the North Panel and is set to Border Layout. I want to make the green panel smaller since i feel its too big
I tried northPanel.setSize(150,150); but I got no result
This is my code:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
You are using a BorderLayout which has five positions to put components in the layout. The five positions are PAGE_START, PAGE_END, LINE_START, LINE_END, and CENTER. Here is a diagram of these positions:
For this layout, you want to put the component that should take the remaining space of the frame in the CENTER position. For this reason, the northPanel is probably better suited in the PAGE_START position while the JPanel that houses the login labels and submit button should be in the CENTER position. Using this positioning will allow you to resize the northPanel and allow the panel housing the login labels and submit button to take up the remaining frame space.
I want to make the green panel smaller
Looks to me like the two panels are the same size which tells me you are using a GridLayout for your frame.
Don't use a GridLayout, instead keep the default BorderLayout of the frame.
Then your code would be something like:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
frame.add(northPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);
Now the green panel will only be as big as the JLabel. If you want the panel to be bigger, then add an EmptyBorder to the northPanel. Read the section from the Swing tutorial on How to Use Borders for more information.

JFrame doesn't show the Jpanel's right

I'm using a JFrame with the size of 800x600.
what i'm trying to do is make this:
the black Panel has 2 other panels inside of him with the size of 300x300 each.
the result is that the black panel is to the left (as suposed) and the red panel in in the centre with a gap on top between the frame and the panel. also, if i remove the black panel the right panel is filling the whole frame...
this is the code:
//create the left part of the screen
JPanel leftPanels = new JPanel();
leftPanels.setLayout(new GridLayout(2,1));
leftPanels.setSize(new Dimension(300,600));
// just to illustrate the 2 panels inside of the black panel.
//leftPanels.add(new JPanel());
//leftPanels.add(new JPanel());
//create the right part
JPanel rightPanel = new JPanel();
rightPanel.setSize(new Dimension(500,600));
rightPanel.setBackground(Color.red);
this.add(leftPanels);
this.add(rightPanel);
this.validate();
this.repaint();
is there an easy way to fix this?
I also tried a Gridlayout on the JFrame but that gives me 2 panels of 400X600 each
First, use FlowLayout like this
setLayout(new FlowLayout(FlowLayout.LEFT));
Secondly, kindly use setPreferedSize rather than setSize for the JPanels
leftPanels.setPreferredSize(new Dimension(300,600));
I don't know what is cashRegister, but it looks like you are not adding the rightPanel to JFrame so make sure you add it.
Try to set the layout of the frame to null. Then use setBounds to position the panel.
If you are trying to set the panel relatively one from another set the frame layout to null
this.getContentPane().setLayout(null);
Then you will be able to place them absolutely.
For more info : Doing Without a Layout Manager (Absolute Positioning)

Leave small space between buttons when using GridLayout

I am trying to create a toolbar that will go at the top of all the pages in my java swing application.
I am creating a JPanel with a series of individual JPanels (containers) inside it. Each JPanel (container) has a north and south component or just a north component, set up using a GridLayout.
My problem is, I want a small gap between the north and south component, but I can't see how to do it and have not been able to find any help on the internet.
Below is a working example for the code for one of the containers:
public static void CustomersGui(){
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container1 = new JPanel();
container1.setLayout(new GridLayout(2,1));
JButton buttonDiary = new JButton("Diary");
buttonDiary.setPreferredSize(new Dimension(140, 25));
JButton buttonCars = new JButton("Cars");
buttonCars.setPreferredSize(new Dimension(140, 25));
container1.add(buttonDiary, BorderLayout.NORTH);
container1.add(buttonCars, BorderLayout.SOUTH);
frame.setContentPane(container1);
frame.pack();
frame.setVisible(true);
}
I am trying to create a toolbar that will go at the top of all the pages in my java swing application.
Why don't just use JToolBar? See How to use Tool Bars
I am creating a JPanel with a series of individual JPanels (containers) inside it. Each JPanel (container) has a north and south
component or just a north component, set up using a GridBagLayout.
Based on your code none of these statements is true: you are adding buttons (not panels) and you are using GridLayout not GridBagLayout:
JPanel container1 = new JPanel();
container1.setLayout(new GridLayout(2,1)); // GridLayout
JButton buttonDiary = new JButton("Diary"); // button here
buttonDiary.setPreferredSize(new Dimension(140, 25));
JButton buttonCars = new JButton("Cars"); // another button here
Besides you are using BorderLayout constraints that will be totally ignored by either GridLayout or GridBagLayout:
container1.add(buttonDiary, BorderLayout.NORTH);
container1.add(buttonCars, BorderLayout.SOUTH);
You should have a look to the whole Laying Out Components Within a Container lesson to learn about layout managers and how do all of them work.
In addition
As wisely pointed out by #nIcEcOw, since Java 1.4 BorderLayout the use of new constants is highly encouraged:
PAGE_START
PAGE_END
LINE_START
LINE_END
CENTER
From How to Use BorderLayout tutorial (bold text mine):
Before JDK release 1.4, the preferred names for the various areas were
different, ranging from points of the compass (for example,
BorderLayout.NORTH for the top area) to wordier versions of the
constants we use in our examples. The constants our examples use are
preferred because they are standard and enable programs to adjust to
languages that have different orientations.
Update
Well now that your question has been edited to say that you actually use GridLayout it's easy to answer by saying that you can specify horizontal and vertical gaps between components either by using class' constructor: or setHgap() and setVgap() methods:
JPanel container1 = new JPanel(new GridLayout(2, 1, 8, 8));
// Or
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(2);
gridLayout.setColumns(1);
gridLayout.setHgap(8);
gridLayout.setVgap(8);
JPanel container1 = new JPanel(gridLayout);
Don't forget to remove BorderLayout constraints when you add buttons to container1 panel, because those will be ignored:
container1.add(buttonDiary);
container1.add(buttonCars);
You might want to take a look to this topic as well: Providing white space in a Swing GUI

Why is my panel not positioned correctly even after setting the boundaries?

I'm trying to make a simple GUI with radio buttons and I grouped them into one panel. I wanted it positioned on the leftmost side so I used the setBounds method. Whatever numbers I put on the parameters, the panel won't move. Are panels not affected by the setBounds method? Or is there another way to position my panel. Here's the snippet of my code:
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3,1));
JRadioButton Rbutton1 = new JRadioButton("Credit Card");
JRadioButton Rbutton2 = new JRadioButton("E-Funds");
JRadioButton Rbutton3 = new JRadioButton("Check");
Rbutton3.setSelected(true);
ButtonGroup Bgroup = new ButtonGroup();
Bgroup.add(Rbutton1);
Bgroup.add(Rbutton2);
Bgroup.add(Rbutton3);
radioPanel.add(Rbutton1);
radioPanel.add(Rbutton2);
radioPanel.add(Rbutton3);
radioPanel.setBounds(10,50,50,40); //this is where I'm trying to position the panel with the radio buttons
paymentPanel.add(radioPanel);
contentPane.add(paymentPanel); //contentPane is the frame
contentPane.setVisible(true);
Set layout for the frame. For example:
contentPane.setLayout(new BorderLayout());
contentPane.add(paymentPanel, BorderLayout.LINE_START);
More info about layout managers you can find here: A Visual Guide to Layout Managers
You should read about Layout Managers which will do this for you. And I would suggest using a GUI Builder Tool, but that might not be allowed for your homework.
You can set layout as null layout for your contentPane.
contentPane.setLayout(null);
Then your setBounds() will work exactly as you designed.
Note:
Creating containers with absolutely positioned containers can cause problems if the window containing the container is resized.

Categories