Java Swing - realising a layout with LayeredPane - java

I have a question regarding making a specific Layout, first I'll show examples then I will add some extra clarification.
Layout when Friends and Messages are closed:
Layout when Friends and Messages are opened:
I intend to make this layout with Java Swing.
My intention is to firstly have the Frame divided in three areas, the top menu row, the main panel and the bottom menu row.
I was thinking of using a BorderLayout for this part.
Then the Friends and Messages buttons should be toggle button's, and on toggle they should show an overlay on top of the mainpanel (or whatever is there), containing a friend list and a message area. I realised I need to use a LayeredPane somehow for this.
Another important part is that the Layout should be viewable in any size, that is the user may resize the application and it will be used on a various amount of resolutions, so I don't really want anything with a fixed width and height.
But I am really lost as how to combine this, so therefore I ask your help.
Hopefully I have explained enough about the situation.
Regards.

this could be about overlay, because JPanel can contains other JComponents
use JLayer (Java7) based on JXLayer(Java6),
use GlassPane with JComponents layed to rellative to....
easiest could be to use JDialog(undecorated) layed to Point (setLocation(int, int)), setVisible() must be wrapped into invokeLater

I will use gridBagLayout.
Here is small example including button which hide your yellow panels:
package Core;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
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;
public class GridBagLayoutDemo {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
add1row(pane);
addmainRow(pane);
addLastRow(pane);
}
private static void addLastRow(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
c.anchor = GridBagConstraints.PAGE_END;
JPanel bottonPanel = new JPanel();
bottonPanel.setBackground(Color.BLUE);
bottonPanel.setLayout(new GridBagLayout());
pane.add(bottonPanel, c);
JPanel messagePanel = new JPanel();
messagePanel.setBackground(Color.GRAY);
messagePanel.add(new JLabel("MESSAGES"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
bottonPanel.add(messagePanel, c);
}
private static void addmainRow(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.CENTER;
JPanel mainManel = new JPanel();
mainManel.setBackground(Color.GREEN);
mainManel.setLayout(new GridBagLayout());
pane.add(mainManel, c);
final JPanel friendListPanel = new JPanel();
friendListPanel.setBackground(Color.YELLOW);
friendListPanel.add(new JLabel("FRIEND LIST"));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.FIRST_LINE_END;
mainManel.add(friendListPanel, c);
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 1;
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.CENTER;
JButton button = new JButton("On/Off");
mainManel.add(button, c);
final JPanel messageAreaPanel = new JPanel();
messageAreaPanel.setBackground(Color.YELLOW);
messageAreaPanel.add(new JLabel("MESSAGE PANEL"));
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 2;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.LAST_LINE_END;
mainManel.add(messageAreaPanel, c);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
friendListPanel.setVisible(!friendListPanel.isVisible());
messageAreaPanel.setVisible(!messageAreaPanel.isVisible());
}
});
}
private static void add1row(Container pane) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.PAGE_START;
Panel headerPanel = new Panel();
headerPanel.setLayout(new GridBagLayout());
headerPanel.setBackground(Color.BLUE);
pane.add(headerPanel, c);
JPanel quitPanel = new JPanel();
quitPanel.setBackground(Color.GRAY);
quitPanel.add(new JLabel("QUIT"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
headerPanel.add(quitPanel, c);
JPanel friendsPanel = new JPanel();
friendsPanel.setBackground(Color.GRAY);
friendsPanel.add(new JLabel("FRIENDS"));
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.LINE_END;
c.gridx = 1;
c.gridy = 0;
headerPanel.add(friendsPanel, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame);
// uncoment to use full screen
// frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setSize(new Dimension(400, 400));
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Related

Java GridBagLayout positioning

i'm trying to make a window with GridBagLayout, here are my code:
import java.awt.FlowLayout;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ReadMessage extends JFrame implements ActionListener
{
JButton Last;
JButton Delete;
JButton Store;
JButton Next;
JTextArea MessageBox;
public ReadMessage()
{
setLayout(new FlowLayout());
JPanel Panel = new JPanel();
add(Panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
MessageBox = new JTextArea();
MessageBox.setEditable(false);
JScrollPane scrollPane = new JScrollPane(MessageBox, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
MessageBox.setLineWrap(true);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
c.weightx = 0.0;
c.ipady = 300;
c.ipadx = 300;
Panel.add(scrollPane, c);
Last = new JButton("Last");
c.gridx = 0;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Last, c);
Last.addActionListener(this);
Delete = new JButton("Delete");
c.gridx = 1;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Delete, c);
Delete.addActionListener(this);
Store = new JButton("Store");
c.gridx = 2;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Store, c);
Store.addActionListener(this);
Next = new JButton("Next");
c.gridx = 3;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Next, c);
Next.addActionListener(this);
}
}
and it turns out to be something like this
what i really want is like this
I know i did terribly wrong but i can't figure out what exactly i did wrong, I read the docs on oracle but couldn't find a thing, could you point out what i did wrong, and how to fix it? thank you very much
All your buttons have a gridwidth of 4 instead of 1.
You can nest panels each using a different layout manager to achieve your desired layout.
Create a main panel with a BorderLayout and add this panel to the frame
Add the JTextArea to the BorderLayout.CENTER of the main panel
Create a second panel for the buttons and use a FlowLayout. Then add the buttons to this panel. Then this panel can be added to the main panel at the BorderLayout.PAGE_END.

