I am stuck with java GridBagLayout. Here is only one issue left. I want the middle panel ("Testing Label 2") take all vertical spaces left.
How to achieve this ?
Thanks.
package com;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Test1 {
private void _panel1(JPanel panel){
panel.setBorder(new LineBorder(Color.RED));
panel.setLayout(new GridBagLayout());
JPanel panel1 = new JPanel();
panel1.add(new JLabel(" Testing Label 1 "));
panel1.setBorder(new TitledBorder("P1"));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTH;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 0.0;
panel.add(panel1, c);
JPanel panel2 = new JPanel();
panel2.add(new JLabel(" Testing Label 2"));
panel2.setBorder(new TitledBorder("P2"));
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTH;
c.gridx = 0;
c.gridy = 1;
c.weightx = 1.0;
c.weighty = 1.0;
panel.add(panel2, c);
JPanel panel3 = new JPanel();
panel3.setBorder(new TitledBorder("P3"));
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTH;
c.gridx = 0;
c.gridy = 2;
c.weightx = 1.0;
c.weighty = 0.0;
panel3.setLayout(new GridLayout(5,1));
for(int i=0; i<5; i++){
panel3.add(new JButton("button "+i));
}
panel.add(panel3, c);
}
public Test1(){
JFrame frame = new JFrame();
frame.setTitle("test 1");
frame.getContentPane().setLayout(new GridLayout(1,3));
JPanel panel1 = new JPanel();
_panel1(panel1);
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel3);
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
Test1 t = new Test1();
}
}
I just changed one line (27):
final JPanel panel2 = new JPanel();
panel2.add(new JLabel(" Testing Label 2"));
panel2.setBorder(new TitledBorder("P2"));
c = new GridBagConstraints();
// You have to use BOTH for the panel to take
// all vertical and horizontal space
c.fill = GridBagConstraints.BOTH;
You were setting the grid constraint for panel2 to HORIZONTAL.
Regards,
Consider changing the GridBagConstraint's fill field to BOTH for panel2:
panel2.setBorder(new TitledBorder("P2"));
c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH; // *****
c.anchor = GridBagConstraints.NORTH;
Either that or for a much simpler solution, just use a BorderLayout instead:
panel.setBorder(new LineBorder(Color.RED));
panel.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.add(new JLabel(" Testing Label 1 "));
panel1.setBorder(new TitledBorder("P1"));
panel.add(panel1, BorderLayout.NORTH);
JPanel panel2 = new JPanel();
panel2.add(new JLabel(" Testing Label 2"));
panel2.setBorder(new TitledBorder("P2"));
panel.add(panel2, BorderLayout.CENTER);
JPanel panel3 = new JPanel();
panel3.setBorder(new TitledBorder("P3"));
panel3.setLayout(new GridLayout(5, 1));
for (int i = 0; i < 5; i++) {
panel3.add(new JButton("button " + i));
}
panel.add(panel3, BorderLayout.SOUTH);
Related
I would like to add a JScrollpane into an existing Jpanel, I created a for loop for adding some buttons, but are not displayed. This is the code, but nothing is displayed inside the Jpanel
public class Main extends javax.swing.JFrame {
public Main() {
initComponents();
//EXISTING JPANEL
gridPanel = new JPanel(new GridLayout(0, 1));
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.add(gridPanel, BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
Avvio();
}
private void Avvio() {
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLUE));
for (int i = 0; i < 10; i++) {
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("Button 3");
c.gridx = 1;
c.gridy = 1;
pane.add(button, c);
}
}
I am using GridBagLayout to design the interface. The interface has one JTabbedPane set at north and fill both directions as I resize, and just below the JTabbedPane there are two JButtons.
What I want to achieve is putting these two buttons at east with using only GridBagLayout capabilities (without introducing an extra JPanel), but I failed to do so.
|-[tab]--------------------------|
|---------------------------------|
|---------------------------------|
|---------------------------------|
|---------------------------------|
|-------------[button][button]|
When I set the layout constraint for both buttons to WEST (not EAST). Both go west correctly!
c.anchor = GridBagConstraints.WEST;
But when I set the layout constraint for both buttons to east
c.anchor = GridBagConstraints.EAST;
One of them go to east and the other go to west!
To solve this issue I added a JPanel which hold both buttons and added this JPanel into the interface, and set its layout constraint to c.anchor = GridBagConstraints.EAST;. It works well.
But is it possible to build the same GridBagLayout interface without using an extra JPanel as a container?
The following two classes are in SSCCE format; the first one show the problem GridBagTest, and the other show the solution GridBagTest_solved that make use of extra JPanel
Problem class:
import javax.swing.*;
import java.awt.*;
public class GridBagTest extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JPanel panel_tab;
JButton btn_previous;
JButton btn_next;
private void create_and_layout() {
this.setLayout(new GridBagLayout());
tabbedPane = new JTabbedPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 150);
}
};
panel_tab = new JPanel();
tabbedPane.addTab("Tab 1", panel_tab);
btn_previous = new JButton("previous");
btn_next = new JButton(" next ");
GridBagConstraints c;
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.gridwidth = 2;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
this.add(tabbedPane, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(2, 0, 2, 2);
this.add(btn_previous, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(2, 0, 2, 2);
this.add(btn_next, c);
}
private void initGUI() {
create_and_layout();
frame = new JFrame("GridBagTest");
frame.getContentPane().add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new GridBagTest()::initGUI);
}
}
Solution class:
import javax.swing.*;
import java.awt.*;
public class GridBagTest_solved extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JPanel panel_tab;
JPanel panel_buttons;
JButton btn_previous;
JButton btn_next;
private void create_and_layout() {
this.setLayout(new GridBagLayout());
tabbedPane = new JTabbedPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 150);
}
};
panel_tab = new JPanel();
tabbedPane.addTab("Tab 1", panel_tab);
panel_buttons = new JPanel(new GridBagLayout());
btn_previous = new JButton("previous");
btn_next = new JButton(" next ");
GridBagConstraints c;
// Adding buttons to panel_buttons
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(2, 0, 2, 2);
panel_buttons.add(btn_previous, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(2, 0, 2, 2);
panel_buttons.add(btn_next, c);
// Adding tabbedPane & panel_buttons to this (GridBagTest_solved)
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
this.add(tabbedPane, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
this.add(panel_buttons, c);
}
private void initGUI() {
create_and_layout();
frame = new JFrame("GridBagTest_solved");
frame.getContentPane().add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new GridBagTest_solved()::initGUI);
}
}
If you only have the three components - the JTabbedPane and two JButtons, you should consider using a simpler layout than GridBag. Just use a BorderLayout for the main panel, placing the JTabbedPane in CENTER, and a JPanel in SOUTH that has a FlowLayout with alignment of TRAILING, then just add the two buttons to that JPanel.
I've seen other posts on this subject, but the solutions they found do not apply to me. I am setting a weighted value and using the c.fill = GridBagConstraints.BOTH constraints as well.
I'm including the whole GUI code I have, just in case my mistake is coming form something other than the GridBagLayout.
I want the scrollable text block on the right to expand the remaining space within the GUI and I have set all the variables that should be attributed to that and yet it still isn't working. What am I doing wrong?
My result:
import java.awt.*;
import javax.swing.*;
public class TestCode extends JFrame {
JTextArea textArea = new JTextArea ();
JComboBox <String> typeComboBox;
JTextField searchField;
JTextField fileField;
public TestCode(){
setTitle ("GUI Test");
setSize (600, 300);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
JScrollPane scrollPane = new JScrollPane(textArea);
JButton readButton = new JButton("Read File");
JButton displayButton = new JButton("Display");
JButton searchButton = new JButton("Search");
searchField = new JTextField(10);
fileField = new JTextField(15);
typeComboBox = new JComboBox <String> ();
typeComboBox.addItem("Index");
typeComboBox.addItem("Type");
typeComboBox.addItem("Name");
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
container.setPreferredSize(new Dimension(250, 100));
JPanel filePanel = new JPanel();
filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
filePanel.add(new JLabel("Source file", SwingConstants.LEFT));
JPanel filePanelTop = new JPanel();
filePanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
filePanelTop.add(fileField);
JPanel filePanelBottom = new JPanel();
filePanelBottom.setLayout(new FlowLayout(FlowLayout.RIGHT));
filePanelBottom.add(readButton);
filePanelBottom.add(displayButton);
filePanel.add(filePanelTop);
filePanel.add(filePanelBottom);
filePanel.setMaximumSize(filePanel.getPreferredSize());
filePanel.setBorder(BorderFactory.createTitledBorder("Import File"));
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS));
searchPanel.add(new JLabel("Search target", SwingConstants.LEFT));
JPanel searchPanelTop = new JPanel();
searchPanelTop.setLayout(new FlowLayout(FlowLayout.LEFT));
searchPanelTop.add(searchField);
searchPanelTop.add(typeComboBox);
searchPanel.add(searchPanelTop);
searchPanel.add(searchButton);
searchPanel.setMaximumSize(searchPanel.getPreferredSize());
searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects"));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
container.add(filePanel, c);
c.gridx = 0;
c.gridy = 1;
container.add(searchPanel, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTHWEST;
container.add(scrollPane, c);
add(container, BorderLayout.WEST);
validate();
} // end method toString
public static void main(String[] args){
TestCode run = new TestCode();
}
} // end class Treasure
//add(container, BorderLayout.WEST);
add(container);
The West contrains the components to their preferred width. The default is the CENTER which allows components to expand to fill the space available.
Also, the main structure of you code is wrong. You should be adding all the component to the frame first and then invoke:
frame.pack();
frame.setVisible(true);
Then there is no need for the validate().
I'm not exactly new to java (I've been using it for a year now) but this is my first go at swing. I'm trying to make a very simple chat client to learn both socket and swing at once. My question is "What must I do to align my panels correctly?". I've tried a lot of things (Though I don't have it in my code). Usually I work something like this out on my own, but I'm to the point I need to ask for help. Do I need to change the wieghtx, weighty? What I want the client to look like is something like this.
This is what it currently looks like.
Here is my code.
package com.client.core;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Window extends JFrame{
private int screenWidth = 800;
private int screenHeight = 600;
public Window(){
//Initial Setup
super("NAMEHERE - Chat Client Alpha v0.0.1");
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(screenWidth,screenHeight);
GridBagConstraints c = new GridBagConstraints();
//Main Panel
JPanel window = new JPanel();
window.setLayout(new GridBagLayout());
window.setBackground(Color.black);
//Panels
JPanel display = new JPanel();
JPanel chat = new JPanel();
chat.setLayout(new GridBagLayout());
JPanel users = new JPanel();
display.setBackground(Color.blue);
c.gridx = 0;
c.gridy = 0;
c.insets= new Insets(5,5,5,5);
window.add(display, c);
chat.setBackground(Color.red);
c.gridx = 0;
c.gridy = 3;
c.gridheight = 2;
c.gridwidth = 1;
c.insets= new Insets(5,5,5,5);
window.add(chat, c);
users.setBackground(Color.green);
c.gridx = 2;
c.gridy = 0;
c.insets= new Insets(5,5,5,5);
window.add(users, c);
//Buttons
//Text fields
JTextArea text = new JTextArea("DEREADFADSFEWFASDFSADFASDF");
c.gridx = 0;
c.gridy = 0;
chat.add(text);
JTextField input = new JTextField("type here to chat", 50);
c.gridx = 0;
c.gridy = 1;
c.insets= new Insets(5,5,5,5);
chat.add(input);
add(window);
}
static class ActLis implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
}
If you wanted something like this as an output :
You can take help from this code example, though you can remove the last ButtonPanel if you don't need that :
package to.uk.gagandeepbali.swing.messenger.gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JTextField;
public class ChatPanel extends JPanel
{
private JButton backButton;
private JButton exitButton;
private JButton sendButton;
private JTextPane chatPane;
private JTextPane namePane;
private JTextField chatField;
private GridBagConstraints gbc;
private final int GAP = 10;
private final int SMALLGAP = 1;
public ChatPanel()
{
gbc = new GridBagConstraints();
}
protected void createGUI()
{
setOpaque(true);
setBackground(Color.WHITE);
setLayout(new BorderLayout(5, 5));
JPanel centerPanel = new JPanel();
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.WHITE);
centerPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, 0, GAP));
centerPanel.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 5;
gbc.weightx = 0.8;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
chatPane = new JTextPane();
JScrollPane scrollerChat = new JScrollPane();
scrollerChat.setBorder(BorderFactory.createTitledBorder("Chat"));
scrollerChat.setViewportView(chatPane);
centerPanel.add(scrollerChat, gbc);
gbc.gridx = 5;
gbc.gridwidth = 2;
gbc.weightx = 0.2;
namePane = new JTextPane();
JScrollPane scrollerName = new JScrollPane(namePane);
scrollerName.setBorder(BorderFactory.createTitledBorder("Names"));
centerPanel.add(scrollerName, gbc);
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 5;
gbc.weightx = 0.8;
gbc.weighty = 0.1;
gbc.fill = GridBagConstraints.HORIZONTAL;
chatField = new JTextField();
chatField.setOpaque(true);
chatField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("")
, BorderFactory.createEmptyBorder(SMALLGAP, SMALLGAP, SMALLGAP, SMALLGAP)));
centerPanel.add(chatField, gbc);
gbc.gridx = 5;
gbc.gridwidth = 2;
gbc.weightx = 0.2;
sendButton = new JButton("Send");
sendButton.setBorder(BorderFactory.createTitledBorder(""));
centerPanel.add(sendButton, gbc);
JPanel bottomPanel = new JPanel();
bottomPanel.setOpaque(true);
bottomPanel.setBackground(Color.WHITE);
bottomPanel.setBorder(
BorderFactory.createTitledBorder(""));
bottomPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(true);
buttonPanel.setBackground(Color.WHITE);
buttonPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, 0, GAP));
backButton = new JButton("Back");
exitButton = new JButton("Exit");
buttonPanel.add(backButton);
buttonPanel.add(exitButton);
bottomPanel.add(buttonPanel, BorderLayout.CENTER);
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
public JTextPane getChatPane()
{
return chatPane;
}
public JTextPane getNamePane()
{
return namePane;
}
public JTextField getChatField()
{
return chatField;
}
public JButton getExitButton()
{
return exitButton;
}
public JButton getBackButton()
{
return backButton;
}
public JButton getSendButton()
{
return sendButton;
}
}
What you could do, and probably gives the desired result
JPanel somethingHere = ...;
JPanel chat = ...;
JPanel userList = ...;
JPanel leftPanel = new JPanel( new BorderLayout() );
leftPanel.add( somethingHere, BorderLayout.CENTER );
leftPanel.add( chat, BorderLayout.SOUTH );
JPanel total = new JPanel( new BorderLayout() );
total.add( leftPanel, BorderLayout.CENTER );
total.add( userList, BorderLayout.EAST );
Way simpler then messing with GridBagLayout
Here is what I came out with thus far. The red box is where I plan to add a simple 2D avatar interface with LWJGL.
Here is the code for it
package com.client.core;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Window extends JFrame{
private int screenWidth = 800;
private int screenHeight = 600;
public Window(){
//Initial Setup
super("NAMEHERE - Chat Client Alpha v0.0.1");
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(screenWidth,screenHeight);
//Main Panels
JPanel window = new JPanel(new BorderLayout());
JPanel center = new JPanel(new BorderLayout());
JPanel right = new JPanel(new BorderLayout());
//Panels
JPanel display = new JPanel( new BorderLayout());
display.setBackground(Color.red);
JPanel chat = new JPanel();
chat.setLayout(new BoxLayout(chat, BoxLayout.Y_AXIS));
chat.setBackground(Color.blue);
JPanel users = new JPanel(new BorderLayout());
users.setBackground(Color.green);
//TextFields
JTextArea chatBox = new JTextArea("Welcome to the chat!", 7,50);
chatBox.setEditable(false);
JTextField chatWrite = new JTextField();
JScrollPane userList = new JScrollPane();
JTextField userSearch = new JTextField(10);
userList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
users.add(userList);
users.add(userSearch, BorderLayout.NORTH);
chat.add(chatBox);
chat.add(chatWrite);
chat.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
//Menu bar
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem ipconnect = new JMenuItem("Connect to IP");
file.add(ipconnect);
file.add(exit);
menu.add(file);
//Main window adding
right.add(users);
center.add(display, BorderLayout.CENTER);
center.add(chat, BorderLayout.SOUTH);
window.add(center, BorderLayout.CENTER);
window.add(right, BorderLayout.EAST);
window.add(menu, BorderLayout.NORTH);
add(window);
//Listeners
chatWrite.addKeyListener(new KeyLis());
ipconnect.addActionListener(new ActLis());
exit.addActionListener(new ActLis());
}
static class KeyLis implements KeyListener{
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
System.out.println("Message recieved.");
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
static class ActLis implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "Exit"){
System.exit(0);
} else if(e.getActionCommand() == "Connect to IP"){
System.out.println("Connecting....");
JFrame frameip = new JFrame();
JPanel panelip = new JPanel();
JButton buttonip = new JButton("Hello");
frameip.add(panelip);
panelip.add(buttonip);
JDialog ippop = new JDialog(frameip, "Enter IP", false);
}
}
}
}
I had to build a similar layout using a GridBagLayout. The code below shows how I achieved it.
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagLayoutTest {
public GridBagLayoutTest() {
JFrame jframe = new JFrame();
jframe.setLayout(new GridBagLayout());
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(800, 600);
jframe.setVisible(true);
// Left
JPanel leftPanel = new JPanel(new GridBagLayout());
leftPanel.setBackground(Color.YELLOW);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weightx = .7f;
gridBagConstraints.weighty = 1f;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
jframe.add(leftPanel, gridBagConstraints);
JPanel leftTopPanel = new JPanel(new FlowLayout());
leftTopPanel.setBackground(Color.RED);
GridBagConstraints gridBagConstraints0 = new GridBagConstraints();
gridBagConstraints0.fill = GridBagConstraints.BOTH;
gridBagConstraints0.weightx = 1f;
gridBagConstraints0.weighty = .7f;
gridBagConstraints0.gridx = 0;
gridBagConstraints0.gridy = 0;
leftPanel.add(leftTopPanel, gridBagConstraints0);
JPanel leftMiddlePanel = new JPanel(new FlowLayout());
leftMiddlePanel.setBackground(Color.BLACK);
gridBagConstraints0 = new GridBagConstraints();
gridBagConstraints0.fill = GridBagConstraints.BOTH;
gridBagConstraints0.weightx = 1f;
gridBagConstraints0.weighty = .2f;
gridBagConstraints0.gridx = 0;
gridBagConstraints0.gridy = 1;
leftPanel.add(leftMiddlePanel, gridBagConstraints0);
JPanel leftBottomBottomPanel = new JPanel(new FlowLayout());
leftBottomBottomPanel.setBackground(Color.PINK);
gridBagConstraints0 = new GridBagConstraints();
gridBagConstraints0.fill = GridBagConstraints.BOTH;
gridBagConstraints0.weightx = 1f;
gridBagConstraints0.weighty = .1f;
gridBagConstraints0.gridx = 0;
gridBagConstraints0.gridy = 2;
leftPanel.add(leftBottomBottomPanel, gridBagConstraints0);
// Right
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.GREEN);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weightx = .3f;
gridBagConstraints.weighty = 1f;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
jframe.add(rightPanel, gridBagConstraints);
}
public static void main (String args[]) {
new GridBagLayoutTest();
}
I need to display a single component within a JPanel, and I want to keep that component in bottom right corner at all times. I tried to do it with GridBagLayout:
val infoArea = new TextArea {
text = "Hello!"
border = Swing.EmptyBorder(30)
background = Color.RED
editable = false
}
val p = new JPanel
p.setLayout(new GridBagLayout)
val c = new GridBagConstraints
c.gridx = 0
c.gridy = 0
c.anchor = GridBagConstraints.LAST_LINE_END
p.add(infoArea.peer,c)
val f = new JFrame
f.setContentPane(p)
f.setVisible(true)
But the text area is at the center for some reason:
What am I doing wrong here?
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
public class LayoutDemo {
private static void createAndShowGui() {
JLabel label = new JLabel("Hello");
label.setOpaque(true);
label.setBackground(Color.red);
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(label, BorderLayout.LINE_END);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
mainPanel.setPreferredSize(new Dimension(400, 400));
JFrame frame = new JFrame("LayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
frame.add(Box.createGlue(), gbc);
final JTextArea textArea = new JTextArea("SE");
textArea.setPreferredSize(new Dimension(50, 50));
textArea.setOpaque(true);
textArea.setBackground(Color.RED);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
frame.add(textArea, gbc);
frame.setSize(640, 480);
frame.setVisible(true);
...if you realy want to use GridBagLayout
you have to put a dummy (use Box.createGlue() to make a dummy component) component on gridx = 0 and gridy = 0 and the component you want to put at the bottom right at gridx = 1, gridy = 1.like this