Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2004, 02:27 PM   PM User | #1
earthsiege
New Coder

 
Join Date: Jul 2004
Location: UK
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
earthsiege is an unknown quantity at this point
java addActionListener problem

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:
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:
Code:
  addButton.addActionListener(this);
i get this error:

PHP Code:
addActionListener(java.awt.event.ActionListenerin javax.swing.AbstractButton cannot be applied to (tester
tester is the name of my class.
can anyone help me?
__________________
c:\dos...
c:\dos\run...
run dos run
earthsiege is offline   Reply With Quote
Old 12-12-2004, 03:01 PM   PM User | #2
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
When you declare your class you need to specify that it implements the ActionListener interface:
Code:
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
shmoove is offline   Reply With Quote
Old 12-12-2004, 07:19 PM   PM User | #3
earthsiege
New Coder

 
Join Date: Jul 2004
Location: UK
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
earthsiege is an unknown quantity at this point
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:

Code:
 public void actionPerformed(ActionEvent e){
        System.out.println(e.getSource());
    if(e.getSource() == addButton){...
or something else?
__________________
c:\dos...
c:\dos\run...
run dos run
earthsiege is offline   Reply With Quote
Old 12-12-2004, 07:56 PM   PM User | #4
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
You got it. Happy programming.
turbowrx is offline   Reply With Quote
Old 12-13-2004, 08:28 AM   PM User | #5
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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
shmoove is offline   Reply With Quote
Old 12-13-2004, 11:30 AM   PM User | #6
earthsiege
New Coder

 
Join Date: Jul 2004
Location: UK
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
earthsiege is an unknown quantity at this point
ok....
not working...
i keep on getting the error:
Code:
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:

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()); 
    }
}
}
__________________
c:\dos...
c:\dos\run...
run dos run
earthsiege is offline   Reply With Quote
Old 12-13-2004, 12:25 PM   PM User | #7
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
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):
Code:
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):
Code:
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 is offline   Reply With Quote
Old 12-13-2004, 12:28 PM   PM User | #8
shmoove
Regular Coder

 
Join Date: Dec 2003
Posts: 367
Thanks: 0
Thanked 0 Times in 0 Posts
shmoove is an unknown quantity at this point
A tutorial about scope in Java.

shmoove
shmoove is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:25 PM.


Advertisement
Log in to turn off these ads.