Best Layout for Dynamically Sized Panel - java

What I am looking for is a suggestion for the best layout to use for my scenario. I basically have any number of child panels which can be in a container panel that can be dynamically resized by the user. All of the child panels will be 300 pixels in width, and can have a variable height. I would like the panels to be placed into the panel from left-to-right, top-to-bottom, just like the FlowLayout. However, anything I try with the FlowLayout will vertically center the panel with less height. I would instead like the panel to be anchored to the top of the screen.
I have created the following example using the FlowLayout to show what I mean.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DynamicPanel extends JPanel {
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new DynamicPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
public DynamicPanel() {
setupGUI();
}
private void setupGUI() {
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(getPanel(1, 4));
this.add(getPanel(2, 2));
}
private JPanel getPanel(int panelNum, int numButtons) {
JPanel panel = new JPanel(new GridBagLayout()) {
#Override
public Dimension getPreferredSize() {
Dimension ret = super.getPreferredSize();
ret.width = 300;
return ret;
}
};
panel.add(new JLabel("Panel "+panelNum), getGrid(0, 0, 1.0, 0));
for(int i = 0; i < numButtons; i++) {
panel.add(new JButton("Button"), getGrid(0, i+1, 1.0, 0));
}
return panel;
}
/*
* Returns the GridBagConstraints for the given x, y grid location
*/
private GridBagConstraints getGrid(int x, int y, double xweight, double yweight) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = x;
c.gridy = y;
c.weightx = xweight;
c.weighty = yweight;
return c;
}
}
In this example, I would like the labels Panel1, and Panel2 to be straight across from each other, instead of Panel2 being set lower because the associated panel is centered.
I guess I could use GridBagLayout, and add a component listener to the container panel, and edit the GridBagContraints accordingly for each child panel when the container panel is resized, but I am wondering if there is a better way to do this? In case this matters, in the actual program the child panels will be custom panels, not just a list of buttons.
Thanks in advance for any help!

I was able to achieve this
Using this...
private void setupGUI() {
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
this.add(getPanel(1, 4), gbc);
this.add(getPanel(2, 2), gbc);
}
The problem is, you're going to need to get your hands a little dirty, as no layout manager will do exactly what you want (with the possible exception of MigLayout, but I've never used it)
What I would do, is create a JPanel per row, set it's layout to GridBagLayout and use the above concept to layout to layout the number of columns you need, then do this for each row...

Related

Java swing layout, 3 panels

So I'm trying to make my first small java game in my own spare time. I am having trouble with the layout.
I want the game to be a specific size (600 height, 800 width) and I would like 3 "Panels" within the main frame. One which is the main game frame and would be 500 height and 600 width, an inventory/info panel on the right which should have 500 height and 200 width and finally a text panel at the bottom to hold information which would have a height of 100 and a width of 800. So far, here is what I have. (I didn't play around with the panel height as I found nothing changed).
How would I go about making a frame with those 3 panels inside it. I had a look on how to use GridBagLayout() but it seems I have made it worse and do not fully understand how to use it, even with the documentation (yes, im stupid).
LMK if you don't understand parts of my code or my post for that matter. Thank you.
package Frame;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class frame {
//Sets variables such as height, width and title
private JFrame frame;
private Canvas canvas;
private JPanel mainWindow, infoWindow, textWindow;
//main constructor to create the frame of the game. Is called in Launcher
//Sets the parameters of the frame. User defined in main.
public frame(){
createDisplay();
}
//sets the properties of the display
private void createDisplay() {
frame = new JFrame();
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.setTitle("Island Man");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
canvas = new Canvas();
canvas.setSize(new Dimension(800, 600));
canvas.setMaximumSize(new Dimension(800, 600));
canvas.setMinimumSize(new Dimension(800, 600));
mainWindow = new JPanel();
mainWindow.setBackground(Color.CYAN);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 2;
c.weighty = 2;
frame.add(mainWindow, c);
infoWindow = new JPanel();
infoWindow.setBackground(Color.GREEN);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
c.weightx = 0;
c.weighty = 2;
frame.add(infoWindow, c);
textWindow = new JPanel();
textWindow.setBackground(Color.MAGENTA);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
c.weightx = 0;
c.weighty = 3;
frame.add(textWindow, c);
frame.add(canvas);
frame.pack();
}
}
One way is to use a BorderLayout with the main panel in CENTER, the inventory panel in EAST, and the text panel in SOUTH. Be sure to set the preferred size of each of the panels.
How do I make a frame that has these 3 panels with those dimensions?
There are too many ways to do it. I most direct ways would be setting the size accordingly for each panel and add them to the main panel, then add the main panel to the frame:
The exact Layout to use is dependent on..
how you want your subpanels to be arranged and
how would you want them to react when the frame is resized and
how you want the components in the panels to be arranged.
The following shows one possible way by making use of the FlowLayout.
class MainPanel extends JPanel
{
public MainPanel(){
setPreferredSize(new Dimension(800, 600));
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
add(new GamePanel());
add(new InventoryPanel());
add(new TextPanel());
}
}
class GamePanel extends Panel
{
public GamePanel(){
setPreferredSize(new Dimension(500, 600));
setBackground(Color.ORANGE);
}
}
class InventoryPanel extends Panel
{
public InventoryPanel(){
setPreferredSize(new Dimension(200, 600));
setBackground(Color.YELLOW);
}
}
class TextPanel extends Panel
{
public TextPanel(){
setPreferredSize(new Dimension(100, 600));
setBackground(Color.CYAN);
}
}
Adding the main panel to the frame:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

jtextarea wont fill panel with GridBagConstraints

From my code I expect my JTextArea to fill the top left border seen below:
But as you can see its taking up a tiny section in the middle.
I am using GridBagConstraints on the panel which contains the components.
There is a main class which calles up a class called frame. This class creates the JFrame and sets the size as well as other things. This is then called from the MainFrame.java which has extended the jframe which creates 3 panels and sets their layout. this is seen below
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame
{
private Panel1 storyPanel;
private Panel2 statsPanel;
private Panel3 commandsPanel;
public MainFrame(String title)
{
super(title);
// Setting Layout
GridBagConstraints gbc = new GridBagConstraints();
storyPanel = new Panel1();
storyPanel.setLayout(new GridBagLayout());
statsPanel = new Panel2();
commandsPanel = new Panel3();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(statsPanel, BorderLayout.EAST);
p.add(commandsPanel, BorderLayout.SOUTH);
}
}
The panel in questions is Panel1 or storyPanel. I have set the layout and the code calls the Panel1.java as seen below:
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
GridBagConstraints gbc = new GridBagConstraints();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//GridBagConstraints setup for components
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
add(scroll, gbc);
}
}
I don't understand why my gbc.fill isnt making the JTextArea fill the top left border of the screen shot.
Thanks in advance for any reply's
-Tom T
changing the layout to border layout
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
//GridBagConstraints gbc = new GridBagConstraints();
//gbc.weightx = 0.1;
//gbc.weighty = 0.1;
BorderLayout b = new BorderLayout();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//gbc.gridx = 0;
//gbc.gridy = 0;
//gbc.weightx = 1;
//gbc.weighty = 1;
//gbc.fill = GridBagConstraints.BOTH;
add(scroll, b.CENTER);
}
}
Don't set the layout of storyPanel, as it's contents have already been added and laid out, so changing the layout manager here will discard any properties you applied. Instead, set the layout in Panel1's constructor before you add any components.
Use GridBagConstraints.BOTH for the fill property. The fill property can only have one specified value
Use weightx and weighty to specify how much of the available space the component should use
For example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MainFrame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new MainFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private Panel1 storyPanel;
// private Panel2 statsPanel;
// private Panel3 commandsPanel;
public MainFrame(String title) {
super(title);
storyPanel = new Panel1();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(new JLabel("East"), BorderLayout.EAST);
p.add(new JLabel("South"), BorderLayout.SOUTH);
}
public class Panel1 extends JPanel {
public Panel1() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//Set size of Panel1
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(20, 20);
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//GridBagConstraints setup for components
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
add(scroll, gbc);
}
}
}
Having said all that, a BorderLayout would be simpler
public class Panel1 extends JPanel {
public Panel1() {
setLayout(new BorderLayout());
//Set size of Panel1
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(20, 20);
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll);
}
}
i tried a border layout and set it to center expecting it to resize but that failed as well
Would suggest that you're making a fundamental mistake some where, as it works fine for me. Remember, set the layout BEFORE you add any components to the container

Java Swing Component Resizing with GridBagLayout