Java GridBagLayout does not fill frame horizontally

I am writing the GUI for a chat program. I can't seem to get the scroller to fill the frame horizontally and vertically and the messageInput to fill the frame horizontally. This is how it looks:
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame{
private JPanel panel;
private JEditorPane content;
private JTextField messageInput;
private JScrollPane scroller;
private JMenu options;
private JMenuBar mb;
private JMenuItem item;
public GUI(){
/** This is the frame**/
this.setPreferredSize(new Dimension(380,600));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
/** This is where the context shows up **/
content = new JEditorPane();
content.setEditable(false);
/** Scroller that shows up in the context JEditorPane **/
scroller = new JScrollPane(content);
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 0;
panel.add(scroller, c);
/** This is where you type your message **/
messageInput = new JTextField();
c.weightx = 0.0;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 1;
c.weighty = 0.5;
panel.add(messageInput, c);
mb = new JMenuBar();
options = new JMenu("Options");
mb.add(options);
this.setJMenuBar(mb);
this.add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
get the scroller to fill the frame horizontally and vertically and the messageInput to fill the frame horizontally.
You want to fill in both directions, so set
c.fill = GridBagConstraints.BOTH; // not HORIZONTAL
The next part is to fix the weights, which will tell how much space to allocate for each component (relatively):
scroller = new JScrollPane(content);
c.weightx = 0.5;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
panel.add(scroller, c);
messageInput = new JTextField();
c.weightx = 0.5;
c.weighty = 0.0;
c.gridx = 0;
c.gridy = 1;
panel.add(messageInput, c);
weightx should be a non-zero value to allow the components to stretch horizontally. weighty should be non-zero for the editor, but not for the text field so that it won't take extra vertical space (in this case you don't need to set c.fill = GridBagConstraints.HORIZONTAL for it).

how to put components in the center in a CardLayout panel

I have a frame that has couple of panels and they change by CardLayout.
Inside each panel i will have different components. To design the GUI of the panel I used GridBagLayout. But the problem is any component or Layout I use for these paneles, they all stay at the top of the page. So basically the panel size of the CardLayout is some small amount of the frame. I want to make the sub panel size as large as the main CardLayout size.
Code for main CardLayout:
public class MainPanel extends JPanel {
private CardLayout cl = new CardLayout();
private JPanel panelHolder = new JPanel(cl);
public MainPanel() {
NewSession session = new NewSession(this);
ChooseSource chooseSource = new ChooseSource(this);
panelHolder.add(session, "1");
panelHolder.add(chooseSource, "2");
cl.show(panelHolder, "dan");
add(panelHolder);
}
public void showPanel(String panelIdentifier){
cl.show(panelHolder, panelIdentifier);
}
}
A sub panel:
public class ChooseSource extends JPanel {
MainPanel ob2;
JButton btn;
JLabel label;
JTextField field;
public ChooseSource(MainPanel mainPanel){
this.ob2 = mainPanel;
btn = new JButton("Browse");
label = new JLabel("Folder ");
field = new JTextField();
btn.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
ob2.showPanel("1");
}
});
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.ipady = 20;
c.gridheight = 2;
add(btn, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
c.ipady = 10;
c.gridheight = 1;
c.insets = new Insets(0,10,0,0);
add(label, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.ipady = 0;
c.gridheight = 1;
c.insets = new Insets(0,10,0,0);
add(field, c);
}
}
The left image shows how it is not, and right one is the one I am trying to make. basically I want to have access to all the available space of the panel.
Any idea?
Change the layout manager of MainPanel to something like BorderLayout or GridBagLayout

