Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event Handling - Access to object instance from method actionPerformed

Status
Not open for further replies.

Marinova

Programmer
May 4, 2003
4
DE
Hello everyone,

i implemented an ActionListener in a GUI and want to access an attribute of the main class object instance from within method public void actionPerformed(ActionEvent e).

The trouble is that the method does not recognize the object instance which was created in the main().

Please tell me how to handle this.

Here piece of the code:


public class GUI extends Frame implements ActionListener{

public GUI()
{...
JRadioButton byteSelected = new JRadioButton("Byteorientiert",false);
byteSelected.addActionListener(this);
...}

public static void main (String args[])
{
GUI frame = new GUI();
...}



public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String buttonClicked = e.getActionCommand();

if ((source == this().byteSelected) && (buttonClicked.equals("Text speichern")))
{
saveByte(this().input.getText());
this().input.setText("");
}


}

I shall be grateful if somebody gives me advice about it.

Thanks,

Maria

 
In the method actionPerformed only members of the class GUI are accessible.
Sollution:

public class GUI extends Frame implements ActionListener{
JRadioButton byteSelected=new JRadioButton("Byteorientiert",false);
public GUI()
{...
byteSelected.addActionListener(this);
...}

public static void main (String args[])
{
GUI frame = new GUI();
...}



public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String buttonClicked = e.getActionCommand();

if ((source == byteSelected) && (buttonClicked.equals("Text speichern")))
{
saveByte(this().input.getText());
this().input.setText("");
}

}
}
 
Hi Stijn147,

my problem still persists. This is the contructor:

public GUI()
{
super("Byte-/Zeichenorientierter Text Input/Output");
setVisible(true);
setSize(700,500);
setLayout(new BorderLayout());
TextArea input = new TextArea("",10,40, TextArea.SCROLLBARS_VERTICAL_ONLY);
add(input, BorderLayout.NORTH);
...

JRadioButton byteSelected = new JRadioButton("Byteorientiert",false);
JRadioButton charSelected = new JRadioButton("Zeichenorientiert",true);
charSelected.addActionListener(this);
byteSelected.addActionListener(this);
byteSelected.setActionCommand("Save to Byte");

...
}



- the event handling method cannot resolve following symbols:

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String buttonClicked = e.getActionCommand();

if ((source == byteSelected) && (buttonClicked.equals("Text speichern")))
{
saveByte(input.getText());
(input.setText("");
}


}


I think this is a principal problem which I encounter all the time - the methods other than main() cannot resolve attributes if the public class instance created in main().

HOw do I have to build methods so that they can perform actions on instances of the public class?

Thanks,

Maria
 
Put the line TextArea input = new TextArea(...); also outside your constructor.

Why do I have to declare JRadioButton byteSelected and TextArea input as members of GUI?

When your program starts the main method is called.
GUI frame = new GUI(); This makes a new instance of the class GUI (allocates memory for his members) and calls the constructor. Every variable that has been declared in the constructor, will be destroyed by the garbage collector when the constructor is finished. Thus it is important to make Objects that you need afterwards, members of the class GUI.



How do I have to build methods so that they can perform actions on instances of the public class?

Every non-static method does that. Non-static methods can access the members of the class.
Every method is automaticaly non-static except when the keyword static is used.




 
Thanks very much.

It worked!
The program was compiled successfully, but the thing now is that the FileDialog does not appear. I have declared it with these lines of code:

FileDialog saveDialog = new FileDialog(this,"",FileDialog.SAVE);
saveDialog.setVisible(true);

Then follow the lines which save text to file byte-oriented:

byte b[] = text.getBytes();
File outputFile = new File(saveDialog.getFile());
FileOutputStream out = new FileOutputStream(outputFile);
out.write(b);
out.close();

Nevertheless, when i click on Button "SAve", no dialog appears.

I would be grateful if you could help me.

Maria
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top