fuction calling

how to call 2 function onclick. i want to know. when i click on button

Page 1 of 1

11 Replies - 1022 Views - Last Post: 26 July 2008 - 12:14 PM Rate Topic: -----

#1 dharam1248  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-July 08

fuction calling

Post icon  Posted 20 July 2008 - 10:07 PM

how to call 2 function onclick. i want to know. when i click on button, call 2 function
Is This A Good Question/Topic? 0
  • +

Replies To: fuction calling

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: fuction calling

Posted 20 July 2008 - 10:46 PM

Assuming you have an an ActionListener set up on your button...

public void actionPerformed(ActionEvent e)
{
    // call your functions here.
}


If you don't have an ActionListener set up...

jButton1.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        // do stuff on the button click (AKA the action performed)
        // of course, you replace "jButton1" with the name of your button object.
    }
});


Hope this helps! :)

This post has been edited by Locke37: 20 July 2008 - 10:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: fuction calling

Posted 21 July 2008 - 04:35 AM

You can not be sure that ActionListener will happen just on click (because it does not).

bt.addMouseListener(new MouseListener(){
	public void mouseClicked(MouseEvent arg0) {
//				method1();
//				method2();
	}
	public void mouseEntered(MouseEvent arg0) {
		
	}
	public void mouseExited(MouseEvent arg0) {
		
	}
	public void mousePressed(MouseEvent arg0) {
		
	}
	public void mouseReleased(MouseEvent arg0) {
		
	}
});

Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: fuction calling

Posted 21 July 2008 - 02:53 PM

...on a JButton...yes, I can be sure, because there is only 1 thing you can do on a button (of any sort). So, if it's a button, the actionPerformed() method will happen.
Was This Post Helpful? 0
  • +
  • -

#5 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: fuction calling

Posted 21 July 2008 - 03:29 PM

View PostLocke37, on 21 Jul, 2008 - 02:53 PM, said:

...on a JButton...yes, I can be sure, because there is only 1 thing you can do on a button (of any sort). So, if it's a button, the actionPerformed() method will happen.

Right, but it will happen when mouse is over, mouse pressed, mouse released, key pressed when button is active, or any other thing not just on click as wanted.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: fuction calling

Posted 21 July 2008 - 03:53 PM

View Postlordms12, on 21 Jul, 2008 - 03:29 PM, said:

View PostLocke37, on 21 Jul, 2008 - 02:53 PM, said:

...on a JButton...yes, I can be sure, because there is only 1 thing you can do on a button (of any sort). So, if it's a button, the actionPerformed() method will happen.

Right, but it will happen when mouse is over, mouse pressed, mouse released, key pressed when button is active, or any other thing not just on click as wanted.


Locke37 solution will also work when <CR> is entererd and the JButton has the focus so it is better a better implementation especially if you want (later) disable the JButton in that case the ActionListener will not be call while your MouseListener will
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: fuction calling

Posted 23 July 2008 - 08:49 AM

View Postlordms12, on 21 Jul, 2008 - 03:29 PM, said:

View PostLocke37, on 21 Jul, 2008 - 02:53 PM, said:

...on a JButton...yes, I can be sure, because there is only 1 thing you can do on a button (of any sort). So, if it's a button, the actionPerformed() method will happen.

Right, but it will happen when mouse is over, mouse pressed, mouse released, key pressed when button is active, or any other thing not just on click as wanted.


No...it won't. I use this on every button when making my own GUIs. It only happens when you click the button or when it has the focus and you press 'Enter' or 'Spacebar'. :ph34r:

This post has been edited by Locke37: 23 July 2008 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 tjd195  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-July 08

Re: fuction calling

Posted 23 July 2008 - 09:03 PM

lordms12's solution is more advanced, but I am pretty sure Locke37 is correct in his last statement. I also use JButton a lot for my less advance GUIs.
Was This Post Helpful? 0
  • +
  • -

#9 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: fuction calling

Posted 24 July 2008 - 03:43 AM

View Posttjd195, on 23 Jul, 2008 - 09:03 PM, said:

lordms12's solution is more advanced, but I am pretty sure Locke37 is correct in his last statement. I also use JButton a lot for my less advance GUIs.


To add my 0.02, using ActionListener is less work because it only has 1 method to implement (and yes I do know my IDE can generate it for me, but that means clicking more checkboxes in case of MouseListener)
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: fuction calling

Posted 24 July 2008 - 01:53 PM

View PostJeroenFM, on 24 Jul, 2008 - 03:43 AM, said:

View Posttjd195, on 23 Jul, 2008 - 09:03 PM, said:

lordms12's solution is more advanced, but I am pretty sure Locke37 is correct in his last statement. I also use JButton a lot for my less advance GUIs.


To add my 0.02, using ActionListener is less work because it only has 1 method to implement (and yes I do know my IDE can generate it for me, but that means clicking more checkboxes in case of MouseListener)


Correct, using an ActionListener is less work, but should only be used on buttons in my opinion. I don't use it for anything else. Course, I don't make too advanced GUIs...
Was This Post Helpful? 0
  • +
  • -

#11 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: fuction calling

Posted 25 July 2008 - 01:13 AM

I also use it for JMenuItem, but I don't think I use it for anything else.
Was This Post Helpful? 0
  • +
  • -

#12 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: fuction calling

Posted 26 July 2008 - 12:14 PM

Hm, I've never used a JMenuItem. Well I've used menu items, just not in Java. :unsure: I use them in VB/VB.Net all the time...same concept. :^:

This post has been edited by Locke37: 26 July 2008 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1