GridBagLayout elements behaviour when resizing window

I'm new to Java Swing and was trying to make a simple layout (I thought), but I have a lot of problems reaching the behavior I want. Here's my code :
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
equipementPanel.setPreferredSize(new Dimension(275, 300));
grillePanel.setPreferredSize(new Dimension(300, 600));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.0; c.weighty = 0.0;
this.add(equipementPanel, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0; c.gridy = 1;
c.anchor = GridBagConstraints.SOUTHWEST;
c.gridwidth = 1; c.gridheight = 2;
c.weightx = 0.0; c.weighty = 0.0;
this.add(informationPanel, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1; c.gridy = 0;
c.anchor = GridBagConstraints.NORTHEAST;
c.weightx = 1.0; c.weighty = 1.0;
this.add(grillePanel, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1; c.gridy = 1;
c.anchor = GridBagConstraints.SOUTHEAST;
c.weightx = 1.0; c.weighty = 0.0;
this.add(commandePanel, c);
this.validate();
Screen of my laptop is a good result of what I want.
equipementPanel = green
grillePanel = gray
But on my bigger screen...it is the gray one that should stretch. Red it's okay.
And a total disaster when I size it down .
What I want it to do is
The Gray should not have fixed height and width.
The Yellow should always have the fixed height but not width.
The Red should always have fixed width but not height.
The Green should always have both fixed.
The smallest the window could become would be set to the height of the Green + Yellow one. and width of Green + whatever nice to display.
I know that the weird behavior with the small window is because I go under 300 + 600 of my preferred size...but I need to fix some size somewhere!?!?
If I can reach the same behavior with another layout, I'd be glad to try it. If I change and use some ScrollPanel, does that change anything?
I added a mcve :
MCVE.JAVA
package mcve;
import java.awt.EventQueue;
public class MCVE {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
mcve.gui.MainWindow mainWindow = new mcve.gui.MainWindow();
mainWindow.setVisible(true);
});
}
}
MainWindow.Java
package mcve.gui;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
public class MainWindow extends JFrame
{
public MainWindow()
{
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLayout(new GridBagLayout());
initComponents();
}
private void initComponents()
{
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
GrillePanel grillePanel = new GrillePanel();
CommandePanel commandePanel = new CommandePanel();
InformationPanel informationPanel = new InformationPanel();
EquipementPanel equipementPanel = new EquipementPanel();
equipementPanel.setPreferredSize(new Dimension(275, 300));
grillePanel.setPreferredSize(new Dimension(300, 600));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.gridwidth = 1; c.gridheight = 1;
c.weightx = 0.0; c.weighty = 0.0;
this.add(equipementPanel, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0; c.gridy = 1;
c.anchor = GridBagConstraints.SOUTHWEST;
c.gridwidth = 1; c.gridheight = 2;
c.weightx = 0.0; c.weighty = 0.0;
this.add(informationPanel, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1; c.gridy = 0;
c.anchor = GridBagConstraints.NORTHEAST;
c.weightx = 1.0; c.weighty = 1.0;
this.add(grillePanel, c);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1; c.gridy = 1;
c.anchor = GridBagConstraints.SOUTHEAST;
c.weightx = 1.0; c.weighty = 0.0;
this.add(commandePanel, c);
this.validate();
}
}
the 4 panel
package mcve.gui;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class InformationPanel extends JPanel
{
public InformationPanel()
{
setBackground(Color.red);
setBorder(BorderFactory.createLineBorder(Color.black));
setVisible(true);
}
}
package mcve.gui;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class GrillePanel extends JPanel
{
public GrillePanel()
{
setBackground(Color.lightGray);
setBorder(BorderFactory.createLineBorder(Color.black));
setVisible(true);
}
}
package mcve.gui;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class EquipementPanel extends JPanel
{
public EquipementPanel()
{
setBackground(Color.green);
setBorder(BorderFactory.createLineBorder(Color.black));
setVisible(true);
}
}
package mcve.gui;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class CommandePanel extends JPanel
{
public CommandePanel()
{
setBackground(Color.yellow);
setBorder(BorderFactory.createLineBorder(Color.black));
setVisible(true);
}
}
There are two basic issues (as I see it)...
One, you are trying to manage a complex layout within a single layout manager, which is pretty hard at the best of times.
Two, you don't seem to understand what the layout manager will do when the available size of the component drops below it's preferred size, which is, in the case of GridBagLayout, revert to it's minimum size...
You can overcome some of this through the use of weights (weightx/weighty), but sometimes, you just need to provide a hard value for the minimum size as well...
Without knowing your exact needs (and you're going to need to make decisions about which components are more important), this is a rough example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LayoutTest {
public static void main(String[] args) {
new LayoutTest();
}
public LayoutTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel greenPane = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(275, 300);
}
#Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
#Override
public Color getBackground() {
return Color.GREEN;
}
};
JPanel redPane = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 600);
}
#Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
#Override
public Color getBackground() {
return Color.RED;
}
};
JPanel left = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 0.25;
gbc.fill = GridBagConstraints.BOTH;
left.add(greenPane, gbc);
gbc.gridy++;
gbc.weighty = 0.75;
left.add(redPane, gbc);
JPanel yellowPane = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 50);
}
#Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
#Override
public Color getBackground() {
return Color.YELLOW;
}
};
JPanel grayPane = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 600);
}
#Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
#Override
public Color getBackground() {
return Color.GRAY;
}
};
JPanel center = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
center.add(grayPane, gbc);
gbc.gridy++;
gbc.weighty = 0;
center.add(yellowPane, gbc);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(left, BorderLayout.WEST);
frame.add(center);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Swing layout managers are quite decent, but unfortunetly they can make a lot of troubles. I think that the only layout managers that are actually usable are BorderLayout, GroupLayout and only in some cases GridBayLayout. In most cases I have found they are useless.
Why dont you try to use 3rd party layout managers? From my experience I can tell thal MigLayout is more than great. It is very flexible and have decent API. You will get desired layout composition in no time. I am using it in all desktop projects.

