Java Forum / GUI / February 2005
GridBagLayout ?
Tuncay Altun - 18 Feb 2005 23:27 GMT Hi,
I hope some can help me to give the last lesson in learning GridBagLayout. Because I'm stuck...
Below, I have my stucking example.
What I need is a top panel with two title panels inside like the example. Each title panel should be shown with the same size no matter how many components each title panel contain. For instance, if the left title panel contains 5 components and the right title panel contains 3 components then both title panels should be shown in same hight and width.
I also need each component pair (e.g. label and comboBox) for each row to be aligned.
Thanks in advance...
//Mike
public class GridBag extends JFrame {
public GridBag() { super("Learn GridBagLayout"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(800,600); this.getContentPane().add(createContentPane(),BorderLayout.WEST); this.pack(); this.setVisible(true); }
private JPanel createContentPane() {
JPanel leftPanel = new JPanel(new GridBagLayout()); leftPanel.setBorder(BorderFactory.createTitledBorder("TitleBorder left")); createLeftPanel(leftPanel);
JPanel rightPanel = new JPanel(new GridBagLayout()); rightPanel.setBorder(BorderFactory.createTitledBorder("TitleBorder right")); createRightPanel(rightPanel);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(4,4,4,4),0,0);
panel.add(leftPanel,gbc);
gbc = new GridBagConstraints(1,0,1,1,1.0,1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(4,4,4,4),0,0); panel.add(rightPanel,gbc);
return panel; }
private void createLeftPanel(JPanel panel) {
GridBagConstraints gbc = new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(4,4,4,4),0,0);
panel.add(new JLabel("Name:"),gbc);
gbc = new GridBagConstraints(1,0,1,1,1.0,1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(4,4,4,4),0,0);
String[] nameItems = {"Mike","Michael","Lee"}; panel.add(new JComboBox(nameItems),gbc);
gbc = new GridBagConstraints(0,1,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(4,4,4,4),0,0);
panel.add(new JLabel("Age:"),gbc);
gbc = new GridBagConstraints(1,1,1,1,1.0,1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(4,4,4,4),0,0);
String[] ageItems = {"10","20","30"}; panel.add(new JComboBox(ageItems),gbc);
gbc = new GridBagConstraints(1,2,1,1,1.0,1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.VERTICAL, new Insets(4,4,4,4),0,0);
JButton button = new JButton("Do it!"); panel.add(button,gbc);
}
private void createRightPanel(JPanel panel) {
GridBagConstraints gbc = new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(4,4,4,4),0,0);
panel.add(new JLabel("Counter:"),gbc);
gbc = new GridBagConstraints(1,0,1,1,1.0,1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.VERTICAL, new Insets(4,4,4,4),0,0);
JTextField textField = new JTextField(5); textField.setEditable(false); textField.setText("123456"); panel.add(textField,gbc);
gbc = new GridBagConstraints(1,1,1,1,1.0,1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.VERTICAL, new Insets(4,4,4,4),0,0);
JButton button = new JButton("Do it!"); panel.add(button,gbc); }
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new GridBag(); } }); } }
Knute Johnson - 19 Feb 2005 02:10 GMT > Hi, > [quoted text clipped - 17 lines] > > //Mike Mike:
You only need one instance of GridBagConstraints. To get a component to stay a certain size, set its preferred size with setPreferredSize() if you are using 1.5, or override the component's getPreferredSize() with the size you want it to be. Below is an example of what I think you want and some extras too!
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class test2 extends JFrame { public test2() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints(); c.gridx = c.gridy = 0; c.insets = new Insets(4,4,4,4); c.fill = GridBagConstraints.BOTH;
JPanel p1 = new JPanel(); p1.setLayout(new GridBagLayout()); p1.setBorder(BorderFactory.createTitledBorder("Blue Panel")); p1.setPreferredSize(new Dimension(320,240)); p1.setBackground(Color.BLUE); add(p1,c);
++c.gridx; JPanel p2 = new JPanel(); p2.setLayout(new GridBagLayout()); p2.setPreferredSize(new Dimension(320,240)); p2.setBackground(Color.RED); add(p2,c);
String[] ord = {"One","Two","Three","Four","Five","Six"}; JLabel[] l = new JLabel[ord.length]; for (int i=0; i<ord.length; i++) { l[i] = new JLabel(ord[i]); l[i].setOpaque(true); }
c.gridx = c.gridy = 0; p1.add(l[0],c); ++c.gridy; p1.add(l[1],c);
c.weightx = 0.2; c.weighty = 0.9; c.gridx = c.gridy = 0; p2.add(l[2],c); ++c.gridx; c.weightx = 0.8; p2.add(l[3],c);
c.gridx = 0; ++c.gridy; c.gridwidth = 2; c.weighty = 0.1; p2.add(l[4],c);
setSize(800,600); setVisible(true); }
public static void main(String[] args) { new test2(); } }
 Signature Knute Johnson email s/nospam/knute/
Tuncay Altun - 19 Feb 2005 05:04 GMT Thanks,
One more question :-)
Both panels are placed in center.
I want both panels to be left aligned in the top panel. ??
Because I'm putting the top panel as left splitpane.
Thanks.
// Mike
> Hi, > [quoted text clipped - 137 lines] > } > } dar7yl - 19 Feb 2005 19:33 GMT > Hi, > [quoted text clipped - 15 lines] > > Thanks in advance... I would make a nested display, perhaps consisting of a horizontal box containing two distinct gridbag components. That way, both layouts can be independant of each other, and you can size the two components appropriately.
You can probably guess that I'm not a big fan of gridbag layout. I consider it a WYWFINWYG interface. (What You Wish For Is Not What You Get)
regards. Dar7yl
Aki \ - 22 Feb 2005 16:19 GMT > You can probably guess that I'm not a big fan of gridbag layout. > I consider it a WYWFINWYG interface. > (What You Wish For Is Not What You Get) For any particular reason aside from it being horribly complicated?
I've just spent a good couple of work days struggling with layouts. That's what you get for starting mess around with an IDEs (In my case, JBuilders) graphic design tools without any idea on how different LayoutManagers work. As a result, I ended up with lots of both GridBagLayouts ("Hey, this thing keeps things of different lengths neatly in rows without forcing them to be of the same size! A-Ok!") and com.borland.jbcl.XYLayouts ("Wow, lookit! Total freedom of component placement!"). Sounds good, right?
Wrong.
The GBL worked fine just as long as I didn't want it to do anything too demanding like change the size or visibility status of the components in the GBL. If I did, I'd end up with, for example, a bunch of 1-pixel-high checkboxes. It was at this point I took a look at how, exactly, are things added on a GridBagLayout. "Defigning the objects constraints takes what, almost a dozen parameters? This doesn't look good..." I also realized an issue with the XYLa(z)yout. Notice the "com.borland." part at the beginning of its package hierarchy? Yup, copyright (c) Borland Corporation. In other words, so much as dream of distributing the jbcl.jar required for using the XYL and you'll get sued for approximately Integer.MAX_VALUE dollars for copyright infringement. 1) 1) I don't know if you get deployment licenses for the libraries with JBuilder Enterprise, but with Foundation it seems you're not allowed to distribute them.
Thus, I decided to strip the app. of every instance of both XYLayout and GridBagLayout. It took me a good two work days, or, to be precise, an intense 14-hour session, to replace them all with combinations of GridLayouyt, BorderLayout and FlowLayout.
The only problem I ran into while replacing the layouts was that to maintain the original GUI design, I had to add quite a lot of extraneous panels to create a "layouts within layouts" type design. For example, I had a GBL containing a checkbox with a long text, a pair of text fields and a couple of smaller buttons. I replaced this with a FlowLayout containing 1. the checkbox and 2. a new panel with GridLayout for the text fields and buttons. After quite a while of fiddling around with extra panels and preferred sizes of components, I had the UI look just like it used to be (at its best behaviour), but without any GBLs or XYLs.
My point? Don't do as I did, throwing GridBagLayouts and the IDEs own layouts about with the possible additions of a few "null" or "default" layouts. That's asking for trouble. Instead, [try to] learn about the functioning and use of different LayoutManagers *before* deciding which layout to use where.
<mode = sarcastic> *smirk* >-) It's really not that hard to learn proper use of layouts. Just get a chinese Kungfu mentor to proofread your code and do the following: 1. slap your face and call you the filthy offspring of a diseased rat for every IDE-specific layout you've used and 2. bash you really hard on the head with an oak staff every time you are even thinking about a GridBagLayout. </mode>
 Signature -Aki "Sus" Laukkanen
