Grid

How can i put login2 just after login1..

what happened in this code is jtextarea size is large so if i want to place some other objects (eg : label ,textfield ,etc ) i can place only after jtextarea...and i can also feel a long gap in between them....

in this code you can see there is a gap between login1 and login2....how can i avoid that spaces in between them....

import java.awt.*;

import javax.swing.*;

publicclass grid1

{

publicstaticvoid main(String args[])

{

JFrame frame =new JFrame("Haila");

GridBagLayout gblayout =new GridBagLayout();

GridBagConstraints gbc =new GridBagConstraints();

//comments 1\5

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 1;

gbc.gridy = 5;

JLabel comments =new JLabel("Comments : ");

gblayout.setConstraints(comments,gbc);

//textarea 1\7

gbc.anchor = GridBagConstraints.WEST;

gbc.gridx = 1;

gbc.gridy = 7;

JTextArea textarea =new JTextArea(20,20);

gblayout.setConstraints(textarea,gbc);

//login1 3\7

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 3;

gbc.gridy = 7;

JLabel login1 =new JLabel("Login 1 ");

gblayout.setConstraints(login1,gbc);

//log1 textfield 4\7

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 4;

gbc.gridy = 7;

JTextField log1 =new JTextField(10);

gblayout.setConstraints(log1,gbc);

//login2 3\8

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 3;

gbc.gridy = 8;

JLabel login2 =new JLabel("Login 2 ");

gblayout.setConstraints(login2,gbc);

//log2 textfield 4\8

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 4;

gbc.gridy = 8;

JTextField log2 =new JTextField(10);

gblayout.setConstraints(log2,gbc);

//logpass1 label 3\9

gbc.anchor = GridBagConstraints.NORTH;

gbc.gridx = 3;

gbc.gridy = 9;

JLabel logpass1 =new JLabel("Login 1 Password ");

gblayout.setConstraints(logpass1,gbc);

//pass1 textfield 4\9

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 4;

gbc.gridy = 9;

JTextField pass1 =new JTextField(10);

gblayout.setConstraints(pass1,gbc);

//logpass2 label 3\10

gbc.anchor = GridBagConstraints.NORTH;

gbc.gridx = 3;

gbc.gridy = 10;

JLabel logpass2 =new JLabel("Login 2 Password ");

gblayout.setConstraints(logpass2,gbc);

//pass2 textfield 4\10

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 4;

gbc.gridy = 10;

JTextField pass2 =new JTextField(10);

gblayout.setConstraints(pass2,gbc);

//submit 1\9

gbc.fill = GridBagConstraints.BOTH;

gbc.anchor = GridBagConstraints.SOUTHWEST;

gbc.gridx = 1;

gbc.gridy = 9;

JButton submit =new JButton("Submit");

gblayout.setConstraints(submit,gbc);

//discard 1\10

gbc.fill = GridBagConstraints.BOTH;

gbc.anchor = GridBagConstraints.SOUTHEAST;

gbc.gridx = 1;

gbc.gridy = 10;

JButton discard =new JButton("Discard");

gblayout.setConstraints(discard,gbc);

JPanel panel =new JPanel();

panel.setLayout(gblayout);

panel.add(comments);

panel.add(textarea);

panel.add(login1);

panel.add(log1);

panel.add(login2);

panel.add(log2);

panel.add(logpass1);

panel.add(pass1);

panel.add(logpass2);

panel.add(pass2);

panel.add(submit);

panel.add(discard);

frame.getContentPane().add(panel);

frame.setDefaultCloseOperation(3);

frame.setSize(550,550);

frame.setResizable(false);

frame.setLocation(200,200);

frame.setVisible(true);

}

}

[5324 byte] By [Reona] at [2007-11-27 3:23:28]
# 1

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.htmlHow to Use Layout Managers[/url]. It explains how all the constraints work.

However, I find it is usually easier to use nested Layout Managers to get your desired effect. I don't understand you exact requirement so I can't give you specific help. But maybe start with a BorderLayout. Create a panel with some components on it and add that panel to the West. Then create another component with components on the East or South. Experiment.

camickra at 2007-7-12 8:26:14 > top of Java-index,Desktop,Core GUI APIs...
# 2

what actually i mean by the code is in first column i created textarea (20,20)...

so that textarea cell became large

for example if gridx = 1 and griy =1 that contains textarea ..

if i put any label ie gridx = 2 and gridy = 1 that will take as much as space that textarea holds...

if i want to put another label ie gridx = 2 and gridy = 2 we can feel a long gap between them ..the reason is that in gridx = 1 and griy = 1 there holds so much space ...

how can i fill the gap in between them..

Did u now understand what i am trying to d o ?

