Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,035 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,656 people online right now. Registration is fast and FREE... Join Now!




Problem with GridBagLayout

 
Reply to this topicStart new topic

Problem with GridBagLayout

nick2price
7 Sep, 2008 - 03:32 AM
Post #1

D.I.C Head
**

Joined: 23 Nov, 2007
Posts: 231



Thanked: 6 times
My Contributions
I have made my code shorter to try and just show the error. I am using 4 panels in a container and i have coloured each panel to show they are there. My problem is my left panel which is set to LINE_START in the container. This panel's layout is set to GridBagLayout. I am trying to create a registration type panel. I am starting off with my registration title, i have set the gridx and gridy both too 0 but my title is being put in the middle of the panel. I cant seem to get it too work from the top of the panel. Please if you could look at the code and see why this is happening. The code should compile if you like to take a look.
Thanks very much.
CODE

import java.awt.*; //the importing of required libraries
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.*;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Album extends JFrame
{
private JPanel topPanel, leftPanel, centerPanel, rightPanel, bottomPanel;
private JLabel  regUser, regPassword, regName, regTitle;
private JTextField regUser2, regPassword2, regName2;

private String regUser3, regPassword3, regName3;


private Container cPane;


public Album()
{
super();

createGUI();

regUser3 = "";
regPassword3 ="";
regName3 ="";

}

private void createGUI(){
  
cPane = getContentPane();
  
arrangeCompenents();

setTitle("Main");
setLocation(new Point(0, 0));
setVisible(true);
setSize(800, 608);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  
}


private void arrangeCompenents(){
  
  
  topPanel = new JPanel();
  leftPanel = new JPanel();
  centerPanel = new JPanel();
  rightPanel = new JPanel();
  bottomPanel = new JPanel();

  regUser = new JLabel("Username");
  regPassword = new JLabel("Password");
  regName = new JLabel("Full Name");
  regTitle = new JLabel("REGISTER");
  
  regUser2 = new JTextField(10);
  regPassword2 = new JTextField(10);
  regName2 = new JTextField(10);

    
  regTitle.setFont(regTitle.getFont().deriveFont(Font.BOLD, 12));
  regTitle.setForeground(new Color(0, 153, 254));
  
  
  cPane.setLayout(new BorderLayout());
  cPane.add(topPanel,BorderLayout.PAGE_START);
  cPane.add(centerPanel,BorderLayout.CENTER);
  cPane.add(bottomPanel, BorderLayout.PAGE_END);
  cPane.add(leftPanel, BorderLayout.LINE_START);

    topPanel.setBackground(Color.black);
    centerPanel.setBackground(Color.green);
    bottomPanel.setBackground(Color.blue);
    leftPanel.setBackground(Color.white);
  
  leftPanel.setLayout(new GridBagLayout());
  GridBagConstraints gbc01 = new GridBagConstraints();
  gbc01.gridx = 0;
  gbc01.gridy = 0;
  gbc01.insets = new Insets(0, 0, 5, 5);
  gbc01.anchor = GridBagConstraints.PAGE_END;
  leftPanel.add(regTitle, gbc01);
  
  GridBagConstraints gbc02 = new GridBagConstraints();
  gbc02.gridx = 0;
  gbc02.gridy = 1;
  gbc02.insets = new Insets(5, 5, 5, 5);
  leftPanel.add(regUser, gbc02);
  
  GridBagConstraints gbc03 = new GridBagConstraints();
  gbc03.gridx = 1;
  gbc03.insets = new Insets(5, 5, 5, 5);
  gbc03.gridy = 1;
  gbc03.gridwidth = 2;
  gbc03.fill = GridBagConstraints.BOTH;
  leftPanel.add(regUser2, gbc03);
  
  GridBagConstraints gbc04 = new GridBagConstraints();
  gbc04.gridx = 0;
  gbc04.gridy = 2;
  gbc04.insets = new Insets(5, 5, 5, 5);
  leftPanel.add(regPassword, gbc04);
  
  GridBagConstraints gbc05 = new GridBagConstraints();
  gbc05.gridx = 2;
  gbc05.insets = new Insets(5, 5, 5, 5);
  gbc05.gridy = 2;
  gbc05.gridwidth = 2;
  gbc05.fill = GridBagConstraints.BOTH;
  leftPanel.add(regPassword2, gbc05);
  
  
  

        
  
  
}


public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
  public void run(){
   new Album();
  }
});

}
}



User is offlineProfile CardPM
+Quote Post

GWatt
RE: Problem With GridBagLayout
7 Sep, 2008 - 11:24 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,177



Thanked: 18 times
Dream Kudos: 450
My Contributions
I modified the code for positioning the title. The important part was giving it a weighty of .1.
java

GridBagConstraints gbc01 = new GridBagConstraints();
gbc01.gridx = 0;
gbc01.gridy = 0;
gbc01.weighty = .1;
gbc01.gridwidth = 3;
gbc01.insets = new Insets(0, 0, 5, 5);
gbc01.anchor = GridBagConstraints.PAGE_START;
leftPanel.add(regTitle, gbc01);


I then added an empty component to the bottom of the layout and gave it a weighty of .1 as well.
java

GridBagConstraints gbc06 = new GridBagConstraints();
gbc06.gridx = 0;
gbc06.gridy = 3;
gbc06.weighty = .1;
leftPanel.add(new Panel(), gbc06);


It ended up looking like this:
IPB Image

This post has been edited by GWatt: 7 Sep, 2008 - 11:25 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 04:06PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month