...

View Full Version : java addActionListener problem



earthsiege
12-12-2004, 03:27 PM
hi guys,

iv written a prog which has a JtabbedPane GUI but the problem is on one of my tabs i have a JPanel called panel1. on panel1 i have a button called addButton now i add this button onto my panel and my panel onto the tab using the following code:


panel1.add(addButton);
tabbedPane.addTab("New Booking", null, panel1, "First Panel");

which works just fine and when compiled you see the tabs and the button on the first pane.

but then when i try and ad an actionlistener to the button using the following code:


addButton.addActionListener(this);

i get this error:



addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (tester)



tester is the name of my class.
can anyone help me?

shmoove
12-12-2004, 04:01 PM
When you declare your class you need to specify that it implements the ActionListener interface:


class tester implements ActionListener {

otherwise the compiler won't know it can treat your class as an ActionListener and give you the error you mentioned when you try to pass it to a method that takes an ActionListener as a parameter.

shmoove

earthsiege
12-12-2004, 08:19 PM
cant believ i missed that out.
thanks for the help man,
one more thing though, when im referring to the button in my actionPerformed class how do i refer to the addButton button?
i mean do i do this:



public void actionPerformed(ActionEvent e){
System.out.println(e.getSource());
if(e.getSource() == addButton){...

or something else?

turbowrx
12-12-2004, 08:56 PM
You got it. Happy programming.

shmoove
12-13-2004, 09:28 AM
Yeah, as long as the addButton field is in scope in the actionPerformed() method (which it looks like it is, if it's a member of the tester class).

shmoove

earthsiege
12-13-2004, 12:30 PM
ok....
not working...
i keep on getting the error:


cannot resolve symbol-addButton


i think im supposed to move the actionPerformer class so that its in scope bu im not sure where im supposed to move it to.
heres my code:



//GUI display for the tester program

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


public class tester extends JFrame implements ActionListener{
public tester()
{
super("Swimming Lesson Booking System");

//create tabbed panel
JTabbedPane tabbedPane = new JTabbedPane();
//------------------------------------------------------------------------------------------------------------
/*------------------------------------set up components for panel 1-----------------------------------------*/
JLabel nameLabel = new JLabel("Name: ", SwingConstants.CENTER);
JLabel surnameLabel = new JLabel("Surname: ", SwingConstants.CENTER);
JLabel addressLabel = new JLabel("Address: ", SwingConstants.CENTER);
JLabel guardianLabel = new JLabel("Name of guardian: ", SwingConstants.CENTER);
JLabel paymentLabel = new JLabel("Payment method: ", SwingConstants.CENTER);
JLabel classLabel = new JLabel("Level: ", SwingConstants.CENTER);
JTextField nameText = new JTextField("", 10);
JTextField surnameText = new JTextField("", 10);
JTextField guardianText = new JTextField("", 10);
JTextField addressText = new JTextField("", 10);
JButton addButton = new JButton("Make new Booking");

//setting up the different methods of payment
String payments[]={"Cash", "Cheque", "Credit card"};

//setting up the combo box
JComboBox paymentMethod = new JComboBox(payments);
paymentMethod.setMaximumRowCount(3);

//Setting up the different classes available
String classes[] = {"Ducklings Group A", "Ducklings Group B", "Beginners Group A ", "Beginners Group B ", "Beginners Group C ", "Intermediate", "Advanced"};

//Setting up the swimming class combo box and adding the action event
JComboBox classesAvailable = new JComboBox(classes);
classesAvailable.setMaximumRowCount(5);


//adding components to the first panel
JPanel panel1 = new JPanel();
panel1.add(nameLabel);
panel1.add(nameText);
panel1.add(surnameLabel);
panel1.add(surnameText);
panel1.add(guardianLabel);
panel1.add(guardianText);
panel1.add(addressLabel);
panel1.add(addressText);
panel1.add(paymentLabel);
panel1.add(paymentMethod);
panel1.add(classLabel);
panel1.add(classesAvailable);
panel1.add(addButton);
addButton.addActionListener(this);



tabbedPane.addTab("New Booking", null, panel1, "First Panel");
//-----------------------------------------------------------------------------------------------------
/*-------------------------------------------------------set up panel 2-------------------------------*/
JLabel viewLevelLabel = new JLabel("Level: ");
JButton view = new JButton("View Bookings");

//viewlevel combobox
String viewClasses[] = {"All", "Ducklings", "Beginners", "Intermediate", "Advanced"};
JComboBox viewClassesAvailable = new JComboBox(viewClasses);
viewClassesAvailable.setMaximumRowCount(3);

//panel 2
JPanel panel2 = new JPanel();
panel2.add(viewLevelLabel);
panel2.add(viewClassesAvailable);
panel2.add(view);


tabbedPane.addTab("View Bookings", null, panel2, "Second Panel");



/*------------------------------------------------------set up panel 3-------------------------------------*/
JLabel deleteLabel = new JLabel("Please enter the refrence number of the\n booking you are looking to cancel");
JTextField deleteText = new JTextField("",2);
JButton deleteButton = new JButton("Cancel Booking");

//panel3
JPanel panel3 = new JPanel();
panel3.add(deleteLabel);
panel3.add(deleteText);
panel3.add(deleteButton);

tabbedPane.addTab("Cancel Booking", null, panel3, "Third Panel");



//------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------
getContentPane().add(tabbedPane);

setSize(400, 300);
setVisible(true);
}

public static void main(String args[])
{
tester tabbedPaneDemo = new tester();
tabbedPaneDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){
// System.out.println(e.getSource());
if(e.getSource() == addButton){
Booking booking = new Booking(nameText.getText(), surnametext.getText(), guardianText.getText(),paymentMethod.getItemSelected(), addressText.getText(), classesAvailable.getItemSelected());
}
}
}

shmoove
12-13-2004, 01:25 PM
addButton is declared inside tester's constructor, so it's scope is only within that method. You can move the declaration outside of the constructor so it becomes a member field (so it's in scope in every method of the class):


public class tester extends JFrame implements ActionListener{
JButton addButton;
public tester()
{
// ....
addButton = new JButton("Make new Booking");

or you could make the ActionListener an anonymous inner class within the constructor (so that the ActionListener is also in the same scope - inside the constructor):


public class tester extends JFrame {
public tester()
{
// ....
addButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
// .....
}
}
);
}

Notice that in this second case the tester class doesn't implement the interface anymore.

shmoove

shmoove
12-13-2004, 01:28 PM
A tutorial about scope in Java (http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html).

shmoove



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum