Results 1 to 2 of 2
  1. #1
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default 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();
    	}
    	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 11:12 PM
  2. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 03:06 PM
  3. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:16 AM
  4. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 08:22 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •