Resizing the panels on gridlayout - java

I'm trying to make a simple ui game with Java swing. Here is my target layout design: (all panels have same width, excuse my drawing)
I need 3 small height panel and 1 large panel
I used GridLayout with 4x1. I added some buttons to first panel.
mainFrame = new JFrame("Basket Game");
mainFrame.setLayout(new GridLayout(4, 1));
options = new JPanel();
options.setLayout(new FlowLayout());
options.setBorder( new TitledBorder("Options Menu") );
options.add(settings);
options.add(start);
options.add(pause);
options.add(reset);
options.add(exit);
mainFrame.add(options);
But it makes the first panel too big.
How can I set a size for these panels or should I use different layout pattern.

With a GridLayout, all cells in the grid have the same size, that's why your panel has 1/4 of the total height.
You may consider using a vertical BoxLayout:
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
// add the panels to mainPanel, then
mainFrame.setContentPane(mainPanel);
Here is an example, with three panels containing one button each, and one panel having a bigger size :
JFrame frame = new JFrame();
JPanel p1 = new JPanel();
p1.add(new JButton("11111"));
JPanel p2 = new JPanel();
p2.add(new JButton("222222"));
JPanel p3 = new JPanel();
p3.add(new JButton("3333"));
JPanel p4 = new JPanel();
p4.setPreferredSize(new Dimension(50, 400));
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(p1);
mainPanel.add(p2);
mainPanel.add(p3);
mainPanel.add(p4);
frame.setContentPane(mainPanel);
frame.pack();
frame.setVisible(true);

Related

JPanel on top of another JPanel

I have been using JPanels for a while and now want to place a JPanel on top of another JPanel.
I have looked at using JLayer but I was wondering If there is a solution to just set the layer of the bottom and top, I don't want to set each components layer if possible.
Example
JPanel bottomPanel = new JPanel(); # Set as bottom panel
JPanel topPanel = new JPanel(); # Set as top panel
JPanel sidePanel = new JPanel(); # Don't have to set
JPanel anotherSidePanel = new JPanel(); # Don't have to set
If this isn't possible what is the best solution for this, Thanks.
You can have the main panel use a BorderLayout.
Then you can do something like:
mainPanel.add(leftSide, BorderLayout.LINE_START);
mainPanel.add(rightSide, BorderLayout.LINE_END);
JLayeredPane lp = new JLayeredPane();
mainPanel.add(lp, BorderLayout.CENTER);
It sounds like what you want is a layout manager. There are a few different ones that suit different needs. There's a link at the bottom of this post.
My personal favorite is GridLayout. So for what you want to do, you can do this:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
//the first number is the number of rows, the second is the number of columns
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
panel.add(topPanel);
panel.add(bottomPanel);
That will do what you want.
If you wanted to read more about them, here's a link:
Oracle Docs on Layout Managers
I know this is quite late, but if anyone now has this issue, I suggest using a BoxLayout. BorderLayout can only have one cell in each of its five locations, and GridLayout's cells are all the same dimension. If you want to stack different sized JPanels, here's how BoxLayout can be implemented:
JFrame frame = new JFrame("Intro to BoxLayout");
JPanel container = new JPanel();
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(X1, Y1));
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(X2, Y2));
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(panel1);
container.add(panel2);
frame.add(container);
frame.pack();
frame.setVisible(true);
where X1, Y1, X2, Y2 are arbitrary panel dimensions.

How can I center and widen out my JPanel in my JFrame?

I am making a copy of the apple calendar application, and I am having trouble aligning the month name and year name with the center of my screen, while aligning the left and right buttons with the left and right sides of the screen. Here is my code:
final JPanel months = new JPanel();
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
months.add(back, BorderLayout.WEST); //back is a JButton
JLabel monthName = new JLabel(this.monthNames[this.month]+" ", SwingConstants.CENTER);
JLabel year = new JLabel("" + this.year, SwingConstants.CENTER);
monthName.setFont(new Font("Helvetica", 0, 24));
year.setFont(new Font("Helvetica", 0, 24));
monthName.setHorizontalAlignment(JLabel.CENTER);
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
months.add(front, BorderLayout.EAST);
add(months);
Yet it shows up like this:
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
You are using a BoxLayout. A BoxLayout just adds the components horizontally to the panel. The WEST, CENTER, EAST constrains are only used by a BorderLayout so they are ignored by the BoxLayout.
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
When using a BorderLayout you can only add a single component to a region of the layout. So if you want to add two components to the CENTER you need to first create a panel and add the components to the panel.
So your basic code might be something like:
JPanel centerPanel = new JPanel();
centerPanel.add(month);
centerPanel.add(year);
JPanel mainPanel = new JPanel( new BorderLayout() );
mainPanel.add(westButton, BorderLayout.WEST);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(eastButton, BorderLayout.EAST);

Add JScrollpane to JPanel containing multiple JPanels

