
-----------------------------------
chrisciscoioio
Wed Oct 31, 2007 2:21 pm

Help with Grid Bag Layout
-----------------------------------
Hello,

So as stated I am having trouble using Grid Bag Layout to create a layout for my program I am writing in class.
Now, before I go any farther, what I am writing is beyond the scope of the actual assignment.

So on to the problem, I am creating a layout using the above stated, I have a grid that is 3 columns by 4 rows of space that I will use, you can look at Diagram.jpg in the attached zip file. You can also see the image of the program running and why there is a problem in Error.jpg.

The code in question

Class userInterface()

So first forguve my lack of comments, because I have not finsihing them, but everything is grouped so it should be easy to follow.

The problem is with the addComponent section.

Now that calls a custom method with 5 agruments, the name of the object to create and the 4 corrds, being topx, topy, bottomx, bottomy.

Now if you look at the code it should be layed out like the Disgram, but instead it looks like the Error.

Now from what I can see is that the button push up and do not stay to there locations. they also force off the reset buttons.

// Imports
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JOptionPane;

// Start of UserInterface
public class UserInterface extends JFrame
{

	// Object Reference Variables
	// StopWatch1 UserInterface delcares
	private JLabel labelTime1;
	private JButton stopTime1;
	private JButton startTime1;
	private JButton resetTime1;
	private JTextField showTime1;

	// StopWatch2 UserInterface delcares
	private JLabel labelTime2;
	private JButton stopTime2;
	private JButton startTime2;
	private JButton resetTime2;
	private JTextField showTime2;

	// Gridbag Reference variables
	private GridBagLayout layout;
	private GridBagConstraints constraints;

	// Primative Variables
	private boolean StopTime1, StopTime2;
	private boolean ResetTime1, ResetTime2;

	private int tempHours1 = 0;
	private int tempMinutes1 = 0;
	private int tempSeconds1 = 0;
	private int tempTenths1 = 0;

	private int tempHours2 = 0;
	private int tempMinutes2 = 0;
	private int tempSeconds2 = 0;
	private int tempTenths2 = 0;


	// Start of Constructor UserInterface
	public UserInterface()
	{
		super("Stop Watch");
		layout= new GridBagLayout();
		setLayout(layout);
		constraints = new GridBagConstraints();

		// StopWatch1 UserInterface objects
		labelTime1 = new JLabel("Stop Watch One: ");
		showTime1 = new JTextField("00:00:00:00",11);
			showTime1.setEditable(false);
		stopTime1 = new JButton("Stop Time One");
		startTime1 = new JButton("Continue Time One");
		resetTime1 = new JButton("Reset Time One");

		// StopWatch2 UserInterface objects
		labelTime2 = new JLabel("Stop Watch Two: ");
		showTime2 = new JTextField("00:00:00:00",11);
			showTime2.setEditable(false);
		stopTime2 = new JButton("Stop Time Two");
		startTime2 = new JButton("Continue Time Two");
		resetTime2 = new JButton("Reset Time Two");

		constraints.fill = GridBagConstraints.RELATIVE;

		// Column One
		addComponent(labelTime1, 0, 0, 1, 1);
		addComponent(stopTime1, 0, 1, 1, 2);
		addComponent(labelTime2, 0, 2, 1, 3);
		addComponent(stopTime2, 0, 3, 1, 4);

		// Column Two
		addComponent(showTime1, 1, 0, 2, 1);
		addComponent(startTime1, 1, 1, 2, 2);
		addComponent(showTime2, 1, 2, 2, 3);
		addComponent(startTime2, 1, 3, 2, 4);

		// Colum Three
		addComponent(resetTime1, 2, 1, 3, 2);
		addComponent(resetTime2, 2, 3, 3, 4);


		ButtonHandler handler = new ButtonHandler();

		// StopWatch1 UserInterface handlers
		stopTime1.addActionListener(handler);
		startTime1.addActionListener(handler);
		resetTime1.addActionListener(handler);

		// StopWatch2 UserInterface handlers
		stopTime2.addActionListener(handler);
		startTime2.addActionListener(handler);
		resetTime2.addActionListener(handler);
	}
	// End of Constructor UserInterface

	// Start of addComponent
	private void addComponent(Component component, int column, int row, int width, int height)
	{
		constraints.gridx = column;
		constraints.gridy = row;
		constraints.gridwidth = width;
		constraints.gridheight = height;
		layout.setConstraints(component, constraints);
		add(component);
	}
	// End of addComponent

	// Start of txtShowTime1
	public void txtShowTime1(int hours, int minutes, int seconds, int tenths)
	{

		if (StopTime1)
		{
			showTime1.setText(tempHours1 + ":" + tempMinutes1 + ":" + tempSeconds1 + ":" + tempTenths1);
		}
		else
		{
			tempHours1 = hours;
			tempMinutes1 = minutes;
			tempSeconds1 = seconds;
			tempTenths1 = tenths;

			showTime1.setText(hours + ":" + minutes + ":" + seconds + ":" + tenths);
		}
	}
	// End of txtShowTime1

	// Start of txtShowTime2
	public void txtShowTime2(int hours, int minutes, int seconds, int tenths)
	{
		if (StopTime2)
		{
			showTime2.setText(tempHours2 + ":" + tempMinutes2 + ":" + tempSeconds2 + ":" + tempTenths2);
		}
		else
		{
			tempHours2 = hours;
			tempMinutes2 = minutes;
			tempSeconds2 = seconds;
			tempTenths2 = tenths;

			showTime2.setText(hours + ":" + minutes + ":" + seconds + ":" + tenths);
		}
	}
	// End of txtShowTime2

	// Start of checkReset1
	public boolean checkReset1()
	{
		if (ResetTime1)
		{
			ResetTime1 = false;
			return true;
		}
		else
		{
			return false;
		}
	}
	// End of checkReset1

	// Start of checkReset2
	public boolean checkReset2()
	{
		if (ResetTime2)
		{
			ResetTime2 = false;
			return true;
		}
		else
		{
			return false;
		}
	}
	// End of checkReset2


	// Start of Inner Class ButtonHanlder
	private class ButtonHandler implements ActionListener
		{

			public void actionPerformed(ActionEvent event)
			{
				// This is were we handle clicks
				if (event.getSource() == stopTime1)
				{
					StopTime1 = true;
				}

				if (event.getSource() == startTime1)
				{
					StopTime1 = false;
				}

				if (event.getSource() == stopTime2)
				{
					StopTime2 = true;
				}

				if (event.getSource() == startTime2)
				{
					StopTime2 = false;
				}

				if (event.getSource() == resetTime1)
				{
					ResetTime1 = true;
				}

				if (event.getSource() == resetTime2)
				{
					ResetTime2 = true;
				}
			}
	}
	// End of Inner Class ButtonHanlder

}
// End of UserInterface



So if you have any questions I will read them when I get a chance and any help would be great.
