bob10 0 Newbie Poster

Hi guys,

I have a problem using GridBagLayout.

I will paste my code here. You can run it. Sorry for the length :) I tried to comment it as much as possible.

So 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...
Any idea how to get this result ?

Thanks heaps!

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();
	}
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.