Solved

using GridBagLayout

Posted on 2000-03-06
9
243 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Impossible to extract MSI from new JAVA releases 2 51
Arrays.asList  VS  ArrayList 4 45
javap bin 2 30
servlet example 17 12
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now