GridBagLayout elements behaviour when resizing window - java

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.

Related

GridBagLayout Pixel Offset from Anchor Position

How do I remove the pixel offset from the SW-Button corner?
Some sizes will work, while others won't I'm just looking for a reliable way to always have the button be in the very bottom-right corner.
I've tried different values for weightx/y and ipadx/y which fix it for specific sizes but just create the issue again on others.
Is this just a general problem with GridBagLayout or am I missing something?
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestFrame extends JFrame {
public static void main(String[] args) {
new TestFrame();
}
public TestFrame() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(501, 500);
this.getContentPane().setBackground(Color.MAGENTA);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
JButton nw = new JButton("NW");
this.add(nw, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.SOUTHEAST;
JButton se = new JButton("SW");
this.add(se, gbc);
this.setVisible(true);
}
}

Why JTextArea is small?

I try to make a simple app in SWING: using BorderLayout layout on the JFrame, i put on SOUTH an executing button, on WEST a panel that contains a combobox and on EAST a panel that contains 2 JTextAreas. The problem is, both JTextArea are damn small. Any help and explanation will be welcomed.
This is the code for the panel with the 2 text areas
package cipher;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
class TextPanel extends JPanel {
private JTextArea inputArea, outputArea;
public TextPanel() {
initSize();
initTextArea();
initBorder();
initLayout();
packing();
}
private void packing() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
add(inputArea,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
add(outputArea,gbc);
}
private void initBorder() {
Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border inner = BorderFactory.createTitledBorder("Text");
setBorder(BorderFactory.createCompoundBorder(outer,inner));
}
private void initLayout() {
setLayout(new GridBagLayout());
}
private void initTextArea() {
inputArea = new JTextArea();
inputArea.setPreferredSize(new Dimension(385,400));
outputArea = new JTextArea();
outputArea.setPreferredSize(new Dimension(385,400));
}
private void initSize() {
Dimension size = getPreferredSize();
size.width = 390;
setPreferredSize(size);
}
}
I've tried using setSize(x,y) but without success. I've tried using JTextArea(rows,columns) but without success. I've used even setPreferredSize with a Dimension but no succeed.
The probable cause of your issue is the container area is smaller than the preferred size of the text area, GridBagLayout will then default to the minimum size instead.
This is a good example of why you should avoid setting these properties directly and instead make use of the layout manager and the components properties.
To start with, make use of the JTextArea's column and rows properties. This will make a better "guess" at the amount of space it needs to display text to fit within these confines.
Second, use GridBagConstraints#fill to override GridBagLayout and force it to make use of the available space
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TextPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TextPanel extends JPanel {
private JTextArea inputArea, outputArea;
public TextPanel() {
initTextArea();
initBorder();
initLayout();
packing();
}
private void packing() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(inputArea, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(outputArea, gbc);
}
private void initBorder() {
Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border inner = BorderFactory.createTitledBorder("Text");
setBorder(BorderFactory.createCompoundBorder(outer, inner));
}
private void initLayout() {
setLayout(new GridBagLayout());
}
private void initTextArea() {
// The borders are just here so you can see the different text areas
inputArea = new JTextArea(10, 20);
inputArea.setBorder(new LineBorder(Color.BLACK));
outputArea = new JTextArea(10, 20);
outputArea.setBorder(new LineBorder(Color.BLACK));
}
}
}
I'd also change...
inputArea = new JTextArea(10, 20);
inputArea.setBorder(new LineBorder(Color.BLACK));
outputArea = new JTextArea(10, 20);
outputArea.setBorder(new LineBorder(Color.BLACK));
and make use of JScrollPanes instead of LineBorder

Debugging GridBagLayout - components clumped to center

I'm trying to get the following layout
Label..................NumericField
Label2................NumericField
Label3333..........NumericField
Basically the (.) dots would be empty space. I had tried GridBagLayout with making the label's gridwidth as 5 and the NumericField's gridwidth as 1. I'm posting the code below. But I don't see the desired result and I see all components aligned at the center instead of Labels being at left border and NFs being at right border.
For Labels:
GridBagConstraints localC = new GridBagConstraints();
localC.anchor = GridBagConstraints.FIRST_LINE_START;
//localC.fill = GridBagConstraints.HORIZONTAL;
localC.weightx = 1.0;
localC.weighty = 1.0;
localC.gridx = 0;
localC.gridy = 0;
localC.gridheight = 1;
localC.gridwidth = 5;
localC.insets = new Insets(0, 0, 0, 0);
For NumericFields
localC.anchor = GridBagConstraints.RELATIVE;
localC.weightx = 0.5;
localC.weighty = 0.5;
localC.gridx = 1;
localC.gridy = 0;
localC.gridheight = 1;
localC.gridwidth = 1;
I'm new to JAVA and struggling with layouts generally.
Add a value to the Insets right property, which will add that number of pixels to the right side of the column. You could also use GridBagConstraints#anchor set to GridBagConstraints.WEST, which will force the components in the columns to be positioned on the left hand side of the "column", this ensures that when a component in the column is wider, they won't be laid out in the middle of the resulting space.
gridwidth determines how a given cell will span across multiple columns, but if there are no other components in the resulting columns, they are discard (defaulted to 0), so in your layout, it's meaningless.
See How to Use GridBagLayout more details
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0, 0, 0, 12);
add(new JLabel("Label"), gbc);
gbc.gridy++;
add(new JLabel("Label2"), gbc);
gbc.gridy++;
add(new JLabel("Label3333"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(0, 0, 0, 0);
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
gbc.gridy++;
add(new JTextField(10), gbc);
}
}
}

GridBagLayout Alignment Issue with Button expanding Column Width

I am trying to create an interface with some JLabels icons ( box icons )and an exit button. The thing is that, when I place the button under the labels, they split, according to the button's position like this:
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
c.gridy = 0;
c.weighty = 1;
add(new JLabel(" "), c);
for(int i = 0; i < label.length; i++){ // Adding the JLabels
c.fill = GridBagConstraints.NONE;
c.weighty = 0;
c.gridy = 1;
c.gridx++;
add(label[i], c);
}
// Adding the button
c.gridy ++;
c.gridx = 2; // Changing the value of gridx, will move the button to that location and it will split another jlabel.
c.weighty = 0.09;
add(eButton, c);
revalidate();
repaint();
GridBagLayout works a lot like a grid paper, it has rows and columns, components are placed within those cells; you can specify how additional space is distributed and components are aligned within those cells.
Each row and column is sized based on the layout requirements of the other components in that row/column, this means that a component may have more space than it needs, because some other component in the same row/column needs more space to be laid out.
You can also allow a component to span across multiple rows/columns, allowing to occupy more space.
So, basically, what you need to do is tell the button that it can occupy all the columns for it's row (or at least the 5 columns which is occupied by the labels). You will need to then tell the button that it needs to be aligned to the center of it's area.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL; // Adding space before the labels
c.gridy = 0;
c.weighty = 1;
for (int i = 0; i < 5; i++) { // Adding the JLabels
c.fill = GridBagConstraints.NONE;
c.weighty = 0;
c.gridy = 1;
c.gridx++;
c.insets = new Insets(4, 4, 4, 4);
JLabel label = new JLabel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(20, 20);
}
};
label.setBorder(new LineBorder(Color.DARK_GRAY));
add(label, c);
}
c.gridy++;
c.gridx = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.anchor = GridBagConstraints.NORTH;
// c.weighty = 0.09;
add(new JButton("Exit"), c);
}
}
}
Take a closer look at How to Use GridBagLayout for more details

Java Swing - realising a layout with LayeredPane

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

Categories