Results 1 to 17 of 17
- 11-09-2010, 03:44 PM #1
Member
- Join Date
- May 2010
- Posts
- 32
- Rep Power
- 0
gridBagLayout anchor ... wanna move all components to top left
Hi All
I have a panel and in that panel I have 3 labels. This is obviously just test code which will become more complicated but the idea is to have a panel of a certain width and then add labels within that panel such that they are top-left/WEST aligned. The sample code is attached with this message and if you run it you will see that java renders all components center aligned which is a massive pain.
I have tried changing the x-weights of labels and it does tend to work very unpredictably. Another thing I have done is to add an empty spacer label right at the end of the row with large width and high x-weighting which tends to push all others to the left. But it does not seem like an elegant solution. Especailly considering that I will have a large number of rows in the app I am working on.
Any suggestions?
ps: I cant use swing unfortunately... I am working on a legacy app.
thanks in advance :)
- 11-09-2010, 04:39 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Try adding BorderLayout constraints when you add the panel to the applet. I haven't done much work with applets, but I've run into this problem with Swing and assume common problems would have common causes/fixes.
Java Code:this.add(mainPanel, BorderLayout.WEST);
- 11-09-2010, 05:07 PM #3
Member
- Join Date
- May 2010
- Posts
- 32
- Rep Power
- 0
noop no joy :(
-
You will want to create an SSCCE and post it here for further help. Please read the 3rd link in my signature links immediately below this line.
- 11-10-2010, 09:28 AM #5
Member
- Join Date
- May 2010
- Posts
- 32
- Rep Power
- 0
Hi Fubarable
I believe the attachment fulfills the criteria for an SSCCE. Please copy and paste the text in a new class file(HelloWorldApplet.java) in an IDE and run it. It will show you the problemo.
thanks
-
OK, but why are you using GridBagLayout when the effect you desire suggests that you not use this. If you want labels placed upper left, I'd use BorderLayout as suggested and BoxLayout or FlowLayout for the inner JPanel.
- 11-10-2010, 05:11 PM #7
Member
- Join Date
- May 2010
- Posts
- 32
- Rep Power
- 0
Hi
No I need GridBagLayout.
As mentioned before I will have multiple rows of this. With different insets, paddings and grids for different components.....
-
Hm, then perhaps you could post a slightly more involved sample program that more closely mimics your requirements. Also could you describe your requirements in greater detail?
- 11-10-2010, 06:11 PM #9
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
What's the problem you are seeing when you add the BorderLayout constraint?
Does it actually move the mainPanel over to the West? If so try creating/adding another panel.
Java Code://Create a Panel which moves the mainPanel to the West JPanel panel2 = new JPanel(); panel2.setLayout(new BorderLayout()); panel2.add(mainPanel, BorderLayout.WEST); //Add the new Panel to the JApplet, making sure to add it to the North this.add(panel2, BorderLayout.NORTH);
- 11-10-2010, 06:12 PM #10
-
- 11-11-2010, 03:40 AM #12
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
hey had this problem today was really annoying.
this solved my prob:
dont skip "d" i think it makes a difference.Java Code:c.weightx = 1d; c.weighty = 1d;
Good luck
- 11-11-2010, 03:42 AM #13
Member
- Join Date
- Nov 2010
- Posts
- 15
- Rep Power
- 0
by the way i think you dont need this:
if what i suggested before doesnt work try editing this aswellJava Code:c.anchor = GridBagConstraints.WEST;//LEFT
I use GridBagConstraints.BOTHJava Code:c.fill = GridBagConstraints.NONE;
- 11-12-2010, 10:02 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
I find it easier to nest the GridBagLayout (GBL) panels from the inside out when working with unevenly sized components like labels. GBLs change their size and shape based on contents.
First, build a JPanel with just the labels and associated component. For example, if you want a column of labels next to a column of text fields or check boxes you could stick each label and associated object in its own JPanel with FlowLayout keeping each panel the same width to get the right spacing, Then you could place each of these panels in another panel with vertical orientation and place it as one column of a GBL or next to another such JPanel.
Panels are the key to alignment. Identical components will fit nicely in a GBL. But if they are not the same size, you might need to place related groups in smaller panels and nest them in the GBL cells. Otherwise you will drive yourself crazy, especially when you need to rearrange something.
Hope this helps.
- 11-15-2010, 04:46 PM #15
Member
- Join Date
- May 2010
- Posts
- 32
- Rep Power
- 0
This was the best solution I could come up with. A high vertical weighting empty label at bottom North anchored to keep all to the top
and a A high horizontal weighting empty label at right WEST anchored to keep all to the left.
The short answer to all the people who are advising to use some thing other than the GridBagLayout. Well they are useless. Countless number of times I have started work and nearly completed to find out the layout manager doesnt allow some thing basic like add different spacing btw diff comp's, diff size comp's, alignment issues and what not.
Java Code:public class TopLeftAlignedPanel extends Panel { public TopLeftAlignedPanel() { this.setLayout( new GridBagLayout() ); GridBagConstraints c = new GridBagConstraints(); c.gridx = 1000; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.WEST;//LEFT c.weightx = 1000; c.weighty = 0; c.ipadx = 0; c.ipady = 0; //Insets(top, left, bottom, right); c.insets = new Insets( 0,0,0,0 ); Label rightEmptyLabel = new Label( "" ); rightEmptyLabel.setBackground(( Color.pink ) ); setCompSize(rightEmptyLabel, 3, 3 ); this.add( rightEmptyLabel , c); c.gridx = 0; c.gridy = 1000; c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.NORTH;//upwards c.weightx = 0; c.weighty = 1000; c.ipadx = 0; c.ipady = 0; //Insets(top, left, bottom, right); c.insets = new Insets( 0,0,0,0 ); Label bottomEmptyLabel = new Label( "" ); bottomEmptyLabel.setBackground(( Color.pink ) ); setCompSize(bottomEmptyLabel, 3, 3 ); this.add( bottomEmptyLabel , c); } public static void setCompSize(Component comp , int width, int height) { Dimension size = new Dimension( width, height ); comp.setSize(size); comp.setPreferredSize(size); comp.setMinimumSize(size); } }Last edited by alinaqvi90; 11-15-2010 at 04:50 PM. Reason: Forgot to add method.
- 11-16-2010, 06:36 AM #16
Maybe I still haven't understood your requirement, but as per that description I would simply put all those components in a JPanel using GBL and add that panel to another JPanel with a FlowLayout(FlowLayout.LEFT, 0, 0). Then add the outer panel to the GUI.This was the best solution I could come up with. A high vertical weighting empty label at bottom North anchored to keep all to the top
and a A high horizontal weighting empty label at right WEST anchored to keep all to the left.
db
- 11-17-2010, 01:02 PM #17
Member
- Join Date
- May 2010
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
Adding and removing components from a GridBagLayout
By peterhabe in forum New To JavaReplies: 4Last Post: 09-19-2010, 10:13 PM -
anchor tag in html
By meghana in forum EclipseReplies: 3Last Post: 08-18-2010, 03:21 PM -
Arrange components in a GridBagLayout
By ze snow in forum New To JavaReplies: 1Last Post: 02-27-2010, 02:22 PM -
just wanna ask about images...
By ashton in forum New To JavaReplies: 11Last Post: 01-21-2009, 12:16 PM -
gridbaglayout: increase/decrease size of components.
By newtojava7 in forum New To JavaReplies: 2Last Post: 01-28-2008, 07:22 AM


LinkBack URL
About LinkBacks


Bookmarks