I've been having trouble with my Java Swing GUI. First of all, I created a panel with the GridBagLayout on it and added all my labels to it. However, I also created a panel to the right of the other JPanel that adds in a button and 2 sliders which are suppose to like match with the labels.
The problem is that the JLabels are smaller than the components from the right of the other panel which, makes it look like this....
ex. water option -- JSLIDER (the jslider looks a lot bigger)
I tried to make the components larger by adding ipadx to a bigger value and also I've tried to set the grid width for the labels panel bigger, but nothing seems to work. It just doesn't respond.
Here is the code:
package gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
public class Options extends JFrame{
//water option (button changes text on or off)
//make a button listener
JSlider renderDistance;
JSlider grassDensity;
JButton waterToggleButton;
JLabel rdTitle;
JLabel gdTitle;
JLabel wtTitle;
JButton buttonClose;
static final int RD_MIN_VALUE = 0;
static final int RD_MAX_VALUE = 1000;
static final int RD_INIT_VALUE = 500;
static final int GD_MIN_VALUE = 0;
static final int GD_MAX_VALUE = 1000;
static final int GD_INIT_VALUE = 500;
public Options() {
this.setTitle("Settings");
this.setSize(getMaximumSize());
this.setLocationRelativeTo(null);
createView();
this.setVisible(true);
}
private void createView() {
//Making panels and adding them to window*
JPanel pOptions = new JPanel();
this.add(pOptions);
//These are for the labels that I added so people know which option they are using
JPanel pOptionLabels = new JPanel(new GridBagLayout());
pOptions.add(pOptionLabels);
//These are for the middle columns, the objects like button and slider
JPanel pOptionObjects = new JPanel(new GridBagLayout());
pOptions.add(pOptionObjects);
//Making panels and adding them to window*
//Initializing Objects*
GridBagConstraints gbcLabels = new GridBagConstraints();
GridBagConstraints gbcObjects = new GridBagConstraints();
renderDistance = new JSlider(RD_MIN_VALUE, RD_MAX_VALUE, RD_INIT_VALUE);
grassDensity = new JSlider(GD_MIN_VALUE, GD_MAX_VALUE, GD_INIT_VALUE);
waterToggleButton = new JButton("On");
rdTitle = new JLabel("Render Distance");
gdTitle = new JLabel("Grass Density");
wtTitle = new JLabel("Water Terrain Visibility");
//Initializing Objects*
//Giving objects some attributes using methods*
renderDistance.setMinorTickSpacing(100);
renderDistance.setMajorTickSpacing(500);
renderDistance.setPaintTicks(true);
renderDistance.setPaintLabels(true);
grassDensity.setMinorTickSpacing(100);
grassDensity.setMajorTickSpacing(500);
grassDensity.setPaintTicks(true);
grassDensity.setPaintLabels(true);
gbcLabels.gridx = 0;
gbcLabels.gridy = 0;
gbcLabels.anchor = GridBagConstraints.LINE_START;
gbcObjects.gridx = 0;
gbcObjects.gridy = 0;
waterToggleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(waterToggleButton.getText().equals("Off")) {
waterToggleButton.setText("On");
}else {
waterToggleButton.setText("Off");
}
}
});
//Giving objects some attributes using methods*
//Add things to panel ex. p.add();
pOptionLabels.add(rdTitle, gbcLabels);
gbcLabels.gridy++;
pOptionLabels.add(gdTitle, gbcLabels);
gbcLabels.gridy++;
pOptionLabels.add(wtTitle, gbcLabels);
gbcObjects.gridx++;
pOptionObjects.add(renderDistance, gbcObjects);
gbcObjects.gridy++;
pOptionObjects.add(grassDensity, gbcObjects);
gbcObjects.gridy++;
pOptionObjects.add(waterToggleButton, gbcObjects);
}
public static void main(String[] args) {
new Options();
}
}
Change the way you are thinking. Instead of trying to layout all the labels and all the "other" components in separate containers, consider putting them all in the same container.
This way, when they are laid out, the layout calculations are made in relationship to all the components in a single context.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
public class Options extends JFrame {
//water option (button changes text on or off)
//make a button listener
JSlider renderDistance;
JSlider grassDensity;
JButton waterToggleButton;
JLabel rdTitle;
JLabel gdTitle;
JLabel wtTitle;
JButton buttonClose;
static final int RD_MIN_VALUE = 0;
static final int RD_MAX_VALUE = 1000;
static final int RD_INIT_VALUE = 500;
static final int GD_MIN_VALUE = 0;
static final int GD_MAX_VALUE = 1000;
static final int GD_INIT_VALUE = 500;
public Options() {
this.setTitle("Settings");
createView();
pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private void createView() {
//Making panels and adding them to window*
JPanel pOptions = new JPanel();
this.add(pOptions);
//These are for the labels that I added so people know which option they are using
JPanel pOptionLabels = new JPanel(new GridBagLayout());
pOptions.add(pOptionLabels);
GridBagConstraints gbc = new GridBagConstraints();
renderDistance = new JSlider(RD_MIN_VALUE, RD_MAX_VALUE, RD_INIT_VALUE);
grassDensity = new JSlider(GD_MIN_VALUE, GD_MAX_VALUE, GD_INIT_VALUE);
waterToggleButton = new JButton("On");
rdTitle = new JLabel("Render Distance");
gdTitle = new JLabel("Grass Density");
wtTitle = new JLabel("Water Terrain Visibility");
//Initializing Objects*
//Giving objects some attributes using methods*
renderDistance.setMinorTickSpacing(100);
renderDistance.setMajorTickSpacing(500);
renderDistance.setPaintTicks(true);
renderDistance.setPaintLabels(true);
grassDensity.setMinorTickSpacing(100);
grassDensity.setMajorTickSpacing(500);
grassDensity.setPaintTicks(true);
grassDensity.setPaintLabels(true);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(2, 2, 2, 2);
waterToggleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (waterToggleButton.getText().equals("Off")) {
waterToggleButton.setText("On");
} else {
waterToggleButton.setText("Off");
}
}
});
//Giving objects some attributes using methods*
//Add things to panel ex. p.add();
pOptionLabels.add(rdTitle, gbc);
gbc.gridy++;
pOptionLabels.add(gdTitle, gbc);
gbc.gridy++;
pOptionLabels.add(wtTitle, gbc);
gbc.gridx++;
gbc.gridy = 0;
pOptionLabels.add(renderDistance, gbc);
gbc.gridy++;
pOptionLabels.add(grassDensity, gbc);
gbc.gridy++;
pOptionLabels.add(waterToggleButton, gbc);
}
public static void main(String[] args) {
new Options();
}
}
What you are experiencing is technically correct since you have two JPanels with two separate instances of the GridBagLayout manager so the rows (and columns) in one layout manager have no information about the rows in the other.
A single GridBagLayout is the way to go if you plan to use JComponents inside them and create a table like appearance. You can later add others JPanels (with different layout managers or with more GridBagLayouts!) but INSIDE your main GridBagLayout JPanel.
GridBagLayout is a really powerful layout, I would recommend sticking with is and master it since it will help you layout things nicely (and fast) in the long run.
This is a great resource to learn more about the GridBagLayout:
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
One small comment: IMO the GridBagLayout is not the best layout manager to use as the "root" layout container. I would instead recommend creating a "root" JPanel with a BorderLayout() and add your GridBagLayout JPanel to the center of the BorderLayout panel add(yourGridPanel, BorderLayout.CENTER) since the BorderLayout usually gives for free the ability for things to fill horizontally/vertically and will let you pack your window nicely at the center (with the possibility of adding more things later to the NORTH/SOUTH/EAST/WEST sides).
Good luck!

