JPanel inside a JPanel - java

I have a problem because I want to put a small JPanel inside a different JPanel, but I can't get the small JPanel to show.
What am I missing?
this.setLayout(new BorderLayout(5,5));
this.cardsPanel= new JPanel();
this.cardsPanel.setBackground(Color.DARK_GRAY);
this.cardsPanel.setLayout(new FlowLayout (FlowLayout.CENTER,3,10));
this.cardsPanel2= new JPanel();
this.cardsPanel2.setBackground(Color.DARK_GRAY);
this.cardsPanel2.setLayout(new FlowLayout (FlowLayout.CENTER,3,10));
this.tablePanel=new JPanel();
this.tablePanel.setBackground(Color.PINK);
this.tablePanel.setLayout(new FlowLayout (FlowLayout.CENTER,5,5));
this.tablePanel1=new JPanel();
this.tablePanel1.setBackground(Color.ORANGE);
this.tablePanel1.setPreferredSize(new Dimension(100,100));
// this.tablePanel1.setLayout(null);
this.tablePanel1.add(tablePanel);
this.add(cardsPanel, BorderLayout.SOUTH);
this.add(cardsPanel2, BorderLayout.NORTH);
this.add(tablePanel,BorderLayout.CENTER);

Using that code:
Neither cardsPanel1 nor cardsPanel2 (dark gray) have any components (or therefore) any size. At 0x0 pixels size, they are invisible even when added to another container (unless the layout stretches them).
tablePanel (pink) should be visible so long as this is assigned enough space in whatever layout it is added to.
tablePanel1 (orange) should not be visible as it is never added to anything.
But as mentioned, for better help sooner, post an MCVE (Minimal Complete and Verifiable Example).

this.validate(); your root panel after adding new content.

Related

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.

JPanel button is not at the correct place

i made a custom JFrame for the desktop application and i added a JPanel on the very top of the app to serve as a subtitute of the title box. the problem is when i added a button it located right in the middle of the JPanel instead of the usual left top. AND it would not move even if i set it at a different location.
here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
btn.setBackground(Color.white);
btn.setText("Button");
btn.setSize(300, 80);
btn.setLocation(50, 0);
pa.setBackground(Color.red);
pa.setPreferredSize(new Dimension(width,60));
pa.add(btn);
f.setBackground(Color.white);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(width,height));
f.setLocation(200, 200);
f.setVisible(true);
You use a BorderLayout in the frame. You can do the same thing in the panel.
pa.setLayout(new BorderLayout());
pa.add(btn, BorderLayout.WEST);
In general, setLocation tends to fight against the layout manager, so you usually don't want to use it unless you're going to position everything by hand.
that is one way to do it, but BorderLayout way is not very good way because i also want to add another button next to it.
Then what this might need is a FlowLayout using FlowLayout.LEADING as the alignment.
But as general tips:
Provide ASCII art or a simple drawing of the intended layout of the GUI (showing all components) at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
For better help sooner, post a
Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example of your attempt.

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)

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);

Java GUI layout problems

I'm writing a small Java GUI program, and I'm having some issues with Java not laying things out properly. I haven't done much Java GUI code lately, so I'm having trouble seeing where the problem lies.
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(500, 270));
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(500,50));
JButton toggleButton = new JButton("Toggle");
final JTextField minRange = new JTextField("10");
final JTextField maxRange = new JTextField("1000");
JButton setLimits = new JButton("Set Limits");
buttonPanel.add(toggleButton, BorderLayout.NORTH);
buttonPanel.add(minRange, BorderLayout.SOUTH);
buttonPanel.add(maxRange, BorderLayout.SOUTH);
buttonPanel.add(setLimits);
JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, chartPanel, buttonPanel);
jsp.setDividerLocation(0.8);
setContentPane(jsp);
What's happening here is that all of the layout options are completely being ignored. The GUI components are showing up properly, and the divider specifically is ignoring the preferred size of JFreeChart, and squeezing it to about 5% of space at the top of the frame.
In addition to problems with the splitpane not respecting your desired sizes, you are using BorderLayout constants but you haven't specified the layout for the panel (the default is FlowLayout).
This:
JPanel buttonPanel = new JPanel();
Should be:
JPanel buttonPanel = new JPanel(new BorderLayout());
I believe that using a float proportion on JSplitPane only works once the split pane is "realized", otherwise you're getting a proportion of zero because it doesn't know how big its going to be.
also:
buttonPanel.add(minRange, BorderLayout.SOUTH);
buttonPanel.add(maxRange, BorderLayout.SOUTH);
BorderLayout only allows one component to be in each area, so min range will never appear, as maxRange is now "the" south component. if you want both you'll need to put those 2 components in another panel, then add that panel to the south.
Try setting the minimum size too.
See: Java GUI Problems
JSplitPane pays attention to the minimum size, not the preferred size. Try simply changing setPreferredSize to setMinumumSize.
Dan Dyer is correct, you didn't set the Layout.
You could also set it by buttonPanel.setLayout(new BorderLayout())
And John Gardner is correct that you set a component to BorderLayout.SOUTH twice.
Also check out MigLayout if you don't already know about it. Its the least "surprising" layout manager I've ever used. It just works. It takes some learning, but very straight forward once you get over the syntax.
And I would avoid SplitPane if you can...its very finicky
Never call setPreferredSize() - it should be a calculation.
For example, your ButtonPanel is being set to a fixed preferred size.
What if you add I18N support and the user is using a language with very long localizations? What if the user resizes the frame?
Check out my article on layout managers for details on how you should really use them. It's from 1999 but still applies:
http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/
Enjoy!

Categories