• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

GridBagLayout. and GridBagConstraints..Help me!!

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a frame with List item and Label item . List item is located at row 0,0 with width = 1 and height = 3 (ocupies 3 rows) and Label at 1 ,1 with width = 4 and height = 1. But there is something wrong in my code.
<code>
import java.awt.*;
class Gb_Lay2
{
public static void main(String arr[])
{
Frame f = new Frame();
Panel p = new Panel();
GridBagLayout g = new GridBagLayout();
p.setLayout(g);
f.setSize(100,200);
List l = new List(3 , true);
l.add("apple");
l.add("orange");
l.add("abcdef");
l.add("cbsims");
l.add("monica");
l.add("Becky");
String str = "The quick brown fox";
Label lab = new Label(str , Label.LEFT);
GridBagConstraints glist = new GridBagConstraints();
GridBagConstraints glabel = new GridBagConstraints();
glist.gridx = 0 ;
glist.gridy = 0 ;
glist.gridwidth = 1 ;
glist.gridheight = 3 ;
glist.weightx = 100 ;
glist.weighty = 100;
glist.fill = GridBagConstraints.BOTH;
glabel.gridx = 0;
glabel.gridy = 1 ;
glabel.gridwidth = 4 ;
glabel.gridheight = 1 ;
glabel.weightx = 100;
glabel.weighty = 100;
glabel.fill = GridBagConstraints.BOTH;
p.add(l , glist);
p.add(lab , glabel);
f.add(p);
f.setVisible(true);

}
}
</code>
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to figure it out, I rearranged it a bit and added a little here and there. Here is 'a' solution... I am pretty new at this Java thang too! I am sure there are many other solutions that will work as well. Take a look and see if you can figure out what I did.
<PRE>
import java
.awt.*;
class Gb_lay2
{
public static void main(String arr[])
{
Frame f = new Frame();
Panel p = new Panel();
f.add(p);
GridBagLayout g = new GridBagLayout();
p.setLayout(g);
p.setBackground(Color.lightGray);

List listBox = new List(3 , true);
listBox.add("apple");
listBox.add("orange");
listBox.add("abcdef");
listBox.add("cbsims");
listBox.add("monica");
listBox.add("Becky");

String str = "The quick brown fox";
Label lab = new Label(str , Label.LEFT);
lab.setBackground(Color.white);

GridBagConstraints gbc = new GridBagConstraints();
//GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0; gbc.gridy = 0 ;
gbc.gridwidth = 1; gbc.gridheight = 1 ;
gbc.weightx = 1.0F ; gbc.weighty = 0.05F;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = gbc.NORTH;
p.add(listBox , gbc);

gbc.gridx = 0; gbc.gridy = 1;
gbc.gridwidth = 1 ; gbc.gridheight = 1 ;
gbc.weightx = 1.0F; gbc.weighty = 1.0F;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = gbc.NORTH;
p.add(lab , gbc);

f.setSize(100,200);
f.show();
}
}
</pre>
[This message has been edited by BillR (edited April 14, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic