Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: ActionListener not working

  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default ActionListener not working

    So Im attempting to get a button to change a picutre in my j frame and so far Im not having any luck.

    Can anyone tell me why my actionlistener isnt picking up on the button events?

    Thanks in advance.
    Last edited by aianta; 09-16-2012 at 09:21 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: ActionListener not working

    How many variables do you have with the same name? One declared as an instance field is not the same as one declared inside a method.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Re: ActionListener not working

    Thanks I looked over things and I was redeclaring variables agian (facepalm) sigh, Ill learn eventually.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: ActionListener not working

    Please don't remove content after your question is answered. This is a forum, where all members are free to learn from the discussion of problems and possible solutions.

    Here's the code you removed; if you do this again you may face a ban, without another warning being given.

    Quote Originally Posted by aianta
    Java Code:
    public class GUI extends JFrame implements ActionListener{
    
     private JPanel contentPane;
    
    
     private JTextField ztextField;
     private JTextField xtextField;
     private JTextField ytextField;
    
     private JTextField atextField;
    
     private VA va;
    
     private int imageWidth;
     private int imageHeight;
    
     //Create the JLabels that will store the images
     private JLabel imageZ;
     private JLabel imageX;
     private JLabel imageY;
    
     private JButton zUp;
     private JButton zDown;
    
     private int keyframe = 0;
    
     /**
     * Create the frame.
     */
    
     public GUI(VideoAnalyzer va, int imageWidth, int imageHeight) {
    
     this.va = va;
     this.imageWidth = imageWidth;
     this.imageHeight = imageHeight;
     this.setFocusable(true);
    
    
    
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setBounds(100, 100, 969, 552);
     contentPane = new JPanel();
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
     setContentPane(contentPane);
     contentPane.setLayout(null);
    
     imageZ = new JLabel(new ImageIcon(va.getImageFixed(1)));
     imageZ.setBounds(10, 11, 300, 300);
     contentPane.add(imageZ);
    
    
     imageX = new JLabel(new ImageIcon(va.getImageFromImageFixedX(1)));
     imageX.setBounds(332, 11, 300, 300);
     contentPane.add(imageX);
    
    
     imageY = new JLabel(new ImageIcon(va.getImageFromImageFixedY(1)));
     imageY.setBounds(650,11,300,300);
     contentPane.add(imageY);
    
    
     ztextField = new JTextField();
     ztextField.setBounds(10, 432, 86, 20);
     contentPane.add(ztextField);
     ztextField.setColumns(10);
     ztextField.addActionListener(this);
    
    
     xtextField = new JTextField();
     xtextField.setBounds(10, 463, 86, 20);
     contentPane.add(xtextField);
     xtextField.setColumns(10);
     xtextField.addActionListener(this);
    
    
     ytextField = new JTextField();
     ytextField.setBounds(10, 494, 86, 20);
     contentPane.add(ytextField);
     ytextField.setColumns(10);
     ytextField.addActionListener(this);
    
    
     atextField = new JTextField();
     atextField.setBounds(10, 401, 86, 20);
     contentPane.add(atextField);
     atextField.setColumns(10);
     atextField.addActionListener(this);
    
    
     JLabel zTextFieldLabel = new JLabel("Z");
     zTextFieldLabel.setBounds(106, 435, 46, 14);
     contentPane.add(zTextFieldLabel);
    
     JLabel xTextFieldLabel = new JLabel("X");
     xTextFieldLabel.setBounds(106, 466, 46, 14);
     contentPane.add(xTextFieldLabel);
    
     JLabel yTextFieldLabel = new JLabel("Y");
     yTextFieldLabel.setBounds(106, 497, 46, 14);
     contentPane.add(yTextFieldLabel);
    
     JLabel imageZLabel = new JLabel("Z");
     imageZLabel.setBounds(135, 322, 46, 14);
     contentPane.add(imageZLabel);
    
     JLabel imageXLabel = new JLabel("X");
     imageXLabel.setBounds(466, 322, 46, 14);
     contentPane.add(imageXLabel);
    
     JLabel imageYLabel = new JLabel("Y");
     imageYLabel.setBounds(795, 322, 46, 14);
     contentPane.add(imageYLabel);
    
     JLabel allTextFieldLabel = new JLabel("All");
     allTextFieldLabel.setBounds(106, 404, 46, 14);
     contentPane.add(allTextFieldLabel);
    
     JButton zUp = new JButton("Z Up");
     zUp.setBounds(182, 431, 89, 23);
     contentPane.add(zUp);
     zUp.addActionListener(this);
    
     JButton zDown = new JButton("Z Down");
     zDown.setBounds(182, 462, 89, 23);
     contentPane.add(zDown);
     zDown.addActionListener(this);
    
    
     }
    
     @Override
     //Action Listener for the JTextFields
     public void actionPerformed(ActionEvent event) {
     // TODO Auto-generated method stub
    
     if (event.getSource().equals(ztextField)){
     imageZ.setIcon(new ImageIcon(va.getImageFromImageFixedZ(Integer.parse Int(ztextField.getText())))); 
     }
    
     if (event.getSource().equals(ytextField)){
     imageY.setIcon(new ImageIcon(va.getImageFromImageFixedY(Integer.parse Int(ytextField.getText()))));
     this.repaint();
     }
    
     if (event.getSource().equals(xtextField)){
     imageX.setIcon(new ImageIcon(va.getImageFromImageFixedX(Integer.parse Int(xtextField.getText()))));
     this.repaint();
     }
    
     if (event.getSource().equals(atextField)){
     imageZ.setIcon(new ImageIcon(va.getImageFromImageFixedZ(Integer.parse Int(atextField.getText()))));
     imageY.setIcon(new ImageIcon(va.getImageFromImageFixedY(Integer.parse Int(atextField.getText()))));
     imageX.setIcon(new ImageIcon(va.getImageFromImageFixedX(Integer.parse Int(atextField.getText()))));
     }
    
     if (event.getSource().equals(zUp)){
     // imageZ.setIcon(new ImageIcon(va.getImageFromImageFixedZ(keyframe + 1)));
     System.out.println("test");
     }
    
     this.repaint();
     }
     }
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: ActionListener not working

    Closing this thread to prevent further editing.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Why is my ActionListener not working
    By isaacske in forum AWT / Swing
    Replies: 2
    Last Post: 09-07-2012, 10:35 PM
  2. actionlistener no working,, help
    By SaraDiab in forum Advanced Java
    Replies: 2
    Last Post: 12-27-2011, 05:22 AM
  3. JButton actionlistener not working, very strange
    By alacn in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 02:25 AM
  4. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 03:40 PM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

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