JFrame doesn't show the Jpanel's right - java

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)

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.

Make buttons split JPanel - Java Swing

I've got a BorderLayout going on, and am working on the North panel. Inside the North panel, I'd like to have 3 components: a picture that is on the left, and two buttons that split the remaining width of the Frame. Right now I'm attempting to accomplish this with another BorderLayout.
The Frame is resizable.
The picture is assigned to BorderLayout.WEST, and with the following code I attempt to add another panel that contains only buttons. The panel is then added to the CENTER of the Frame's NORTH layout component.
//create panel to hold buttons
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BorderLayout());
JButton btnMatrix = new JButton("Matrix View");
btnPanel.add(btnMatrix);
JButton btnList = new JButton("List View");
btnPanel.add(btnList);
add(btnPanel);
however, the buttons both try to take up the entire panel. If I leave it to a flow layout (I don't use btnPanel.setLayout(new BorderLayout()); in the above code), the buttons sit nicely in the center, but do not expand and share the btnPanel.
Thoughts? I'm new enough to Java that I could be going about this the wrong way from the start.
btnPanel.setLayout(new BorderLayout());
You didn't specify a constraint when you added the buttons to the panel. So both buttons are added to the CENTER. However, only one component can be added to the CENTER, so only the last one added is displayed.
You can try a different layout:
btnPanel.setLayout( new GridLayout(0, 2));
Then each button will be the same size and both buttons will fill the space available.

set layout on top left of the JPanel instead of center

Say I have a GridLayout in a JPanel, if my layout isn't big enought to fill the whole JPanel, it will auto align it at the center of ther JPanel like:
But I want my layout to align it on top left of the JPanel like:
so my component will start on top left of the panel, is there any method to do it?
Add your gridLayoutPanel to another panel i.e. subPanel. Set this subPanel layout to the flow layout like subPanel.setLayout(new FlowLayout(FlowLayout.LEFT)). Than set your main panel's layout to BorderLayout. And add your subPanel panel to the NORTH position of the main panel.
Example code:
JPanel subPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
subPanel.add(gridLayoutPanel);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(subPanel, BorderLayout.NORTH);
it will auto align it at the center of ther JPanel like:
Sounds like you are using a GridBagLayout on the main panel and this is the default behaviour unless you use the GridBagConstraints to control this behaviour.
Read the section from the Swing tutorial on How to Use GridBagLayout, especially the section on using the weightx/y constraints. Setting these constraints correctly will prevent the panel from centering itself.

Using GridLayout - how to set JPanel size?

I'm using GridLayout and all my panels have the same size, how can I change their size?
I tried all functions getPreferred/Minimum/MaximumSize which makes that, and nothing.
I still want to stay with GridLayout
Any suggestions?
By its very nature, all components held by a GridLayout will have the same size. If you want them to be different sizes,
You can use a different layout, such as a GridBagLayout or MigLayout or
Use the GridLayout to hold same-sized JPanels that act as containers for other components. The inner components of course can be different sizes. For example: a chess board that holds its chess cells in a GridLayout, but that allows each cell to hold a chess piece that has varying sizes.
If this doesn't answer your question, then please clarify your question.
So I managed to split all the part above to my first JPanel which located to NORTH.
Then, the JPanel with the button I did the same to be located SOUTH.
So the scrolled JLabel in now located in the CENTER which allows him to be flexible.
JPanel mainPanel1 = new JPanel(new GridLayout(6,1));
mainPanel1.add(titleLabel);
mainPanel1.add(participantPanel);
mainPanel1.add(swimPanel);
mainPanel1.add(ridePanel);
mainPanel1.add(runPanel);
mainPanel1.add(categoriesPanel);
JPanel mainPanel2 = new JPanel(new GridLayout(1,1));
mainPanel2.add(listPanel);
JPanel mainPanel3 = new JPanel(new GridLayout(1,1));
mainPanel3.add(buttonsPanel);
this.getContentPane().add(mainPanel1, BorderLayout.NORTH);
this.getContentPane().add(mainPanel2, BorderLayout.CENTER);
this.getContentPane().add(mainPanel3, BorderLayout.SOUTH);

Categories