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;
		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.

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

http://www.java2s.com/Tutorial/Java/0240__Swing/1480__GridBagConstraints.htm

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.