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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

Thread: How to set a button event trigger?

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default How to set a button event trigger?

    Hi.
    I have created a modal dialog box thus:
    	JDialog dialog = new  JDialog();
    	dialog.setModal(true);
    	dialog.setSize(400, 150);
     
    	dialog.setTitle("Input dialog");
     
    	Container pane = dialog.getContentPane();
    pane.setLayout(null);
     
     
    	JLabel l1 = new JLabel("simtime(min)");
    	pane.add(l1);
    	l1.setBounds(10, 10, 100, 10);
     
    		JTextField  tf = new JTextField("");
    	pane.add( tf);
    	tf.setBounds(110,10, 40, 15);
     
    	JLabel l2 = new JLabel("interval(sec)") ;
    pane.add(l2);
    	l2.setBounds(10, 50, 100, 10);
     
    	JTextField  tf2 = new JTextField("");
    	pane.add( tf2);
    	tf2.setBounds(110,50, 40, 15);
    	dialog.setVisible(true);	
     
    	JButton button = new JButton("OK");
    pane.add(button);
    button.setBounds(120,120,40,20);
     
     
    		int simTime = Integer.parseInt( tf.getText() );;
    		int interval = Integer.parseInt( tf2.getText() );

    But nothing happens when I click on the "OK" button. Why?and what should I do?

    Thanks in advance


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

  3. The Following User Says Thank You to copeg For This Useful Post:

    hooshdar3 (July 14th, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to set a button event trigger?

    nothing happens when I click on the "OK" button
    You need an action listener. See the tutorial: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    hooshdar3 (July 14th, 2014)

  6. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: How to set a button event trigger?

    At what point in your code do you tell the button what to do when it is clicked?

  7. The Following User Says Thank You to Cornix For This Useful Post:

    hooshdar3 (July 14th, 2014)

  8. #5
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by Cornix View Post
    At what point in your code do you tell the button what to do when it is clicked?
    Before the last two lines.

    --- Update ---

    But this way, there is no way to distinguish between different events for one component, isn't it?

  9. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: How to set a button event trigger?

    No you dont.
    This is "before the last 2 lines":
    JButton button = new JButton("OK");
    pane.add(button);
    button.setBounds(120,120,40,20);
    And you dont tell the button anything here. Just because it displays "OK" as its label does not mean that it knows what to do when its clicked.

  10. #7
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by Cornix View Post
    No you dont.
    This is "before the last 2 lines":
    JButton button = new JButton("OK");
    pane.add(button);
    button.setBounds(120,120,40,20);
    And you dont tell the button anything here. Just because it displays "OK" as its label does not mean that it knows what to do when its clicked.
    No.I meant I want the last two lines onward be executed then. there is processing code not shown here.
    Again, the last two lines are:
    int simTime = Integer.parseInt( tf.getText() );;
    int interval = Integer.parseInt( tf2.getText() );

  11. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: How to set a button event trigger?

    And where do you tell the button that it is supposed to execute these lines?

  12. #9
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by Cornix View Post
    And where do you tell the button that it is supposed to execute these lines?
    It is the topic of this topic

  13. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by hooshdar3 View Post
    It is the topic of this topic
    Well, this was your question:
    But nothing happens when I click on the "OK" button. Why?
    So the answer is pretty simple: Nothing happens because you never told the button what to do when it is being clicked.

    As far as I know there are 2 ways to define the behavior of a button:
    1) You give it an "Action"-Object. This action object will define the behavior when clicked.

    2) You add an ActionListener to the button. When clicked, the button will notify all ActionListeners that have been registered to it.

    An example in code:
    JButton btn = new Button();
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             // do stuff
        }
    });

  14. The Following User Says Thank You to Cornix For This Useful Post:

    hooshdar3 (July 15th, 2014)

  15. #11
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by Cornix View Post
    Well, this was your question:

    So the answer is pretty simple: Nothing happens because you never told the button what to do when it is being clicked.

    As far as I know there are 2 ways to define the behavior of a button:
    1) You give it an "Action"-Object. This action object will define the behavior when clicked.

    2) You add an ActionListener to the button. When clicked, the button will notify all ActionListeners that have been registered to it.

    An example in code:
    JButton btn = new Button();
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             // do stuff
        }
    });
    Do I have to put the code inside a function?

    N.B:Should the second line be:
    btn.addActionListener( new ActionListener() )?

  16. #12
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: How to set a button event trigger?

    Perhaps you should read up on anonymous classes.

  17. The Following User Says Thank You to Cornix For This Useful Post:

    hooshdar3 (July 15th, 2014)

  18. #13
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by hooshdar3 View Post
    Do I have to put the code inside a function?
    I asked because then the TextFields will not be known in the function actionPerformed()

  19. #14
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: How to set a button event trigger?

    Make it known. If you dont want to make it a private member variable you can also declare it as "final".

  20. The Following User Says Thank You to Cornix For This Useful Post:

    hooshdar3 (July 15th, 2014)

  21. #15
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by Cornix View Post
    Make it known. If you dont want to make it a private member variable you can also declare it as "final".
    The end-user is supposed to enter numbers in the text-fields. Doesn't Final make them unchangable? What about private?

  22. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: How to set a button event trigger?

    final means the variable's value can't be changed. The contents of the object can be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    hooshdar3 (July 15th, 2014)

  24. #17
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by Norm View Post
    final means the variable's value can't be changed. The contents of the object can be changed.
    What do you mean?
    I declare them as e.g:
    public static final JTextField tf = new JTextField("")
    outside any function? then what happens when the user chabges their content? Will
    tf.getText()
    return the string representation of the number entered?

  25. #18
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    583
    Thanks
    0
    Thanked 105 Times in 90 Posts

    Default Re: How to set a button event trigger?

    no, dont use static, and dont make it a member variable either. you just declare them is final within your method. The same method you have been using already.
    This means that you can not use the same variable to create a new JTextField, but all of the variables inside the JTextField class can change same as before.

    For example:
    public void initGUI() {
    	final JTextField txtF = new JTextField();
     
    	JButton btn = new JButton();
    	btn.addActionListener(new ActionListener {
    		String content = txtF.getText();
    		System.out.println(content);
    	});
    }

  26. #19
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by Cornix View Post
    no, dont use static, and dont make it a member variable either. you just declare them is final within your method. The same method you have been using already.
    This means that you can not use the same variable to create a new JTextField, but all of the variables inside the JTextField class can change same as before.

    For example:
    public void initGUI() {
    	final JTextField txtF = new JTextField();
     
    	JButton btn = new JButton();
    	btn.addActionListener(new ActionListener {
    		String content = txtF.getText();
    		System.out.println(content)	}
    );
    }
    Why is it that your ActionListener is not followed by a pair of parentheses, and are followed by a pair of braces instead?
    Last edited by hooshdar3; July 16th, 2014 at 03:20 AM.

  27. #20
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default ActionEvent

    Hi.
    I set an action listener for my button but nothing happens when the button is clicked.Why?
    Here is my code:
    public class sysinfoHooshi implements ActionListener{
        static JTextField  tf = new JTextField("");
        static JTextField  tf2 = new JTextField("");
     
    	public static void simulate()throws Exception{
    	JDialog dialog = new  JDialog();
    	dialog.setModal(true);
    	dialog.setSize(400, 150);
     
    	dialog.setTitle("Input dialog");
     
    	Container pane = dialog.getContentPane();
            pane.setLayout(null);
     
     
    	JLabel l1 = new JLabel("simtime(min)");
    	pane.add(l1);
    	l1.setBounds(10, 10, 100, 10);
     
     
    	pane.add( tf);
    	tf.setBounds(110,10, 40, 15);
     
    	JLabel l2 = new JLabel("interval(sec)") ;
    	pane.add(l2);
    	l2.setBounds(10, 50, 100, 10);
     
     
    	pane.add( tf2);
    	tf2.setBounds(110,50, 40, 15);
     
    	JButton button = new JButton("OK");
    	pane.add(button);
    	button.setBounds(100,100,40,20);
    	dialog.setVisible(true);
     
    //some code
     
    	}//end simulate()
    	 public void actionPerformed(ActionEvent e) {tf2.setText("Hello");}
    public static void main(String args[]) throws Exception{	
    	simulate();
    	} // end main
       }// end class

    Thanks in advance

  28. #21
    Forum VIP
    Join Date
    Jun 2011
    Posts
    313
    My Mood
    Bored
    Thanks
    46
    Thanked 87 Times in 73 Posts
    Blog Entries
    4

    Default Re: ActionEvent

    You need to tell the button that the class is the action listener.

    button.addActionListener(this);

    You can read up on Action Listeners here

    --- Update ---

    Oh, and you can also do away with the 'implements ActionListener' and use anonymous inner classes. They are real nifty.

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            simulate();
        }
    });

  29. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    hooshdar3 (July 16th, 2014)

  30. #22
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,124
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: How to set a button event trigger?

    Essentially the same topic, the same road being repaved, threads merged.

  31. #23
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: ActionEvent

    Quote Originally Posted by ChristopherLowe View Post
    You need to tell the button that the class is the action listener.

    button.addActionListener(this);
    Heh!unbelievable!I haven't done that!

    OK, I added that line, and now get the compile error:
    non-static variable this cannot be referenced from a static context
    button.addActionListener(this);
    ^
    1 error

  32. #24
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,124
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: How to set a button event trigger?

    And this ground has been covered before. Please read the advice you've been given and apply it. If you don't understand it, then continue asking questions. DON'T bail to a new thread where potentially the conversation has to start all over again. If you don't follow the advice given or at least acknowledge it with questions, those trying to help will give up on you as untrainable.

  33. #25
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: How to set a button event trigger?

    Quote Originally Posted by GregBrannon View Post
    And this ground has been covered before. Please read the advice you've been given and apply it. If you don't understand it, then continue asking questions. DON'T bail to a new thread where potentially the conversation has to start all over again. If you don't follow the advice given or at least acknowledge it with questions, those trying to help will give up on you as untrainable.
    OK, but I have asked my question already

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: September 26th, 2013, 12:29 PM
  2. Button event handler from another class
    By Scorliss in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 6th, 2013, 06:41 PM
  3. Problem netbeans gui button event handling
    By hiepa in forum AWT / Java Swing
    Replies: 7
    Last Post: December 3rd, 2012, 04:24 PM
  4. How to set cancel button ASAP please
    By smasm in forum Object Oriented Programming
    Replies: 1
    Last Post: October 1st, 2011, 08:29 AM
  5. Trigger Button in Html
    By liron in forum Java Networking
    Replies: 1
    Last Post: October 16th, 2010, 09:17 AM