Gridbag layout resizes when I add components

I am new to gridbag layout. While doing testing, I noticed that my grid bag layout does not retain specific size (grid size) when I add components.
I want my application to be compatible with various screensizes, so I can not hardcode the size in advance.
What could I be doing wrong. Am I not understanding the way grids should be designed?
Update: All I had to do was to set the preferred Size of the component. It they stopped growing arbitrarily.
code:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Main extends JFrame {
// Drawing panels
private JPanel panelLeft;
private JPanel panelCenter;
private JPanel panelRight;
private JPanel panelTopCenter, panelTopLeft, panelTopRight;
private JPanel storyPanel;
// Layout
private GridBagLayout gridBadLayout;
private static final String DEFAULT_GAME_WORLD_NAME = "DefaultWorldName";
// Constructor
public Main() {
initializePanels();
}
/*
* Initializes look and feel of the window. No components should be
* initalized in this because controller would like to lazily initialize
* them once data is avialable.
*/
private void initializePanels() {
this.setTitle("World Maker");
this.setBounds(55, 5, 600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
gridBadLayout = new GridBagLayout();
panel.setLayout(gridBadLayout);
GridBagConstraints c = new GridBagConstraints();
this.add(panel);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.3;
c.weighty = 0.1;
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.BOTH;
panelTopLeft = new JPanel();
panelTopLeft.setBackground(Color.WHITE);
panelTopLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopLeft, c);
c.gridx = 1;
c.gridwidth = 3;
panelTopCenter = new JPanel();
panelTopCenter.setBackground(Color.WHITE);
panelTopCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopCenter, c);
c.gridx = 4;
c.gridwidth = 1;
panelTopRight = new JPanel();
panelTopRight.setBackground(Color.WHITE);
panelTopRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopRight, c);
c.gridx = 0;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
panelLeft = new JPanel();
panelLeft.setBackground(Color.WHITE);
panelLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelLeft, c);
c.gridx = 1;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 3;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
panelCenter = new JPanel();
panelCenter.setBackground(Color.WHITE);
panelCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelCenter, c);
c.gridx = 4;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.BOTH;
panelRight = new JPanel();
panelRight.setBackground(Color.WHITE);
panelRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelRight, c);
addRandomComponents();
this.setVisible(true);
panel.setOpaque(true);
this.validate();
}
private void addRandomComponents() {
//add JButton to panelTopLeft
panelTopLeft.add(new JButton("TEST1"));
panelTopLeft.add(new JButton("TEST2"));
panelTopLeft.add(new JButton("TEST3"));
//add combo box to panelRight
panelRight.add(new JComboBox(Arrays.asList("TEST1", "TEST2").toArray()));
}
public static void main(String[] args) {
Main main = new Main();
}
}
Screenshots:
1) Before I add any components
2) After I add components (see the left top panel)
This happens because there is no preferred size set for the components. Once set, components do not grow larger than the size.
panelTopLeft.setPreferredSize(new Dimension(100, 0));
Complete fixed code.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Main extends JFrame {
// Drawing panels
private JPanel panelLeft;
private JPanel panelCenter;
private JPanel panelRight;
private JPanel panelTopCenter, panelTopLeft, panelTopRight;
private JPanel storyPanel;
// Layout
private GridBagLayout gridBadLayout;
private static final String DEFAULT_GAME_WORLD_NAME = "DefaultWorldName";
// Constructor
public Main() {
initializePanels();
}
/*
* Initializes look and feel of the window. No components should be
* initalized in this because controller would like to lazily initialize
* them once data is avialable.
*/
private void initializePanels() {
this.setTitle("World Maker");
this.setBounds(55, 5, 600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
gridBadLayout = new GridBagLayout();
panel.setLayout(gridBadLayout);
GridBagConstraints c = new GridBagConstraints();
this.add(panel);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.3;
c.weighty = 0.1;
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.BOTH;
panelTopLeft = new JPanel();
panelTopLeft.setBackground(Color.WHITE);
panelTopLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopLeft, c);
c.gridx = 1;
c.gridwidth = 3;
panelTopCenter = new JPanel();
panelTopCenter.setBackground(Color.WHITE);
panelTopCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopCenter, c);
c.gridx = 4;
c.gridwidth = 1;
panelTopRight = new JPanel();
panelTopRight.setBackground(Color.WHITE);
panelTopRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelTopRight, c);
c.gridx = 0;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
panelLeft = new JPanel();
panelLeft.setBackground(Color.WHITE);
panelLeft.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelLeft, c);
c.gridx = 1;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 3;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
panelCenter = new JPanel();
panelCenter.setBackground(Color.WHITE);
panelCenter.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelCenter, c);
c.gridx = 4;
c.gridy = 1;
c.gridheight = 3;
c.gridwidth = 1;
c.weightx = 0.3;
c.weighty = 1.0;
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.BOTH;
panelRight = new JPanel();
panelRight.setBackground(Color.WHITE);
panelRight.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panelRight, c);
addRandomComponents();
this.setVisible(true);
panel.setOpaque(true);
this.validate();
}
private void addRandomComponents() {
//add JButton to panelTopLeft
panelTopLeft.add(new JButton("TEST1"));
panelTopLeft.add(new JButton("TEST2"));
panelTopLeft.add(new JButton("TEST3"));
panelTopLeft.setPreferredSize(new Dimension(100, 0));
//add combo box to panelRight
panelRight.add(new JComboBox(Arrays.asList("TEST1", "TEST2").toArray()));
}
public static void main(String[] args) {
Main main = new Main();
}
}

Categories