aspose file tools*
The moose likes Beginning Java and the fly likes simple program with a panel class and actionlistener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple program with a panel class and actionlistener" Watch "simple program with a panel class and actionlistener" New topic
Author

simple program with a panel class and actionlistener

alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
I am writting a small program that displays a tiny integer calculator. I'm having problems with my panel class. It has 4 labels, 2 text fields, and 2 buttons. I added the actionlistener to the components being used and under the actionlistener class i can't get the value that the user enters to be set. Here's my code....

private class CalcListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
IntegerCalculator myCalculator = new IntegerCalculator();

String text = operand1.getText();
myCalculator = Integer.parseInt(text);
operand1.setText("");

my error states that it can not find variable "text"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Alaina,

Welcome to JavaRanch!

We may need to back up a little. In the code you've shown, I don't see where there'd be a message about the variable "text". But the variable myCalculator is declared to be of type IntegerCalculator, and a few lines later you try to assign the result of Integer.parseInt() -- an int -- to it. That's not going to compile right there. You need a new variable of "int" type to hold that number. Make that change, then cut and paste your compiler errors for us. Make sure to show us any specific lines of code mentioned in the error messages, and we'll try to help you understand what's going wrong.


[Jess in Action][AskingGoodQuestions]
alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
ok i dont understant completely. I think i have fixed it a little, but now im getting a run-time error. In my first class called IntegerCalculator i have set these methods to set and get the values, and to add and subtract. Here is my code and my error
public int getoperand1()
{
return operand1;

}
public int getoperand2()
{
return operand2;

}

public int getresult()
{
return result;
}
public int add()
{
result = operand1 + operand2;
return result;
}

public int subtract()
{
result = operand1 - operand2;
return result;
}

and here is my panel with private class buttonlistener
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{

if (event.getSource() == push1)
{
myCalculator.add();
result.setText("Result: " + result);
}
else
if (event.getSource() == push2)
{
myCalculator.subtract();
result.setText("Result: " + result);
}

my error states it can not find symbol: class buttonlistener
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You state that you are getting a runtime error, then write that it is
"can not find symbol: class buttonlistener". That sounds like a compile-time
error. Are you aware that class names are case sensitive: buttonlistener
and ButtonListener will be different classes?


There is no emoticon for what I am feeling!
alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
ok i changed my buttonlisteners and added them with my CalcListener class inside of my panel. Inside the CalcListener is where i am trying to get and set the two values that the user inputs, and then perform actions when the buttons are pushed. Here is my code

public class IntegerCalculatorPanel extends JPanel
{
private JLabel inputlabel1 = new JLabel("Enter first operand: ");
private JLabel inputlabel2 = new JLabel("Enter second operand: ");
private JLabel Instruction = new JLabel("Select operation: ");
private JLabel result = new JLabel("Result: ");
private JButton push1 = new JButton("+");
private JButton push2 = new JButton("-");
private JTextField operand1 = new JTextField(5);
private JTextField operand2 = new JTextField(5);
IntegerCalculator myCalculator;


/** Creates a new instance of IntegerCalculatorPanel */
public IntegerCalculatorPanel()
{
IntegerCalculator myCalculator = new IntegerCalculator();

add(inputlabel1);
add(operand1);
add(inputlabel2);
add(operand2);
add(Instruction);
add(push1);
add(push2);
add(result);

setPreferredSize (new Dimension(300, 80));

operand1.addActionListener(new CalcListener());

operand2.addActionListener(new CalcListener());


push1.addActionListener(new CalcListener());

push2.addActionListener(new CalcListener());



}

private class CalcListener implements ActionListener
{
public void actionPerformed (ActionEvent event)

/**How do i proceed from here?, i need valid code's to set the values when they are entered and then code for my buttons that will eithor subtract or add these values, depending on which button is pushed.*//
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Your class CalcListener is an inner class so it has access to its parent's fields:
As for deciding to add or substract, you have two options:
1. Write a single class with a case analysis of who the source is:
Or you could define separate listener classes for each operation,
or even better, parameterize your listener class by a binary operation:

Most Java programmers would go these latter routes, but that could just be me talking...

By the way, you are declaring myCalculator as a local variable in your
constructor, and not access instance field myCalculator.
[ December 06, 2005: Message edited by: Jeff Albrechtsen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple program with a panel class and actionlistener