0

I am building a grpahic interface with different buttons, and when user clicks a button I use :

actionPerformed(ActionEvent e) {
Object source = e.getSource();
if
else if
else if
...
}

The problem is that when the user clicks a button, actionPerformed(ActionEvent e) is called, and it enter in the if corresponding to e.getSource();. And it executes all instructions in the corresponding if. But I want to make possible that the user clicks different buttons, so that a actionPerformed(ActionEvent e) { is called, even if the instructions of the previous actionPerformed(ActionEvent e) { is not finish yet.

I don't know if you understand, but thank you if you can help me !

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Natacha
  • 1,887
  • 2
  • 14
  • 7

2 Answers2

5

You will need to use background threads if you want the GUI to be responsive while a long-running process is occurring. A SwingWorker works well for this in Swing GUI's. Please check out Concurrency in Swing for all the gory details on this.

Examples:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @Natacha: yep, tons of examples. Please search this site with my name and the key word *SwingWorker* to see some of them. – Hovercraft Full Of Eels Apr 07 '13 at 17:34
  • @Natacha: one way is to simply use Google: [Google Search](https://www.google.com/#hl=en&sclient=psy-ab&q=site:stackoverflow.com+hovercraftfullofeels+swingworker+doinbackground). Some links posted above in answer. – Hovercraft Full Of Eels Apr 07 '13 at 17:40
2

You need to use SwingWorker . It does the work in background without freezing the UI.

Here is the question you can have a look for how to use them. Or you can use official oracle documentation for SwingWorker.

Community
  • 1
  • 1
kaysush
  • 4,797
  • 3
  • 27
  • 47