Karsten Lentzsch - 22 Feb 2005 19:31 GMT Hi Aki,
The free JGoodies Forms layout system has been designed to good design quickly. Besides a powerful and flexible layout manager, there are helper classes (non-visual) builders, a tutorial, layout tips&tricks, etc. guide you through visual design and its implementation in Java.
Many developers have reported that the Forms has turned their UI work into kind-of joy, where they were frustrated with the GBL before.
The pure Forms layout system targets primarily source coders; however, a design goal was the simple integration with visual builder tools that help you save time or improve the design. There are now some visual builders that are worth a look - open the Forms' README.html and see the "Visual Builders" section in the sidebar.
Hope this helps. Best regards, Karsten Lentzsch
Overview - http://www.jgoodies.com/freeware/forms/index.html Downloads - http://www.jgoodies.com/downloads/libraries.html
dar7yl - 23 Feb 2005 08:12 GMT > Hi Aki, > [quoted text clipped - 20 lines] > Overview - http://www.jgoodies.com/freeware/forms/index.html > Downloads - http://www.jgoodies.com/downloads/libraries.html I remember the good ole' days when we had a 80x25 screen to cram everything into. It was a lot simpler then. We had a curses interface that definately brought out the swear words. We could blink and underline and change colours. Ah, the memories.
The problem with GridBagLayout is that it is too flexible. Enough to program yourself into a corner and hang yourself. There is a lot of magic going on inside the layout manager and associated components that require an inordinate amount of internal knowledge. Components can affect the visual properties of other components that are not directly apparent or expected. For instance, some components expand and contract depending upon the contents of the data they are displaying, and affect other components in the same row or column. If you are using third-party components (such as Borland's) you are not always assured that those components cooperate with the layout manager in a logical manner, or support all the necessary properties.
I do use GBL for record field layout, but I usually wrap it inside other components such as panes, boxes, splitpanels, borderlayout etc. I had started by using an IDE to generate the layout, but quickly determined that it generates horrendous, inefficient code, and found it was better to hand-craft my displays. I ended up putting a wrapper around the layout that makes it easier for me to control what happens, but it doesn't look anything like the LayoutManager interface. (Which I consider to be sophomoric)
I wish there was something easier to understand and use. Sort of a compromise between Grid and GridBag layouts where you can define the rows and columns in terms of screen positions, not relative component placements. And don't try to sell me SpringLayout - not even the authors can understand or describe how to use it.
Anyway, thanks for the opportunity to rant.
regards, Dar7yl
Thomas Weidenfeller - 23 Feb 2005 10:12 GMT > I wish there was something easier to understand and use. Sort of a > compromise > between Grid and GridBag layouts where you can define the rows and columns > in terms of screen positions, not relative component placements. I would like to see a reasonable logical description of a mechanism of how (cross-platform) resizing features can be achieved by specifying component positions with presumably static screen position information.
I don't think it is possible. Describing resizing behavior means to describe dynamic behavior and dynamic relations between components. The relations often depend on relative instead of absolute positions, distances, weights, etc.
> And don't > try > to sell me SpringLayout - not even the authors can understand or describe > how > to use it. I am deeply burned by a certain Motif geometry manager based on the idea of nuts and bolts to put GUIs together. The sheer idea that a GUI is a mechanical, dynamic system is laughable. I wish the people who come up with this idea again and again are forced to use their stuff on at least ten non-trivial GUI projects. They would quickly run away.
/Thomas
 Signature The comp.lang.java.gui FAQ: ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Joe Knapka - 23 Feb 2005 21:42 GMT > > I wish there was something easier to understand and use. Sort of a > > compromise [quoted text clipped - 20 lines] > come up with this idea again and again are forced to use their stuff > on at least ten non-trivial GUI projects. They would quickly run away. I think the idea of One Layout Manager To Rule Them All is basically misguided, since any such manager is going to have to satisfy a huge number of design goals and constraints, and is therefore going to be very complicated. It's better IMO to use different kinds of layouts that fit the exact requirements of a particular application. In my experience, more than one layer of component nesting is rarely, if ever, necessary; and BorderLayout, GridLayout, BoxLayout, and JTabbedPane suffice for 99.99% of all usage.
(Incidentally, I never use IDE UI builders. I prefer to be able to maintain the code in the inevitable event that the IDE vendor ceases to support their wonderful creation.)
Cheers,
-- Joe
 Signature A real God would simply Be, and it would be as rare to find a person who did not acknowledge God's existence as it is to find someone who denies the existence of the earth. -- Mark Nutter -- pub 1024D/BA496D2B 2004-05-14 Joseph A Knapka Key fingerprint = 3BA2 FE72 3CBA D4C2 21E4 C9B4 3230 94D7 BA49 6D2B If you really want to get my attention, send mail to jknapka .at. kneuro .dot. net.
John McGrath - 24 Feb 2005 20:19 GMT On 2/22/2005 at 11:19:09 AM, Aki "Sus" Laukkanen wrote:
> The GBL worked fine just as long as I didn't want it to do anything too > demanding like change the size or visibility status of the components in > the GBL. The problem with GridBagLayout is not really changing the size - if a layout manager can determine the size once, it can do it again. It uses the exact same process in both cases. The problem GBL has is that it does not deal well with situations where it does not have enough space to size components at their preferred sizes. Rather than shrink the components down a little bit, it knocks them down to their minimum sizes.
I presume that the reason you see this happen when you resize a component is that the size requirements of the container is growing and its parent is not being revalidated, so after the resize the container does not have the amount of space it needs.
GBL is definitely *not* the all-purpose layout manager that some people make it out to be. There are many situations where it does not work well, but there are also situations where it does.
> I also realized an issue with the XYLa(z)yout. Notice the "com.borland." > part at the beginning of its package hierarchy? Yup, copyright (c) [quoted text clipped - 4 lines] > JBuilder Enterprise, but with Foundation it seems you're not allowed to > distribute them. Yes, all of the paid versions of JBuilder provide a license to redistribute JBCL. I suppose it would be nice if they allowed that with Foundation also, but that is not much to complain about. It is hard to make the argument that, because they give you something for free, they should give you other things for free as well.
It is also not much of a hardship, since you *really* should not be using fixed-coordinate layouts in software that you distribute. The problem is that you cannot be sure that the layout will work on any other system, since there are any number of factors that could affect the size of the components being laid out.
XYLayout is very convenient, particularly when used with JBuilder's UI Designer. You can generally construct a UI using an XYLayout, then switch the layout manager, and it will automatically provide the constraints for the new layout manager that results in a similar GUI. I often construct layouts that way, particularly when I want to end up with a GridBagLayout, but I would never distribute software that uses an XYLayout.
> The only problem I ran into while replacing the layouts was that to > maintain the original GUI design, I had to add quite a lot of extraneous > panels to create a "layouts within layouts" type design. There is nothing wrong with creating a UI with multiple nested containers. It is often the simplest and most robust approach.
 Signature Regards,
John McGrath
Dirk Starke - 22 Feb 2005 16:08 GMT Hello Mike,
if you want two panels having the same size you might use GridLayout.
Greetings Dirk
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|