Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using GridBagLayout

Posted on 2000-03-06
9
Medium Priority
?
278 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 150 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

916 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