Thanks...

Reona at 2007-7-12 8:26:14 > top of Java-index,Desktop,Core GUI APIs...
# 3

I'm not sure, but this might be how you want it done.

To add components to the right of your JTextArea, just let your

JTextArea span several rows

import java.awt.*;

import javax.swing.*;

public class grid1

{

public static void main(String args[])

{

JFrame frame = new JFrame("Haila");

GridBagLayout gblayout = new GridBagLayout();

GridBagConstraints gbc;

Insets insets;

//comments 1\5

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 0;

gbc.gridy = 0;

JLabel comments = new JLabel("Comments : ");

gblayout.setConstraints(comments,gbc);

//textarea 1\7

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.WEST;

gbc.gridx = 0;

gbc.gridy = 1;

gbc.gridheight = 4;// ROWS TO SPAN

JTextArea textarea = new JTextArea(20,20);

gblayout.setConstraints(textarea,gbc);

//login1 3\7

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 1;

gbc.gridy = 1;

gbc.insets = new Insets(10,10,0,10);

JLabel login1 = new JLabel("Login 1 ");

gblayout.setConstraints(login1,gbc);

//log1 textfield 4\7

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 2;

gbc.gridy = 1;

gbc.insets = new Insets(10,10,0,10);

JTextField log1 = new JTextField(10);

gblayout.setConstraints(log1,gbc);

//login2 3\8

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 1;

gbc.gridy = 2;

gbc.insets = new Insets(10,10,0,10);

JLabel login2 = new JLabel("Login 2 ");

gblayout.setConstraints(login2,gbc);

//log2 textfield 4\8

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 2;

gbc.gridy = 2;

gbc.insets = new Insets(10,10,0,10);

JTextField log2 = new JTextField(10);

gblayout.setConstraints(log2,gbc);

//logpass1 label 3\9

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTH;

gbc.gridx = 1;

gbc.gridy = 3;

gbc.insets = new Insets(10,10,0,10);

JLabel logpass1 = new JLabel("Login 1 Password ");

gblayout.setConstraints(logpass1,gbc);

//pass1 textfield 4\9

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 2;

gbc.gridy = 3;

gbc.insets = new Insets(10,10,0,10);

JTextField pass1 = new JTextField(10);

gblayout.setConstraints(pass1,gbc);

//logpass2 label 3\10

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTH;

gbc.gridx = 1;

gbc.gridy = 4;

gbc.insets = new Insets(10,10,0,10);

JLabel logpass2 = new JLabel("Login 2 Password ");

gblayout.setConstraints(logpass2,gbc);

//pass2 textfield 4\10

gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.NORTHWEST;

gbc.gridx = 2;

gbc.gridy = 4;

gbc.insets = new Insets(10,10,0,10);

JTextField pass2 = new JTextField(10);

gblayout.setConstraints(pass2,gbc);

//submit 1\9

gbc = new GridBagConstraints();

gbc.fill = GridBagConstraints.BOTH;

gbc.anchor = GridBagConstraints.SOUTHWEST;

gbc.gridx = 0;

gbc.gridy = 5;

JButton submit = new JButton("Submit");

gblayout.setConstraints(submit,gbc);

//discard 1\10

gbc = new GridBagConstraints();

gbc.fill = GridBagConstraints.BOTH;

gbc.anchor = GridBagConstraints.SOUTHEAST;

gbc.gridx = 0;

gbc.gridy = 6;

JButton discard = new JButton("Discard");

gblayout.setConstraints(discard,gbc);

JPanel panel = new JPanel();

panel.setLayout(gblayout);

panel.add(comments);

panel.add(textarea);

panel.add(login1);

panel.add(log1);

panel.add(login2);

panel.add(log2);

panel.add(logpass1);

panel.add(pass1);

panel.add(logpass2);

panel.add(pass2);

panel.add(submit);

panel.add(discard);

frame.getContentPane().add(panel);

frame.setDefaultCloseOperation(3);

frame.pack();

frame.setSize(550,550);

frame.setResizable(false);

frame.setLocation(200,200);

frame.setVisible(true);

}

}

Carsten_Fa at 2007-7-12 8:26:14 > top of Java-index,Desktop,Core GUI APIs...
# 4
First of all thanx Carsten_F ..I was trying and trying it for so many days ..i check rowspan and colspan from documentation.. but i struck there....Thanx
Reona at 2007-7-12 8:26:14 > top of Java-index,Desktop,Core GUI APIs...
# 5
:)I forgot to tell that you must create a new instance of GridBagConstraint for each component you add to the frameCarsten
Carsten_Fa at 2007-7-12 8:26:14 > top of Java-index,Desktop,Core GUI APIs...