Solved

using GridBagLayout

Posted on 2000-03-06
9
269 Views
Last Modified: 2006-11-17
I want to build a form with the following structure:
lable        textfield
lable        textfield
lable        textfield
'
'
etc.
the number of rows changes dynamically according to the number of columns in a table.(each lable contains a name of a column).
how can I create such a form using this layout?
0
Comment
Question by:twg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 4

Accepted Solution

by:
kylar earned 50 total points
ID: 2588136
I created a general purpose panel that does a very similar thing. You would override JPanel, give it a GridBagLayout, then keep track of how many pairs of labels/textfields you have on it. then make a method called addComponents(JComponent, JComponent) or something like that, that would handle the adding automatically.


public int addComponents(JComponent inComp1, JComponent inComp2,int justifyconstraints){
      int retval = -1;
      int firstx;
      int secondx;
      
      if(justifyconstraints == LEFT){
            firstx = LEFT;
            secondx = LEFT;
      }else{
            if(justifyconstraints == RIGHT){
                  firstx = RIGHT;
                  secondx = RIGHT;
            }else{
                  firstx = RIGHT;
                  secondx = LEFT;
                  
            
            }

      
      }
      
      
              gc.gridy = (curComponentPairsOnMainPanel);
            
      gc.gridx = 0;
      
      gc.anchor = firstx;
      gc.weightx = 0.5;
      gc.fill = GridBagConstraints.NONE;
      gbLayout.setConstraints(inComp1, gc);
      mainPanel.add(inComp1);
      
      gc.gridx = 1;
      gc.anchor = secondx;
      gc.weightx = 0.5;
      gc.fill = GridBagConstraints.NONE;
      gbLayout.setConstraints(inComp2, gc);
      mainPanel.add(inComp2);
      
      
      curComponentPairsOnMainPanel++;
      curComponentPairsOnLeftPanel++;      
      
      

      if(retval == -1)retval = curComponentPairsOnMainPanel;
      
      return(retval);
}

Or something Similar

Kylar
0
 

Author Comment

by:twg
ID: 2590544
thanks kylar.
what is "justifyconstraints"?
0
 
LVL 4

Expert Comment

by:kylar
ID: 2592140
It's just an integer that represents where you want stuff to go, like:

static final int LEFT = GridBagConstraints.WEST;
static final int RIGHT = GridBagConstraints.EAST;

just to make it easier. instead of remembering that right justify is 'GridBagConstraints.EAST', I can just put RIGHT.  :)

Kylar
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:twg
ID: 2594664
thanks kylar. I did what you suggested and it worked but I have another problem: I put the panel in a frame. How can I control the size of the frame dynamically, I mean how can I change its size to be according to the number of components it contains and not a fixed size?  
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2596129
those floats u have to calculate in a Frame object constructor like

frame()
{
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize((int)(dim.width * 0.75),(int)(dim.height * 0.9));
}
0
 

Author Comment

by:twg
ID: 2596234
thanks mbormann but I don't understand
how this calculation depends on the number of components. As I see here it depends on the screen's size. If I change the number of components the frame stays at the same size but I want the frame to be smaller if I have less components and vice versa. how do I do that?
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2596303
u can't do it I think unless you change the size,
you calculate by trial and error as I did or you do it some other way which I don't know abt.

What abt calling frame.pack() at end of ur Frame's constructor?try it and see
0
 
LVL 4

Expert Comment

by:kylar
ID: 2605085
Yeah, I'd use the pack() command too.

Kylar
0
 
LVL 5

Expert Comment

by:mbormann
ID: 2607418
Cheers :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question