Results 1 to 5 of 5
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default JButton actionlistener not working, very strange

    Ok normally i would try solve something like this on my own but im not getting any errors and i cant understand for the life of me why the actionListener for my Jbutton object isnt working.

    If you look at the below code i have an object called button which calls the actionListener event however none of the code in the actionListener code block is responding, i even tested it out with a System.out statment and nothing.
    However the textField actionListener is working fine.

    Any ideas?

    PHP Code:
    package javaapplication28;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class fun extends JApplet  {
    
        JButton button;
        JTextField textField;
        JTextArea textArea;
        JScrollPane scrollPane;
        JPanel panelText;
        DrawingPanel panelPaint;
        JPanel panelButton;
        String text = " ";
    
       @Override public void init() {
    
            buildLayout();
    
            textField.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text = textField.getText();
                textArea.append(text + "\n");
                textArea.setCaretPosition(textArea.getDocument().getLength());
                panelPaint.repaint();
                textField.selectAll();
            }});
    
            button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text = " ";
                textArea.setText(" ");
                panelPaint.repaint();
                textField.selectAll();
            }});
        }
    
       public void buildLayout(){
    
            setLayout(new BorderLayout());
    
            textField = new JTextField();    add(BorderLayout.NORTH,textField);
            panelText = new JPanel();        add(BorderLayout.WEST,panelText);
            panelPaint = new DrawingPanel(); add(BorderLayout.EAST,panelPaint);
            panelButton = new JPanel();      add(BorderLayout.SOUTH,panelButton);
    
            textField.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
            panelText.setBorder(BorderFactory.createLineBorder(Color.ORANGE, 1));
            panelPaint.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
    
            panelText.add(textArea = new JTextArea(8,15));
            scrollPane = new JScrollPane(textArea);
            panelText.add(scrollPane);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            panelPaint.setPreferredSize(new Dimension (150,0));
    
            panelButton.add(button = new JButton("Clear"));
       }
       
       public class DrawingPanel extends JPanel{
    
           @Override public void paintComponent(Graphics g){
    
               super.paintComponent(g);
               g.setColor(Color.red);
               g.setFont(new Font("Serif",Font.BOLD,14));
               g.drawString(text, 5, 50);
           }
       }
    
    
    }
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  2. #2
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your problem is one of references: The button you've added to the display is not the same as the one getting the ActionListener.
    thanks for quick reply. I double checked the code, and i just have the one jbutton and i didnt misspell the object name, or have i misunderstood what you are saying?
    thanks for taking the time to help me.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Sorry, ignore my previous post. I'm finding that your button's ActionListener is in fact being called.
    oh so the code is working for you?
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    i restarted netbeans and it worked all of a sudden. Is netbeans buggy sometimes?
    Teaching myself java so that i can eventually join the industry! Started in June 2010

Similar Threads

  1. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 04:40 PM
  2. help with lab - JButton not working
    By sAntA199 in forum AWT / Swing
    Replies: 6
    Last Post: 11-19-2009, 02:20 PM
  3. Replies: 2
    Last Post: 04-19-2009, 06:39 AM
  4. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 01:00 AM
  5. Jbutton not working??
    By rmabrey in forum AWT / Swing
    Replies: 7
    Last Post: 09-24-2008, 06:30 PM

Posting Permissions

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