Results 1 to 9 of 9
Thread: GridBagLayout
- 01-24-2009, 01:23 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
-
solution: 1) (my preferred solution) don't use GridBagLayout unless absolutely necessary. Usually a more stable layout can be created by nesting JPanels each with their own layouts.
2) Study up on GridBagLayout and its rules and regulations. Are you setting the weight value?
3) If you need specific help, you'll have to post code. No one can even get close to guessing what your problem is based solely on description alone.
4) Or even post a pic of the proper layout desired and we could give you pointers. (both 3 and 4 together would be even better still!)Last edited by Fubarable; 01-24-2009 at 01:36 PM.
- 01-24-2009, 06:31 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Here's a pic of what it looks like:
I'm obviously happy to do it with whatever layout manager. What do you recommend for this situation?


Here's some code:I haven't used weight values, because I've set the whole thing to be non-resizable...Java Code:class Panel extends JPanel { Panel() { setLayout(new GridBagLayout()); GridBagConstraints c; JButton myButton = new JButton("HELLO WORLD"); c = new GridBadConstraints(.......); add(newComponent, c); //create another component //new values for c add(newComponent, c); } } class Driver { static void createAndShowUI() { JFrame frame = new JFrame("HELLO WORLD"); frame.getContentPane().add(new Panel()); } }
Also, I want the edges of my JList components to be flush with the border of the entire window, i.e. there mustn't be that little gap on the sides...
ThanksLast edited by carderne; 01-24-2009 at 06:45 PM.
-
It almost appears that a JTable could work well there. Otherwise, hm, hard to say.
- 01-24-2009, 11:18 PM #5
Judicious use of weightX values should solve your problem.
Another possibility is that setting an appropriate prototype cell value for the jists (see the API for JList) would "freeze" their widths.
If neither solves the problem completely, you may need to set a preferred size for each component.
db
- 01-25-2009, 03:38 AM #6
I use GridBagLayout all the time, but it requires some thought about how to set the constraints to cause the components to set where they should.
Looking at the concept, I assume you plan to use a JTree on the left and a JTable on the right, with a title area on top and messages below. So, you have three rows and two columns in your main JPanel. The 0 and 2 rows extend across the remaining rows. Use two JPanel's and set their preferred heights.
In the 1 row, you have two areas, each in its own column. A nice approach is to fill both columns with a JSplitPane. If you do this, your main JPanel is down to one column. The JSPlitPane should take up the entire width and height.
You JTree and JTable can be larger than the space available, so use JScrollPane in each area of the JSplitPane. Put the JTree and JTable in those.
You can create only one constraints object, but modify it each time before you use it. You will need to reset the previous settings. Or, you can create a new one each time you add a component. Set the minimum number of constraint properties while achieving the desired result.
Get your code to that point, and you will be able to ask more specific questions if it doesn't work.
- 01-25-2009, 05:18 AM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Well, I never use gridbags. I use Boxes instead. I find I can lay things out easily using Boxes, and either let them resize, or fix their size also very easily. I've written hundreds of GUIs using nothing but Boxes for layouts.
- 01-25-2009, 09:34 AM #8
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Thanks for your pointers...
Judicious use of weightX values should solve your problem.
I haven't really looked into the weightx stuff, but I need to figure out exactly how it works and I'll have a fiddle with those.
Another possibility is that setting an appropriate prototype cell value for the jists (see the API for JList) would "freeze" their widths.
I'll give that a try.
If neither solves the problem completely, you may need to set a preferred size for each component.
That somehow slipped my mind. I'll give that a try too.I use GridBagLayout all the time, but it requires some thought about how to set the constraints to cause the components to set where they should.
Looking at the concept, I assume you plan to use a JTree on the left and a JTable on the right, with a title area on top and messages below. So, you have three rows and two columns in your main JPanel. The 0 and 2 rows extend across the remaining rows. Use two JPanel's and set their preferred heights.
The pic I posted isn't a concept, it's a screenshot of what my program currently looks like. It's actually a JList on both sides, but that could change and is irrelevant anyway. So should I set up a 3x2 Grid layout with a panel in the left and another in the right? I quite like that idea.
In the 1 row, you have two areas, each in its own column. A nice approach is to fill both columns with a JSplitPane. If you do this, your main JPanel is down to one column. The JSPlitPane should take up the entire width and height.
I've never used a JSplitPane, but I'll have a look at that option.
You JTree and JTable can be larger than the space available, so use JScrollPane in each area of the JSplitPane. Put the JTree and JTable in those.
You can create only one constraints object, but modify it each time before you use it. You will need to reset the previous settings. Or, you can create a new one each time you add a component. Set the minimum number of constraint properties while achieving the desired result.
Instead of using the standard of setting each value, (c.gridx = ...) I kept the same 'c' object, but used the complete constructor each time, to be sure that no undesired values were being carried over...
Get your code to that point, and you will be able to ask more specific questions if it doesn't work.Well, I never use gridbags. I use Boxes instead. I find I can lay things out easily using Boxes, and either let them resize, or fix their size also very easily. I've written hundreds of GUIs using nothing but Boxes for layouts.
I'll have a look at that option.
- 01-25-2009, 02:06 PM #9
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Well I seem to have a decent solution:
The main Panel uses a vertically arranged BoxLayout,which holds three Panels.
The top one will house the controls and search bar etc.
The middle one has the headers and things (Playlists, Title, Artist etc).
The bottom one has the two JLists side-by-side, in JScrollPanes.
Then I just added the "0 songs" JLabel at the bottom...
It is now much better and easier to read.
Thanks very much for your help.
One last question, if someone can answer it: how can I remove the little gaps between my components and the edge of the window?
Similar Threads
-
GridBagLayout
By MuslimCoder in forum New To JavaReplies: 1Last Post: 01-15-2009, 08:54 PM -
abt gridbaglayout
By pinky in forum AWT / SwingReplies: 1Last Post: 12-15-2008, 08:35 AM -
GridBagLayout
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-07-2008, 12:16 AM -
GridBagLayout...please help
By newtojava7 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 01:16 AM -
gridbaglayout
By newtojava7 in forum New To JavaReplies: 4Last Post: 01-27-2008, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks