12 Replies - 1345 Views - Last Post: 26 February 2011 - 01:11 PM Rate Topic: -----

#1 mdaniel1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-February 11

clear values

Posted 25 February 2011 - 02:09 PM

I have the following code that changes the colors on a checkerboard when the go button is clicked, however, I don't know how to utilize the clear button to turn everything back to white. here is the code that I have so far, and it compiles ok.
import java.awt.*;
import java.awt.event.*;

public class Checkerboard extends Frame implements ActionListener
{
	int start, stop, step;

	Panel boardPanel = new Panel();
		TextArea boardDisplay[] = new TextArea[16];

	Panel buttonPanel = new Panel();
			Button goButton = new Button("Go");
			Button clearButton = new Button("Clear");

	Panel inputPanel = new Panel();
			Label startLabel = new Label("Start");
			TextField startField = new TextField(10);
			Label stopLabel = new Label("Stop");
		    TextField stopField = new TextField(10);
		    Label stepLabel = new Label ("Step");
		    TextField stepField = new TextField (10);

public Checkerboard()
	{
		//set Layouts for frame and three panels
		this.setLayout(new BorderLayout());
			boardPanel.setLayout(new GridLayout(4,4,15,15));
			inputPanel.setLayout(new GridLayout(2,3,15,15));
			buttonPanel.setLayout(new GridLayout(1,2,15,15));

		//add components to board panel
		for (int i = 0; i<16; i++)
		{
			boardDisplay[i] = new TextArea(null,4,4,3);
            boardDisplay[i].setText(""+i);
			boardDisplay[i].setEditable(false);
			boardDisplay[i].setBackground(Color.white);
			boardPanel.add(boardDisplay[i]);
		}

		//add components to input panel

		inputPanel.add(startField);
		inputPanel.add(stopField);
		inputPanel.add(stepField);
		inputPanel.add(startLabel);
		inputPanel.add(stopLabel);
		inputPanel.add(stepLabel);
		//add components to button panel
		buttonPanel.add(goButton);
		buttonPanel.add(clearButton);

		//add panels to frame
		add(buttonPanel, BorderLayout.SOUTH);
		add(inputPanel, BorderLayout.CENTER);
		add(boardPanel, BorderLayout.NORTH);

		goButton.addActionListener(this);
		clearButton.addActionListener(this);

		//overriding the windowClosing() method will allow the user to click the Close button
		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
	} //end of constructor method

    public void actionPerformed(ActionEvent e)
    {
		//read user input into variables
		int start = Integer.parseInt(startField.getText());
		int stop = Integer.parseInt(stopField.getText());
		int step = Integer.parseInt(stepField.getText());

		for (int i=0; i<16; i++)
		{
			boardDisplay[i].setBackground(Color.magenta);
		}
		for (int i=start; i<stop; i+=step)
		{
			boardDisplay[i].setBackground(Color.yellow);
		}
	}
	public static void main(String[] args)
		{
			Checkerboard f = new Checkerboard();
			f.setBounds(50,100,300,400);
			f.setTitle("Checkerboard Array");
			f.setVisible(true);
	} //end of main
}


This post has been edited by macosxnerd101: 25 February 2011 - 02:23 PM
Reason for edit:: Please use code tags!!!


Is This A Good Question/Topic? 0
  • +

Replies To: clear values

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,386
  • Joined: 27-December 08

Re: clear values

Posted 25 February 2011 - 02:28 PM

You need to add an ActionListener to the clear Button. Since your class implements ActionListener, you can add this as an ActionListener to the clear Button. Note that since your class (or more specifically, each instance of the class) listens to multiple Buttons, you'll want to check which one was clicked using the ActionEvent getSource() method.

Also, not good practice to use AWT Components. Use the more modern Swing JComponents like JButton, JPanel, JLabel, etc. Also, use the more modern JFrame over Frame.
Was This Post Helpful? 0
  • +
  • -

#3 mdaniel1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-February 11

Re: clear values

Posted 25 February 2011 - 02:39 PM

View Postmacosxnerd101, on 25 February 2011 - 02:28 PM, said:

You need to add an ActionListener to the clear Button. Since your class implements ActionListener, you can add this as an ActionListener to the clear Button. Note that since your class (or more specifically, each instance of the class) listens to multiple Buttons, you'll want to check which one was clicked using the ActionEvent getSource() method.

Also, not good practice to use AWT Components. Use the more modern Swing JComponents like JButton, JPanel, JLabel, etc. Also, use the more modern JFrame over Frame.


Sorry, I am really new to this. I don't even know how the go button worked. How with the current code that I have, tell it or add an ActionListener to the clear button?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,386
  • Joined: 27-December 08

Re: clear values

Posted 25 February 2011 - 03:05 PM

Copying code, especially when you don't understand the concepts being used, won't help you learn. I highly recommend you check out the Oracle and DIC Java tutorials covering Swing and GUIs.

Also, take a look at the documentation for Button and JButton, specifically the addActionListener() method.
Was This Post Helpful? 0
  • +
  • -

#5 mdaniel1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-February 11

Re: clear values

Posted 26 February 2011 - 10:51 AM

I am very new, and I am trying to teach myself going through a text book. My latest lesson wants me to use a try and catch to throw an error if text is entered in the 3 Fields and not numbers (integers). My one problem is the lesson specifically says to build this WITHOUT JOptionPane, and I am lost. I don't know whether to enter a boolean statement, etc... Any help would be appreciated.
Thanks
Here is the code I have. I am pretty sure that whatever the code is I need to insert it after the- public void actionPerformed(ActionEvent e)
import java.awt.*;
import java.awt.event.*;

public class Checkerboard extends Frame implements ActionListener
{
	int start, stop, step;

	Panel boardPanel = new Panel();
		TextArea boardDisplay[] = new TextArea[16];

	Panel buttonPanel = new Panel();
			Button goButton = new Button("Go");
			Button clearButton = new Button("Clear");

	Panel inputPanel = new Panel();
			Label startLabel = new Label("Start");
			TextField startField = new TextField(10);
			Label stopLabel = new Label("Stop");
		    TextField stopField = new TextField(10);
		    Label stepLabel = new Label ("Step");
		    TextField stepField = new TextField (10);

public Checkerboard()
	{
		//set Layouts for frame and three panels
		this.setLayout(new BorderLayout());
			boardPanel.setLayout(new GridLayout(4,4,15,15));
			inputPanel.setLayout(new GridLayout(2,3,15,15));
			buttonPanel.setLayout(new GridLayout(1,2,15,15));

		//add components to board panel
		for (int i = 0; i<16; i++)
		{
			boardDisplay[i] = new TextArea(null,4,4,3);
            boardDisplay[i].setText(""+i);
			boardDisplay[i].setEditable(false);
			boardDisplay[i].setBackground(Color.white);
			boardPanel.add(boardDisplay[i]);
		}

		//add components to input panel

		inputPanel.add(startField);
		inputPanel.add(stopField);
		inputPanel.add(stepField);
		inputPanel.add(startLabel);
		inputPanel.add(stopLabel);
		inputPanel.add(stepLabel);
		//add components to button panel
		buttonPanel.add(goButton);
		buttonPanel.add(clearButton);

		//add panels to frame
		add(buttonPanel, BorderLayout.SOUTH);
		add(inputPanel, BorderLayout.CENTER);
		add(boardPanel, BorderLayout.NORTH);

		goButton.addActionListener(this);
		clearButton.addActionListener(this);

		//overriding the windowClosing() method will allow the user to click the Close button
		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
	} //end of constructor method

    public void actionPerformed(ActionEvent e)
    {
		//read user input into variables
		int start = Integer.parseInt(startField.getText());
		int stop = Integer.parseInt(stopField.getText());
		int step = Integer.parseInt(stepField.getText());
   		for (int i=0; i<16; i++)
		{
			boardDisplay[i].setBackground(Color.magenta);
		}
		for (int i=start; i<=stop; i+=step)
		{
			boardDisplay[i].setBackground(Color.yellow);
		}
	}
	public static void main(String[] args)
		{
			Checkerboard f = new Checkerboard();
			f.setBounds(50,100,300,400);
			f.setTitle("Checkerboard Array");
			f.setVisible(true);
	} //end of main
}


This post has been edited by macosxnerd101: 26 February 2011 - 01:04 PM
Reason for edit:: Please use code tags!!

Was This Post Helpful? 0
  • +
  • -

#6 ScottH323  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-February 11

Re: clear values

Posted 26 February 2011 - 10:55 AM

Try this:

line after the action performed put in:
try {


then at the end before the action performed closing bracket put:
        } catch (Exception ex) {
            ex.printStackTrace();
        }



that is just catching any exception/error and printing it to the console.
Was This Post Helpful? 0
  • +
  • -

#7 mdaniel1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-February 11

Re: clear values

Posted 26 February 2011 - 11:08 AM

That compiles, but the problem is, I need it to display an error message, and I only get the dos error. let me know
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 ScottH323  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-February 11

Re: clear values

Posted 26 February 2011 - 11:17 AM

you could use one of the lables that would change when the error occurs. in the catch statement just put

jlabel.setText(ex);


you will have to make another label ofcourse.
Was This Post Helpful? 0
  • +
  • -

#9 mdaniel1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-February 11

Re: clear values

Posted 26 February 2011 - 11:28 AM

View PostScottH323, on 26 February 2011 - 11:17 AM, said:

you could use one of the lables that would change when the error occurs. in the catch statement just put

jlabel.setText(ex);


you will have to make another label ofcourse.


Do you mean a public class label? or what?
without changing anything, I entered jlabel into catch statement and got the following error.

C:\Users\Mark Daniel\Documents\Java\Checkerboard.java:101: cannot find symbol
symbol : variable jlabel
location: class Checkerboard
jlabel.setText(ex);
^
1 error

Tool completed with exit code 1
Was This Post Helpful? 0
  • +
  • -

#10 ScottH323  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-February 11

Re: clear values

Posted 26 February 2011 - 11:39 AM

you need to declare it:

Label ErrorLabel = new Label("");

Was This Post Helpful? 0
  • +
  • -

#11 mdaniel1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-February 11

Re: clear values

Posted 26 February 2011 - 12:11 PM

View PostScottH323, on 26 February 2011 - 11:39 AM, said:

you need to declare it:

Label ErrorLabel = new Label("");


I entered in that as show
 public void actionPerformed(ActionEvent e)
    {
		//read user input into variables
		int start = Integer.parseInt(startField.getText());
		int stop = Integer.parseInt(stopField.getText());
		int step = Integer.parseInt(stepField.getText());
		Label ErrorLabel = new Label("jlabel");
   		for (int i=0; i<16; i++)
   		{
					boardDisplay[i].setBackground(Color.magenta);
				}
				for (int i=start; i<=stop; i+=step)
				{
					boardDisplay[i].setBackground(Color.yellow);
				}

   		try
   		{

		}
        catch (Exception ex)
        jlabel.setText(ex);
        {

       		}

	}
	public static void main(String[] args)
		{
			Checkerboard f = new Checkerboard();
			f.setBounds(50,100,300,400);
			f.setTitle("Checkerboard Array");
			f.setVisible(true);
	} //end of main
}


here is the message
C:\Users\Mark Daniel\Documents\Java\Checkerboard.java:102: '{' expected
        jlabel.setText(ex);
        ^
C:\Users\Mark Daniel\Documents\Java\Checkerboard.java:108: illegal start of expression
	public static void main(String[] args)
        ^
C:\Users\Mark Daniel\Documents\Java\Checkerboard.java:114: ';' expected
	} //end of main
         ^
C:\Users\Mark Daniel\Documents\Java\Checkerboard.java:116: '}' expected
^
4 errors

Tool completed with exit code 1


Thanks

disregard that last one, I realized after I sent it that I put the code in above the bracket. sorry,
I entered it correctly and got this error.

C:\Users\Mark Daniel\Documents\Java\Checkerboard.java:104: cannot find symbol
symbol  : variable jlabel
location: class Checkerboard
jlabel.setText(ex);
^
1 error

Tool completed with exit code 1


Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,386
  • Joined: 27-December 08

Re: clear values

Posted 26 February 2011 - 01:06 PM

Related threads merged. Please avoid duplicate posting.
Was This Post Helpful? -1
  • +
  • -

#13 mdaniel1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 25-February 11

Re: clear values

Posted 26 February 2011 - 01:11 PM

View Postmacosxnerd101, on 26 February 2011 - 01:06 PM, said:

Related threads merged. Please avoid duplicate posting.

sorry didn't know that I duplicated a posting, was only hitting reply, just like this one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1