Results 1 to 2 of 2
- 02-27-2010, 01:59 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 29
- Rep Power
- 0
Arrange components in a GridBagLayout
Hi guys,
The problem is that my components aren't displayed as I wish.
I made a new post since the other one is almost on the 2nd page and I doubt someone will look at it ^^
So I will paste my code here. You can run it. Sorry for the length :) I tried to comment it as much as possible.
There are three panels in this GUI. One left, one right, one south. The problem is only for the right one. In the right one, there are three pairs of (Label,TextField). They are not where I want them to be. What I would like is to have them all at the top of the panel, one after each other. Moreover, I would like all the labels aligned together (easy) but also all the textField aligned together...
Now, my pairs of (Label,TextField) are spread all over the panel, they aren't all at the top. And I would like to be able to do that using the GridBagLayout because it will be useful later if I need to do any modification.
I probably did all wrong with the anchors, I know. But even with the link masijade gave me, it still doesn't really help me to understand how it works.
Any idea how to get this result ?
Thanks heaps!
Java Code:import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class GUI2 extends JFrame { private final static long serialVersionUID = 1L; private final static boolean RIGHT_TO_LEFT = false; private final static boolean SHOULD_FILL = true; private final static boolean SHOULD_WEIGHTX = true; // Swing components JFrame frame; JLabel labelChar; JLabel labelIndex; JLabel labelCategory; JLabel labelUnicode; JTextField textFieldIndex; JTextField textFieldCategory; JTextField textFieldUnicode; JPanel panelLeft; JPanel panelRight; JPanel panelSouth; JTextField textPinyin; // Borders TitledBorder title; Border raisedbevel, loweredbevel, compound; public GUI2() { super(); createGUI(); } public void createGUI() { // Set the Look & Feel from the current platform try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Creates the frame frame = new JFrame(); frame.setTitle("Hello!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container pane = frame.getContentPane(); if (RIGHT_TO_LEFT) { pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); if (SHOULD_FILL) { //natural height, maximum width c.fill = GridBagConstraints.BOTH; } if (!SHOULD_WEIGHTX) { c.weightx = 0.5; } raisedbevel = BorderFactory.createRaisedBevelBorder(); loweredbevel = BorderFactory.createLoweredBevelBorder(); compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel); title = BorderFactory.createTitledBorder("title"); //------------------------------------------------------------------- // Panel Left //------------------------------------------------------------------- panelLeft = new JPanel(); panelLeft.setBorder(title); // Label displaying the letter "A" (only component of the left panel) labelChar = new JLabel(); labelChar.setFont(new Font("Arial",Font.PLAIN, 150)); labelChar.setText("A"); labelChar.setBorder(compound); // Add the label to the left panel panelLeft.add(labelChar); //------------------------------------------------------------------- // Panel Right //------------------------------------------------------------------- panelRight = new JPanel(new GridBagLayout()); panelRight.setBorder(title); GridBagConstraints c1 = new GridBagConstraints(); // Label "index" labelIndex = new JLabel("Index: "); labelIndex.setBorder(raisedbevel); // Constraints for the label "index" c1.anchor = GridBagConstraints.NORTHWEST; c1.weightx = 0.5; c1.weighty = 0.5; c1.ipady = 10; c1.gridx = 0; c1.gridy = 0; // Add the label "Index" to the right panel panelRight.add(labelIndex,c1); // TextField "index" textFieldIndex = new JTextField("Some index..."); textFieldIndex.setBorder(raisedbevel); // Constraints for the textField "index" c1.gridx = 1; c1.gridy = 0; // Add the textField "Index" to the right panel panelRight.add(textFieldIndex,c1); // Label "category" labelCategory = new JLabel("Category: "); labelCategory.setBorder(raisedbevel); // Constraints for the label "category" c1.anchor = GridBagConstraints.NORTHWEST; c1.weightx = 0.5; c1.weighty = 0.5; c1.gridx = 0; c1.gridy = 1; panelRight.add(labelCategory,c1); // TextField "category" textFieldCategory = new JTextField("Some category..."); textFieldCategory.setBorder(raisedbevel); // Constraints for the textField "category" c1.gridx = 1; c1.gridy = 1; // Constraints for the textField "category" panelRight.add(textFieldCategory,c1); // Label "unicode" labelUnicode = new JLabel("Unicode: "); labelUnicode.setBorder(raisedbevel); // Constraints for the label "unicode" c1.anchor = GridBagConstraints.NORTHWEST; c1.gridx = 0; c1.gridy = 2; panelRight.add(labelUnicode,c1); // TextField "unicode" textFieldUnicode = new JTextField("Some unicode..."); textFieldUnicode.setBorder(raisedbevel); // Constraints for the label "unicode" c1.gridx = 1; c1.gridy = 2; panelRight.add(textFieldUnicode,c1); // Constraints for both left & right panels c.weightx = 0.5; c.gridx = 0; c.gridy = 0; c.ipady = 150; c.ipadx = 150; pane.add(panelLeft,c); c.gridx = 1; c.gridy = 0; c.ipady = 300; c.ipadx = 300; pane.add(panelRight,c); //------------------------------------------------------------------- // Panel South //------------------------------------------------------------------- panelSouth = new JPanel(); panelSouth.setBorder(title); // Constraints for the south panel c.weightx = 0.5; c.gridx = 0; c.gridy = 1; c.gridwidth = 2; c.ipadx = 0; c.ipady = 100; pane.add(panelSouth, c); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { GUI2 gui = new GUI2(); } }
-
Please don't divide the discussion up in several threads. As this is against forum rules and general etiquette, I'm locking this thread and referring others to your original thread here: Problem with GridBagLayout
The reason your other thread is two pages long is that your code is so long. I suggest that you create a small code example and post it if you want more to read your code help you.
Much luck!
Similar Threads
-
GridBagLayout
By Moncleared in forum New To JavaReplies: 1Last Post: 10-18-2009, 10:12 PM -
GridBagLayout
By carderne in forum New To JavaReplies: 8Last Post: 01-25-2009, 02:06 PM -
GridBagLayout
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-07-2008, 12:16 AM -
gridbaglayout: increase/decrease size of components.
By newtojava7 in forum New To JavaReplies: 2Last Post: 01-28-2008, 07:22 AM -
gridbaglayout
By newtojava7 in forum New To JavaReplies: 4Last Post: 01-27-2008, 08:03 PM


LinkBack URL
About LinkBacks

Bookmarks