I am tying to use Gridbag layout, but I cannot seem to get it working. is the plan. and here is the code:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.nio.*;

public class Chatroom implements ActionListener {

	public Chatroom (){
		
		
		
		
		
		
		JFrame frame = new JFrame ("Warrior Productions Chat");
		JPanel pane = new JPanel ();
		
		pane.setLayout(new GridBagLayout ());
		GridBagConstraints c = new GridBagConstraints();
		
		//JPanel chatArea = new JPanel ();
		//JPanel messageArea = new JPanel();
		//JPanel onArea = new JPanel();
		
		JTextField textArea = new JTextField (60);     // enter message
		c.gridx = 10;
		c.gridy = 55;
		c.gridwidth = 60; c.gridheight = 5;
		pane.add(textArea,c);
		//JPanel sendButtonArea = new JPanel ();
		
		JButton send = new JButton ("Send Message");send.setActionCommand ("send");send.addActionListener(this);
		c.gridx = 75;
		c.gridy = 55;
		c.gridwidth = 15; c.gridheight = 5;
		pane.add (send,c);
		JTextArea users = new JTextArea ("");    // SET THE USERS AKA TEXTAREA 2		c.gridx = 75;
		c.gridy = 15;
		c.gridwidth = 15;
		c.gridheight = 35;
		pane.add(users,c);
		JTextArea messages = new JTextArea (35,60); // see messages TEXTAREA 1		c.gridx = 10; c.gridy = 15; c.gridwidth = 60; c.gridheight = 35;
		pane.add (messages, c);
		
		
		
		//chatArea.add(messages);onArea.add(users);messageArea.add(textArea); sendButtonArea.add(send);
		
		//pane.add (chatArea); pane.add (onArea); pane.add(messageArea);pane.add (sendButtonArea);
		
		
		frame.add(pane);
		
		
		
		frame.setSize (700,400);
		frame.setResizable(false);
		frame.setVisible(true);
	}
	
	
	
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Chatroom cr = new Chatroom ();
	}






	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if ("send".equals(e.getActionCommand())){
			// send message to other person
		}
		
		
	}

}

What am I doing wrong. BTW the increments on both axis in the plan are 10 each time.

Thanks for any help.

Dani AI

Generated

Short, practical guidance and a small recipe to get the layout you sketched working.

The core problem is the grid coordinates: gridx and gridy are cell indices (0-based), not pixels — as pointed out. GridBagLayout does not force a fixed row/column count; you design a small logical grid and place components in those cells, using gridwidth/gridheight to span multiple cells. ’s suggestion to consider simpler managers is valid: a combination of BorderLayout/JSplitPane or nested JPanels often gets the job done with less fuss than a large GridBag grid.

Suggested, reliable approach that matches the usual chat layout (messages | users on the right, input + send on the bottom):

  • Treat the main area as a 3-column by 2-row logical grid.
  • Messages: left area, span two columns, set weightx=1, weighty=1, fill=BOTH, wrap the JTextArea in a JScrollPane.
  • Users: right column, weightx=0, weighty=1, fill=VERTICAL (also in a JScrollPane).
  • Bottom: input field spanning the left two columns (weighty=0, fill=HORIZONTAL) and the Send button in the right column.

Practical notes and common fixes:

  • Set insets on the GridBagConstraints for spacing.
  • Use GridBagConstraints.REMAINDER or explicit small indices (0,1,2) rather than huge numbers.
  • Call frame.pack() (preferred) and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE). Build the UI on the Event Dispatch Thread (SwingUtilities.invokeLater).
  • Avoid using JTextArea without a JScrollPane — otherwise the component sizing will be unpredictable.
  • weightx/weighty only affects how extra space is distributed on resize; setResizable(false) prevents that behavior.

Compact example of the placement pattern:

c.insets = new Insets(4,4,4,4);

c.gridx=0; c.gridy=0; c.gridwidth=2; c.fill=BOTH; c.weightx=1; c.weighty=1;
pane.add(new JScrollPane(messages), c);

c.gridx=2; c.gridy=0; c.gridwidth=1; c.fill=VERTICAL; c.weightx=0; c.weighty=1;
pane.add(new JScrollPane(users), c);

c.gridx=0; c.gridy=1; c.gridwidth=2; c.fill=HORIZONTAL; c.weighty=0;
pane.add(textField, c);

c.gridx=2; c.gridy=1; c.gridwidth=1; c.weightx=0;
pane.add(sendButton, c);

For simpler maintenance, consider the BorderLayout/JSplitPane route mentioned by when the UI is conceptually split into large areas rather than a strict grid.

Recommended Answers

All 5 Replies

are you sure that that must be done by GridBag, because there are my questions

1/ JComponent placed at botton should be risiziable or not
2/ JComponent placed at top should be risiziable or not

add 1)

JPanelNo0.setPrefferedSize(new Dimension(x,y))
add JPanelNo0 to JFrame with BorderLaoyut.North

add JComponent1 to JPanelNo1 with BorderLaoyut.Center (without any definitions for PrefferedSize)
JComponent2.setPrefferedSize(new Dimension(x,y))
add JComponent2 to JPanelNo1 with BorderLaoyut.EAST

add JPanelNo1 to JFrame with BorderLaoyut.Center (without any definitions for PrefferedSize)

add JComponent3 to JPanelNo2 with BorderLaoyut.Center (without any definitions for PrefferedSize)
JComponent4.setPrefferedSize(new Dimension(x,y))
add JComponent4 to JPanelNo2 with BorderLaoyut.EAST

JPanelNo2.setPrefferedSize(new Dimension(x,y))
add JPanelNo2 to JFrame with BorderLaoyut.South

add 2)

vice versa

GridBagLayout

GridBagLayout you have to look for

GridBagConstraints

c.gridy = 55; You do realize that gridx and gridy are row and column index values and not pixels? Did you really want this component in column 56?

commented: hmmm, right +7

I know that now. How do I find the number of columns and rows in my JFrame then?

Thanks for the help

You don't find, you decide. It is a grid of component cells that you're creating. Read the tutorial on Grid Bag that mKorbel linked above.

just advice:

1/ create manual grid by using JLabel
2/ on both directions H + V

thenafter you can place your JComponents where you wants

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.