Solved

using GridBagLayout

Posted on 2000-03-06
9
265 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
Independent Software Vendors: 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
map related example 6 59
check java version using powershell 13 291
How to configure empty element in XML Document parser? 15 44
Notify sent to other threads in Java 9 41
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

763 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