I need help on layout manager (Gridlayout and GridBagLayout). I try adding two labels and two text fields within a JFrame as follows: First Name: ____________ Last Name: ____________ By default setting, they are placed in the center of the Frame. No matter how I play around with the setting, I am not able to make it left justified to the edge of the Frame. Can someone help me how to make it left justified with say 1 inch margin.
Originally posted by Rudy Yeung: I need help on layout manager (Gridlayout and GridBagLayout). I try adding two labels and two text fields within a JFrame as follows: First Name: ____________ Last Name: ____________ By default setting, they are placed in the center of the Frame. No matter how I play around with the setting, I am not able to make it left justified to the edge of the Frame. Can someone help me how to make it left justified with say 1 inch margin.
You just need to play with different layouts until it works. I'm not sure if GridLayout is what you want, and personally I wouldn't go near GridBagLayout with a 10ft pole. It provides the most accurate positioning, but it can be a nightmare to set up. I think the standard layout is BorderLayout, and you can add stuff to the left with add( BorderLayout.West, myLabel ); You will need to put your components in a panel first and add that to the layout if you have more than one. This panel could have a GridLayout or perhaps a FlowLayout. Or you could set the layout to GridLayout with a few 'cells', and work out which ones should contain your components - you can fill the rest with blank labels. You can't do this if you have other things in the frame, unless you want them positioned in the cells.
As Grant said, "You just need to play with different layouts until it works. " Dont forget you can add panels(with their own layouts), thereby creating almost anything you want. I think null layout is even more of a last resort than GridBagLayout. ------------------ Dont blindly believe everything I say.
Hi Rudy, I think I can help you out. Think physically about how would you like the layout of the components. A frame can have as many panel as you want. You can create panel within panel. You situation is not that bad. I assume you have previously created the label and text box! first create 1 panel for label. //LAYOUT THE LABLES IN ONE PANEL JPanel panLabel = new JPanel(); panLabel.setLayout(new GridLayout(0,1)); // row, column panLabel.add(lblFirst); //add the label panLabel.add(lblLast); //add another label create another panel to hold the text box. //LAYOUT THE TEXT FIELD JPanel panField = new JPanel(); panField.setLayout(new GridLayout(0,1)); panField.add(txtFirst); panField.add(txtLast);
//create a content panel to hold the above two panels JPanel panContent = new JPanel(); panContent.setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); //set the panel distance from the edge of the frame panContent.setLayout(new BorderLayout()); panContent.add(panLabel, BorderLayout.WEST); panContent.add(panField, BorderLayout.EAST); And that it is! Let me know if you can make it. I hope I can provide further assistance. Good luck. Mindy Wu
Mindy, Thanks for your advice. It works. The key point in your snippet is the line: "topPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));" Actually, I have another solution for myself. Instead of using GridLayout or DGridBagLayout, I use flowlayout manager because it can control the left and right justification of the components against the edge of the frame. I use two panels. Each panel uses the flowlayout manager, which has two components (one label and one text field) filled in. The two resulting panels is then added to another panel in both the North and South borders. But I like the way you do it though. If you had not told me, I would have never figured out the usage of the setBorder and the BorderFactory. BTW, do you know if there is any website talking about the tricks like the one I give. It looks very simple, but just do not know how to solve it.