I am tying to use Gridbag layout, but I cannot seem to get it working. here 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;
		//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;
		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.setSize (700,400);
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Chatroom cr = new Chatroom ();

	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.

Edited by sirlink99: n/a

6 Years
Discussion Span
Last Post by mKorbel

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 you have to look for



Edited by mKorbel: n/a


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?

Edited by Ezzaral: n/a

Votes + Comments
hmmm, right

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.