Solved

using GridBagLayout

Posted on 2000-03-06
9
233 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

760 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

18 Experts available now in Live!

Get 1:1 Help Now