Results 1 to 2 of 2
Thread: GridBagLayout
- 10-18-2009, 07:28 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
GridBagLayout
So I've been getting away from the default layout manager and have been playing with GridBagLayout. I actually like this layout a lot, its a rather simple way to give a complex layout.
My trouble is that I have, say 100 buttons, I'm adding to a JPanel. This JPanel is inside a JScrollPane (So when there are too many buttons, you can scroll to see the rest). I'm trying to use the GridBagLayout manager to add each button to the NORTH. So I set a constraint;
As I understand this, if the Component's Displaying Area (the JPanel) has extra room, this constraint will distribute the extra space based around the constraint. I choose to set mine Centered at the top, which as I understand to be NORTH.Java Code:c.anchor = GridBagConstraints.NORTH;
When I run this app, the button continues to display in the center.
The above code is not exact, its the pieces I think are important in solving the puzzle. Its the setup, the JPanel and JScrollPane (w/Dimension), and the buttons being added using the constraints.Java Code:JPanel buttonHolder = new JPanel(); JScrollPane sp = new JScrollPane(buttonHolder); GridBagConstraints c = new GridBagConstraints(); Dimension d = new Dimension(160,560); c.gridx = 100; c.anchor = GridBagConstraints.NORTH; sp.setPreferredSize(d); mainPanel.add(sp); for(int i=0; i<counter; i++){ buttonBuddies[i].setText(buddies[i]); buttonHolder.add(buttonBuddies[i],c); }
Thanks for any suggestions
- 10-18-2009, 10:12 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
A simpler way is to use nested panels with each panel using the appropriate layout manger. You are not forced to use a single layout manager.its a rather simple way to give a complex layout.
When you have a problem you don't know what is and what isn't important. That is why you post a SSCCE to demonstrate the problem.The above code is not exact, its the pieces I think are important in solving the puzzle
Similar Threads
-
GridBagLayout
By carderne in forum New To JavaReplies: 8Last Post: 01-25-2009, 02:06 PM -
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...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