I have one JFrame (black) that contains one main JPanel (grey), this JPanel contains three other JPanels : North and South (red) and the Center one (blue). The blue JPanel in the center will have lots of JPanels (green) added dynamically to it during the course of the program. When the Center JPanel is full I would like a JScrollbar to appear automatically to scroll down the Center Panel and see all the child (green) panels it contains. Can somebody help me? The problem is that the scrollbar isn't appearing at all, if i add 15 green JPanels to my blue JPanel container, i only see 10 and i can't scroll down.
This is the type of code i have tried so far...
JPanel panelNorth = new JPanel(new GridBagLayout());
panelNorth.setPreferredSize(new Dimension(1000,100);
//add some labels and buttons
JPanel panelCenter = new JPanel();
panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.Y_AXIS));
panelCenter.setPreferredSize(new Dimension(1000,500)
JPanel panel1 = new JPanel(new GridLayout(1, 10));
panel1.setPreferredSize(new Dimension(1000,50));
panelCenter.add(panel1);
//...etc dynamically
JPanel panelSouth = new JPanel(new GridBagLayout());
panelSouth.setPreferredSize(new Dimension(1000,100);
//add some labels and buttons
JScrollPane scrollPaneCenter = new JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//panelCenter must be scrollable when too many panels are added to panelCenter
//add everything to the main panel container
JPanel panelContainer = new JPanel(new BorderLayout(3,3));
panelContainer.setBorder(new EmptyBorder(5,5,5,5));
panelContainer.add(panelNorth,BorderLayout.NORTH);
panelContainer.add(scrollPaneCenter,BorderLayout.CENTER);
panelContainer.add(panelSouth,BorderLayout.SOUTH);
//add everything to frame
JFrame frame = new JFrame();
frame.add(panelContainer);
Thank you.
EDIT:
I changed
JScrollPane scrollPaneCenter = new JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
to this :
JScrollPane scrollPane = new JScrollPane(panelCentre,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
And this is what i get:
I can see it now, it's in the right place, i just can't scroll down to see my other components.
Fixed it by switching
panelCenter.setPreferredSize(new Dimension(1000,500);
with
scrollPane.setPreferredSize(new Dimension(1000,500));

How to fix JPanel with Grid that occupies full width of frame?

I'm working on a layout for a simple login screen. I am using GridLayout in order to manipulate elements, but have came across an issue, It occupies full frame width like this:
Where as I want it to be with a fixed position and width,height and be vertically, horizontally centered inside a frame, but not occupy it's full size.
Possibly make Username and Password labels occupy less space so text fields are actually closer to them. I tried setting main size of my panel like .setMaximumSize(Dimension (200, 150));
But it doesn't seem to work.
//Create the frame.
JFrame frame = new JFrame("Online Shop");
JPanel mainPanel = new JPanel();
JPanel usernamePanel = new JPanel();
JPanel passwordPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton loginButton = new JButton("Login");
loginButton.setMaximumSize(new Dimension(100, 50));
JTextField username = new JTextField();
JLabel usernameLabel = new JLabel("Username");
JPasswordField password = new JPasswordField();
JLabel passwordLabel = new JLabel("Password");
//Panel
frame.setContentPane(mainPanel);
usernamePanel.setLayout(new GridLayout(1,2));
usernamePanel.add(usernameLabel);
usernamePanel.add(username);
passwordPanel.setLayout(new GridLayout(1,2));
passwordPanel.add(passwordLabel);
passwordPanel.add(password);
mainPanel.setLayout(new GridLayout(3, 1));
mainPanel.add(usernamePanel);
mainPanel.add(passwordPanel);
mainPanel.add(loginButton);
//Event Listeners
frame.addWindowListener(new MyWindowListener());
loginButton.addActionListener(new MyActionListener());
//Sizes, Positioning
frame.setSize(720, 480);
frame.setLocationRelativeTo(null);
//Show Frame
frame.setVisible(true);
frame.setSize(720, 480);
Don't use the setSize() method.
Instead use:
frame.pack();
Also use:
JTextField username = new JTextField(10);
To give the text fields a preferred number of characters.
Now when the frame is made visible the components will be displayed at their preferred size and the frame will be sized appropriately to fit the components.
If you want a little extra space between the components and the frame then you can do:
mainPanel.setBorder( new EmptyBorder(20, 20, 20, 20) );

wrong alignment with miglayout

In jframe, I use miglayout for main jpanel position.
in the left panel, I have 2 jpanel, I use boxlayout.
ComponentPanel is the top left position and PropertyPanel is at the bottom left position.
leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setMinimumSize(new Dimension(600, 600));
add(leftPanel, BorderLayout.WEST);
componentPanel = new ComponentPanel();
propertyPanel = new PropertyPanel();
in the propertyPanel constructor, i do
setLayout(new MigLayout("debug"));
i get this
why panel is setted to right?
if i add dynamically some space is added.
I tried to use fill to the miglayout constructor without success.
in green is the leftPanel
in red is the componentPanel
http://imagepaste.nullnetwork.net/img/1354548433miglayout3.jpg
Using BorderLayout instead of BoxLayout seem better...
leftPanel.setLayout(new BorderLayout());
leftPanel.add(componentPanel, BorderLayout.NORTH);
leftPanel.add(propertyPanel,BorderLayout.SOUTH);
BoxLayout problem?

Categories

Resources