Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You got it. Happy programming.

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Jul 2004
    Location
    UK
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •