Click to See Complete Forum and Search --> : ActionListener... Im loosing my mind.. Please help ;)


jaskali
November 7th, 2006, 05:29 AM
The CODE is:
//testi.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class testi extends JPanel{

/**
* @param args
*/
public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() { createAndShowGUI(); }
} );

}

static JTextField tx01=new JTextField();
static JTextField tx02=new JTextField();
static JTextField tx03=new JTextField();

public class PushButtonActionListener implements ActionListener{


JFrame frame = null;
public PushButtonActionListener(JFrame af) {
this.frame = af;
}


public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("count")){

try
{
int value1 = Integer.valueOf(tx01.getText()).intValue();
int value2 = Integer.valueOf(tx03.getText()).intValue();
tx02.setText(Integer.toString(value1*value2));
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Error");
}
}
}
}

public static void createAndShowGUI(){
JFrame frame = new JFrame("Area");

//frame.getContentPane().add(paneeli);
frame.setLayout(new GridLayout(4,2));

JButton bt01=new JButton("Count");
bt01.setActionCommand("count");


JPanel paneeli = new JPanel();
paneeli.setPreferredSize(new Dimension(200,100));

JLabel lb01=new JLabel("Width");
JLabel lb02=new JLabel("Height");
JLabel lb03=new JLabel("Area");
JLabel lb04=new JLabel(" ");

frame.getContentPane().add(lb01);
frame.getContentPane().add(lb03);
frame.getContentPane().add(tx01);
frame.getContentPane().add(tx02);
frame.getContentPane().add(lb02);
frame.getContentPane().add(lb04);
frame.getContentPane().add(tx03);
frame.getContentPane().add(bt01);
//----------------------------------------------------
bt01.addActionListener(new PushButtonActionListener(frame)); //THIS IS THE LINE where both errors occur
//-----------------------------------------------------
frame.pack();
frame.setVisible(true);
}

}


i get this error message in eclipse 3.2:

"No enclosing instance of type testi is accessible. Must qualify the allocation with an enclosing instance of type testi (e.g. x.new A() where x is an instance of testi). "

and if i create new interface file Actionlistener.java i get error:

"The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (testi.PushButtonActionListener)"

wildfrog
November 7th, 2006, 05:50 AM
No enclosing instance of type testi is accessible. Must qualify the allocation with an enclosing instance of type testi (e.g. x.new A() where x is an instance of testi). "

Did you try this solution? Like:

bt01.addActionListener(yourTesti.new PushButtonActionListener(frame));
?
Anyway, please use code-tags when posting code.

- petter

jaskali
November 7th, 2006, 06:28 AM
Yep.. works.. Thank you verry much.. seems i really need to think what the errors mean :D

Yeah.. first message.. but the next one will have code-tags

Magus
November 9th, 2006, 08:11 AM
Hmmm... Doesn't he still need to say the inner class (PushButtonActionListener) is static (a top-level nested class) in order to instantiate it without an instance of testi?

Update: Oh, what wildfrog meant by yourtesti must've been an instance of testi. If so, never mind. :P