BorderLayout, GridLayout, GridBagLayout? Which should I use?

I'm trying to create this basic GUI, but cannot get my panels to setup correctly.(Numbers are pixel sizes)
I've tried using this tutorial as a reference (http://www.youtube.com/watch?v=Kl3klve_rmQ) but, mine never works the same.
My code declares variables in the top of the class, then creates a constructor which add the components (panels, buttons, etc), then it calls the constructor in the main method.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FinalProject extends JPanel
{
private static final long serialVersionUID = 1L;
static JPanel nav;
static JPanel queue;
static JPanel menu;
GridBagConstraints gbc = new GridBagConstraints();
public FinalProject()
{
nav = new JPanel();
nav.setLayout(new GridBagLayout());
nav.setBackground(Color.RED);
gbc.gridy = 0;
gbc.gridx = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
add(nav, gbc);
queue = new JPanel();
queue.setLayout(new GridBagLayout());
queue.setBackground(Color.GREEN);
gbc.gridy = 1;
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
add(queue, gbc);
menu = new JPanel();
menu.setLayout(new GridBagLayout());
menu.setBackground(Color.BLUE);
gbc.gridy = 2;
gbc.gridx = 2;
gbc.gridheight = 1;
gbc.gridwidth = 1;
add(menu, gbc);
}
public static void main(String[] args)
{
FinalProject p = new FinalProject();
JFrame f = new JFrame();
f.add(nav);
f.add(queue);
f.add(menu);
f.setTitle("Subway");
f.setSize(800, 500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(p);
}
}
How should I go about getting this layout right? Panels in Panels, Panels independent of each other, etc.?
No, don't use a GridBagLayout for this as you'll be adding more complexity than is actually needed. Myself, I try to avoid using this layout and all its potential pitfalls as much as possible, and usually you can get all you need by nesting JPanels, each using its own more simple layout. For instance here, all you need is a BorderLayout:
Place the top JPanel in the BorderLayout.NORTH position
Place the left JPanel in the BorderLayout.WEST position
Place the center JPanel in the BorderLayout.CENTER position.
That's it.
Again, please check out the Swing and the layout manager tutorials as the information is well presented there.
Edit
Note that nothing shows up on your JFrame because you're not adding your JPanel to the JFrame!
Edit 2
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.*;
public class SimpleLayout {
private static final Color GREEN = new Color(200, 255, 200);
private static final Color BLUE = new Color(200, 200, 255);
private static void createAndShowGui() {
JFrame frame = new JFrame("SimpleLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// note that a JFrame's contentPane uses BorderLayout by default
frame.getContentPane().add(new ColorPanel(Color.pink, 800, 80), BorderLayout.NORTH);
frame.getContentPane().add(new ColorPanel(GREEN, 300, 420), BorderLayout.WEST);
frame.getContentPane().add(new ColorPanel(BLUE, 500, 420), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ColorPanel extends JPanel {
private static final float FONT_POINTS = 24f;
private int prefW;
private int prefH;
public ColorPanel(Color color, int prefW, int prefH) {
setBackground(color);
this.prefW = prefW;
this.prefH = prefH;
// GBL can be useful for simply centering components
setLayout(new GridBagLayout());
String text = String.format("%d x %d", prefW, prefH);
JLabel label = new JLabel(text, SwingConstants.CENTER);
label.setFont(label.getFont().deriveFont(FONT_POINTS));
label.setForeground(Color.gray);
add(label);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
}
This displays as:
GridLayout I've never actually used but I'm not sure if it can do this.
BorderLayout can do this and is simpler to use than GridBagLayout.
GridBagLayout can do this.
In general GridBagLayout is one of the most flexible LayoutManagers and is well worth learning so I'd recommend using it here to get used to it on a simple case before you need it anyway for something more complex.
If you are familiar with HTML then think of GridBagLayout as working like HTML tables.
Very quickly - create the three panels and set the sizes/borders/whatever you need in them.
Add one in cell 0,0 with a colspan of 2.
Add one in cell 1,0
Add one in cell 1,1
After that you are done although you will probably want to specify resize/anchor/etc behaviour.

Java Swing BoxLayout ignoring AlignmentX

In the code below, by calling setAlignmentX with Component.LEFT_ALIGNMENT I expected to get a left aligned label over a centered slider. For some reason the label is also centered, seemingly regardless of what value is passed to setAlignmentX.
What value must I pass to setAlignmentX to get it left aligned?
package myjava;
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
public class LayoutTest {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create left aligned label over centered column
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JLabel label = new JLabel("test");
label.setAlignmentX(Component.LEFT_ALIGNMENT);
contentPane.add(label);
contentPane.add(new JSlider());
frame.pack();
frame.setVisible(true);
}
});
}
}
Basically, you can't have different alignments in BoxLayout, from How To Use BoxLayout
In general, all the components controlled by a top-to-bottom BoxLayout
object should have the same X alignment.
Edit
Typically, it's not documented which default alignment a component type has (JSlider is centered by default, me incorrectly thought that a JLabel were centered while it is left-aligned ;-) One option is to keep a list somewhere (dooooh...), another is to simply force them all to the same alignment on adding.
Or use a third-party layoutManager, which doesn't have this rather unintuitve (for me) mix-in of layout and alignment.
BoxLayout have strange behavior. Try to use GridBagLayout instead:
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
public class Aligment {
public static void main(String[] args) {
final JPanel root = new JPanel(new GridBagLayout());
root.setPreferredSize(new Dimension(500, 400));
root.add(new JLabel("LEFT"), new GridBagConstraints() {{
gridx = 0;
gridy = 0;
anchor = PAGE_START;
}});
root.add(new JLabel("CENTER"), new GridBagConstraints() {{
gridx = 1;
gridy = 1;
anchor = CENTER;
weightx = 1.0; // fill Width
}});
root.add(new JLabel("RIGHT"), new GridBagConstraints() {{
gridx = 2;
gridy = 2;
anchor = LINE_END;
}});
// hack: Push all rows to Top
root.add(Box.createVerticalGlue(), new GridBagConstraints() {{
gridx = 0;
gridy = 3;
weighty = 1.0; // fill Height
}});
new JFrame() {
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(root);
pack();
setLocationRelativeTo(null);;
}
}.setVisible(true);
}
}

Categories