Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: aCTION EVENT WITHOUT ACTIONLISTENER

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default aCTION EVENT WITHOUT ACTIONLISTENER

    how make action event with jButton, without using ActionListener??


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: aCTION EVENT WITHOUT ACTIONLISTENER

    I think a good question would be why do you want to avoid the ActionListener?

    Rolf

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    990
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: aCTION EVENT WITHOUT ACTIONLISTENER

    Programs can, but typically don't *make* events - they just happen. If you want to make them, you invoke their constructor, like any other class. This doesn't involve an event listener.

    If that doesn't answer what's on your mind, perhaps you could describe what it is you want to do by creating these events without using listeners.

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: aCTION EVENT WITHOUT ACTIONLISTENER

    because i cant use the actionlistener ..... everytime i use it it says i need to final the int, and when i final the int, it says final int is cannot be changed ^^

    --- Update ---

    Quote Originally Posted by pbrockway2 View Post
    Programs can, but typically don't *make* events - they just happen. If you want to make them, you invoke their constructor, like any other class. This doesn't involve an event listener.

    If that doesn't answer what's on your mind, perhaps you could describe what it is you want to do by creating these events without using listeners.

    when i click a buton i want to add something ^^

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    990
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: aCTION EVENT WITHOUT ACTIONLISTENER

    Post code. Not the whole thing but showing the button and how you're writing the listener.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: aCTION EVENT WITHOUT ACTIONLISTENER

    here sir....

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Example
    {
        public Example()
        {
          int x = 0;
          JButton button = new JButton("Add");
     
          button.addActionListener(new ActionListener()
            {
              public void actionPerformed(ActionEvent e)
              {
                  x = x + 1;
     
               }
            }
            );
        }
    }


    --- Update ---

    please copy .... and try to run it

    --- Update ---

    Quote Originally Posted by pbrockway2 View Post
    Post code. Not the whole thing but showing the button and how you're writing the listener.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Example
    {
    public Example()
    {
    int x = 0;
    JButton button = new JButton("Add");

    button.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    x = x + 1;

    }
    }
    );
    }
    }
    Last edited by pbrockway2; May 1st, 2013 at 01:48 AM. Reason: code tags added

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    990
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: aCTION EVENT WITHOUT ACTIONLISTENER

    Declare x as an instance variable of the Example class not a local variable of the Example constructor.


    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*; 
     
    public class Example {
     
        private int x;
     
        public Example() {
            //etc

    Local variables have to be final. And final variables can't change their value. It's a Java thing... you simply can't declare a variable to have scope you mean the way you can in JavaScript.

    --- Update ---

    The update looks good. Of course you have to add the button to a gui, and actually do something with the updated value of x. I'm going offline now, but post if you get stuck, someone is sure to help.

    (there are no sirs here. We're comrades, colleagues, citizens...)

Similar Threads

  1. Writing an Action Event in a Jpanel to convert user text entered in a textField to uppercase
    By veryNoviceProgrammer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 24th, 2013, 07:48 PM
  2. Problem with Action Event
    By jayzee98 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: August 29th, 2012, 11:04 AM
  3. [SOLVED] difference between action event from 2 buttons with same name?
    By nggdowt in forum AWT / Java Swing
    Replies: 5
    Last Post: November 7th, 2011, 10:51 AM
  4. Replies: 9
    Last Post: June 29th, 2011, 01:27 PM
  5. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 07:24 AM

Tags for this Thread