1 Replies - 78 Views - Last Post: 08 February 2012 - 09:08 AM Rate Topic: -----

Topic Sponsor:

#1 jimmyo88  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 126
  • Joined: 27-February 11

Gridbag Constraints Problem

Posted 07 February 2012 - 07:20 PM

I am having a problem with displaying an image as well as a number of textfields inside a JPanel. I was told to use a gridbagConstraints style layout but am having problems positioning the components.
If you look at the attached image, there is a large space between the labels, textfields and a large space around the image.
This is about the closest I've been able to get it by messing around with the gbc.gridheight and gbc.weightx/y.
Could anyone help me remove this space please.

this is my code
package jazzyjunglesystem;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public class Gui extends JFrame implements ActionListener  {
//link to mainSystem    
    MainSystem MS = new MainSystem(this);
//panel
    JPanel  customer = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    JPanel imageHolder = new JPanel ();
    JPanel outputArea = new JPanel();
    JScrollPane sp = new JScrollPane();

//images
        JLabel image = new JLabel();
        JLabel image2 = new JLabel();
    
//Tables
   JTable table1 = new JTable();
    
    
//colours
    Color background = new Color(34, 177, 76);

//menu
    JMenu       menu = new JMenu("Customer");
    JMenuBar    menuBar = new JMenuBar();
//buttons
    JButton     save = new JButton("Save");
    JButton     newc = new JButton("New Customer");
//textfields
    JTextField TFname =  new JTextField(20);
    JTextField TLname =  new JTextField(20);
    JTextField THouseNumber = new JTextField(20);
    JTextField TStreetName = new JTextField(20);
    JTextField TTown = new JTextField(20);
    JTextField TCity = new JTextField(20);
    JTextField TPNumber = new JTextField(20);
    JTextField TCName = new JTextField(20);
    JTextField TCAge = new JTextField(20);
    JTextField TEAddress = new JTextField(20);
    JTextField TPCode = new JTextField(20);
//labels
    JLabel fname = new JLabel ("First Name");
    JLabel lname = new JLabel ("Last Name");
    JLabel hnumber = new JLabel ("House No.");
    JLabel sname = new JLabel ("Street Name");
    JLabel town = new JLabel ("Town");
    JLabel city = new JLabel ("City");
    JLabel pnumber = new JLabel ("Phone Number");
    JLabel cname = new JLabel("Child Name");
    JLabel cage = new JLabel("Child Age");
    JLabel eaddress = new JLabel("email Address");
    JLabel pcode = new JLabel("Post Code");
    
    public Gui() {

        //set JFrame properties
        super("Jazzy Jungle");
        
        //add an image
        image.setIcon(new ImageIcon("C:\\Users\\James\\Documents\\JazzyJungleSystem\\src\\addCustomer.jpg"));
        image2.setIcon(new ImageIcon("C:\\Users\\James\\Documents\\JazzyJungleSystem\\src\\customerService.jpg"));
        imageHolder.add(image);

        //Set Look and Feel        
        try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
        System.err.println("Can't set look and feel: " + e);
        }
        
        //set frame settings
        setVisible(true);
        setExtendedState(MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //add action listeners
        save.addActionListener(this);
        newc.addActionListener(this);
        
        //add gridbag 
        gbc.insets = new Insets(3,3,3,3);
        
        gbc.gridx = 0;
        gbc.gridy = 0;
                   
        // Name the JMenu & Add Items
        menu.add(makeMenuItem("New"));
        menu.add(makeMenuItem("Search"));
        menu.add(makeMenuItem("Delete"));
 
        // Add JMenu bar
        menuBar.add(menu);
        setJMenuBar(menuBar);
        
        //add panels
        customer.setVisible(false);
        add(customer, BorderLayout.WEST);
        customer.setBackground(Color.LIGHT_GRAY);
        customer.setBorder(BorderFactory.createLineBorder(Color.black));


  
        imageHolder.setVisible(false);
        add(imageHolder, BorderLayout.NORTH);
        imageHolder.setBackground(background);
        imageHolder.setBorder(BorderFactory.createLineBorder(Color.black));
        
        outputArea.setVisible(false);
        add(outputArea, BorderLayout.CENTER);
        outputArea.setBackground(background);
        outputArea.setBorder(BorderFactory.createLineBorder(Color.black));
        outputArea.setPreferredSize(new Dimension(1000,50));

        //add labels and textfields
        gbc.gridheight = 1;       
        gbc.weighty = 1;
        gbc.gridx = 1; 
        gbc.gridy = 0;
        customer.add(image2, gbc); 

        gbc.weighty = 0;
        gbc.gridx = 0; 
        gbc.gridy = 1;
        customer.add(fname, gbc); 
               
        gbc.gridx = 3; 
        gbc.gridy = 1;
        customer.add(TFname, gbc); 
        
        gbc.gridx = 0; 
        gbc.gridy = 2;
        customer.add(lname, gbc);
        gbc.gridx = 3; 
        gbc.gridy = 2;
        customer.add(TLname, gbc);
         gbc.weighty = 0;
        gbc.gridx = 0; 
        gbc.gridy = 3;
        customer.add(hnumber, gbc);
        gbc.gridx = 3;
        gbc.gridy = 3;
        customer.add(THouseNumber, gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 4;
        customer.add(sname, gbc);
        gbc.gridx = 3;
        gbc.gridy = 4;
        customer.add(TStreetName, gbc);
        
        gbc.gridx = 0; 
        gbc.gridy = 5;
        customer.add(town, gbc);
        gbc.gridx = 3;
        gbc.gridy = 5;
        customer.add(TTown, gbc);
        
        gbc.gridx = 0; 
        gbc.gridy = 6;
        customer.add(city, gbc);
        gbc.gridx = 3;
        gbc.gridy = 6;
        customer.add(TCity, gbc);
        
        gbc.gridx = 0; 
        gbc.gridy = 7;
        customer.add(cname, gbc);
        gbc.gridx = 3; 
        gbc.gridy = 7;
        customer.add(TCName, gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 8;
        customer.add(pnumber, gbc);
        gbc.gridx = 3;
        gbc.gridy = 8;
        customer.add(TPNumber, gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 9;
        customer.add(cage, gbc);
        gbc.gridx = 3;
        gbc.gridy = 9;
        customer.add(TCAge, gbc);
 
        gbc.gridx = 0;
        gbc.gridy = 10;        
        customer.add(eaddress, gbc);
        gbc.gridx = 3;
        gbc.gridy = 10;
        customer.add(TEAddress, gbc);
 
       gbc.gridx = 0; 
       gbc.gridy = 11; 
       customer.add(pcode, gbc);
       gbc.gridx = 3;
       gbc.gridy = 11; 
       customer.add(TPCode, gbc);     
       
       gbc.gridx = 0; 
       gbc.gridy = 12; 
       customer.add(save, gbc);
       gbc.gridx = 3;
       gbc.gridy = 12; 
       customer.add(save, gbc); 
       
       gbc.gridx = 0; 
       gbc.gridy = 12; 
       customer.add(newc, gbc);
       gbc.gridx = 2;
       gbc.gridy = 12; 
       customer.add(newc, gbc);
       
       //set button displays
       newc.setVisible(false);
    }



Many thanks to anyone who can help.

Attached image(s)

  • Attached Image

This post has been edited by jimmyo88: 07 February 2012 - 07:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Gridbag Constraints Problem

#2 jimmyo88  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 126
  • Joined: 27-February 11

Re: Gridbag Constraints Problem

Posted 08 February 2012 - 09:08 AM

just to say, i sorted this problem.
I believe i was using the weightx and weighty parameters wrong.

This tutorial cleared things up for me if anyone else is having a similar problem

http://www.youtube.c...h?v=YKaea4ezQQE
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1