Results 1 to 8 of 8
- 07-05-2010, 09:01 PM #1
Gridbaglayout not working as expected
Hi there,
I'm trying to layout three panels vertically in a JScrollPane.
I've made a class which extends JPanel and implements Scrollable. I have placed it into the JScrollPane. In the panel I have placed TopPanel, MiddlePanel and BottomPanel.
However, the panels are appearing horizontally in the scrollpane, instead of vertically.
If I set the preferred size of the panel, the panels are placed vertically, however the third panel is not shown and the bottom of the second panel is missing.
How do I make all three panels appear vertically, without cutting out any panels?
Thanks for your attention,
Berkeleybross
Java Code:public class AdviceNotePanel extends JPanel implements Scrollable{ public AdviceNotePanel () { GridBagConstraints myConstraints; setBackground(Color.WHITE); //setPreferredSize(new Dimension(595, 842)); JPanel topPanel = new JPanel (new GridBagLayout()); topPanel.setBackground(Color.WHITE); myConstraints = new GridBagConstraints(); myConstraints.gridx = 1; myConstraints.gridy = 1; myConstraints.gridwidth = 1; myConstraints.gridheight = 1; myConstraints.weightx = 1; myConstraints.weighty = 1; myConstraints.anchor = GridBagConstraints.CENTER; myConstraints.fill = GridBagConstraints.BOTH; myConstraints.insets = new Insets(0, 0, 0, 0); myConstraints.ipadx = 0; myConstraints.ipady = 0; add(topPanel, myConstraints); //Code adding components to top panel JPanel middlePanel = new JPanel(new GridBagLayout()); myConstraints = new GridBagConstraints(); myConstraints.gridx = 1; myConstraints.gridy = 2; myConstraints.gridwidth = 1; myConstraints.gridheight = 1; myConstraints.weightx = 1; myConstraints.weighty = 1; myConstraints.anchor = GridBagConstraints.BASELINE; myConstraints.fill = GridBagConstraints.BOTH; myConstraints.insets = new Insets(0, 0, 0, 0); myConstraints.ipadx = 0; myConstraints.ipady = 0; add(middlePanel, myConstraints); // Code adding components to middlepanel JPanel bottomPanel = new JPanel(new GridBagLayout()); bottomPanel.setBorder(BorderFactory.createLineBorder(Color.gray)); bottomPanel.setBackground(Color.WHITE); myConstraints = new GridBagConstraints(); myConstraints.gridx = 1; myConstraints.gridy = 3; myConstraints.gridwidth = 1; myConstraints.gridheight = 1; myConstraints.weightx = 1; myConstraints.weighty = 1; myConstraints.anchor = GridBagConstraints.CENTER; myConstraints.fill = GridBagConstraints.BOTH; myConstraints.insets = new Insets(0, 0, 0, 0); myConstraints.ipadx = 0; myConstraints.ipady = 0; add(bottomPanel, myConstraints); // Code adding components to bottomPanel } // Code implementing scrollable interface }
-
If you can, can you post a small compilable runnable example of your problem? Just at a glance though, you know that gridx and gridy both are 0 based, not 1 based.
Also, based on your description, it may not be important that the top, middle and bottom panel implement GridBagLayout, however it is critical that AdviceNotePanel, the "this" JPanel, implement GridBagLayout, and I don't see that you've done this, at least not in the code that you've posted.
Also, if all you want are three equally sized JPanels stacked on top of each other, why not use the far simpler GridLayout(0, 1)?Last edited by Fubarable; 07-05-2010 at 09:39 PM.
- 07-05-2010, 09:50 PM #3
Hi Fubarable,
I'll change the constraints to 0 based. If that doesnt work, then I'll try to make a small program and post it here.
The AdviceNotePanel does indeed implement gridbaglayout, i do it when i instanciate the AdviceNotePanel so you dont see it in this code :P
The JPanels should be the same width, but not the same height. They have quite a few components in them already, I left them out so to make clearer code.
Ill get back to you if 0basing them has any effect :)
Berkeleybross
- 07-05-2010, 11:17 PM #4
Heh, its amazing what SSCCE's can do.
Turns out I wasn't using GridBagLayout for the panel.
I had been setting the layout as using GridBagLayout after putting all the components in the panel, so it didn't work.
I seem to have a habit of looking for hours at my problem, giving in and coming here only for the solution to be staring me in the face all along.
Thanks for your time though, much appreciated.
Berkeleybross
-
I don't think that this matters as long as it is set to GridBagLayout before pack() or revalidate() is called. For my money though, if you know that the JPanel is going to always need to be using GridBagLayout, then set it near the beginning of its constructor and be done with it.
Glad you have it fixed. You may wish to mark this thread "solved".
Oh, another way to stack different height JPanels on top of each other is to use BoxLayout which is easier to use than GridBagLayout.
- 07-05-2010, 11:31 PM #6
I didnt think it mattered either, apparently it does though :P
Ill mark it solved now.
Thanks again,
Berkeleybross
- 07-06-2010, 08:51 AM #7
Yes, it matters. To see why, read the source of GridBagLayout#addLayoutComponent(Component comp, Object constraints) which calls setConstraints(Component comp, GridBagConstraints constraints) which in turn adds the Component and a clone of the GBC to a Hashtable.
The values in the Hashtable are read when layoutContainer calls arrangeGrid which calls ArrangeGrid [aside: one has to wonder who was responsible for that particular cr@p in violation of naming conventions] which calls lookupConstraints which provides the value from the Hashtable.
If GBL is set after adding the components, the Hashtable won't have any entries and the components will be laid out as if added with a null constraint.
db
-
Similar Threads
-
help with gridbaglayout
By robertbob in forum AWT / SwingReplies: 5Last Post: 05-18-2010, 04:14 AM -
GridBagLayout
By Moncleared in forum New To JavaReplies: 1Last Post: 10-18-2009, 10:12 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 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