This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Waiting for Action Listener to continue rest of the code.  RSS feed

 
Anthony Johnson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I'm having a issue, I think it is minor but I don't know. I'm trying to find the best way to wait for my button's action listener to be pressed before it runs through the code. I'm trying to write to an excel file but its skipping over my action listener and closing my workbook. What's the best way for me to do this.

 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the workbook to close after executing what's inside the listener then you need to close it inside the listener my friend.
 
Anthony Johnson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yosuf Ibrahim wrote:If you want the workbook to close after executing what's inside the listener then you need to close it inside the listener my friend.


Yes I tried that then eclipse asks me to put everything into a try catch. After I do that, it then throws an another error message.
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Johnson wrote:
Yes I tried that then eclipse asks me to put everything into a try catch. After I do that, it then throws an another error message.


Don't open the file outside the listener, open it and close it inside the listener
 
Anthony Johnson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yosuf Ibrahim wrote:
Anthony Johnson wrote:
Yes I tried that then eclipse asks me to put everything into a try catch. After I do that, it then throws an another error message.


Don't open the file outside the listener, open it and close it inside the listener


Sorry to say, but I did that already. I even tried a while loop to have it wait until the button was pressed. And for some reason, it still went to close the file first.
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm. Sorry, I am not an expert, this is my stage in which I learn by trial and error. So here is the last idea I have.

How about you open the file on application start and close it when the app is closing??
 
Anthony Johnson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yosuf Ibrahim wrote:hmm. Sorry, I am not an expert, this is my stage in which I learn by trial and error. So here is the last idea I have.

How about you open the file on application start and close it when the app is closing??


Its ok and thanks, but I believe that is what I have now.
 
Dave Tolls
Ranch Hand
Posts: 2721
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is going to come down to how you want the app to respond.

All I can tell from what you have at the moment is that you start up and have a screen (presumably set up by the EnergyStructures... class) with a button on it.
The user has to push the button to do load the data to work on?

Why have a button?
Why not just load the data?
Or load (as suggested above) when the button is clicked...this would probably involve a SwingWorker.

One thing I notice is you seem to be doing GUI stuff outside the EDT (that's the GUI thread).  This can cause issues, and probably needs sorting out, especially if you go down the SwingWorker route.
 
Campbell Ritchie
Marshal
Posts: 54882
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find out more about threads in Swing in the Java™ Tutorials.

It is usually a bad idea to have more than one statement in the main method. There is something wrong when you start using the SuppressWarnings annotation. Does that mean you have static fields in that class? I have a rule of thumb about the keyword static. If you don't have a good explanation why you used it, it is a mistake. Uing static to make code compile is a mistake. The correct solution is to move code out of the main method.
 
Anthony Johnson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:This is going to come down to how you want the app to respond.

All I can tell from what you have at the moment is that you start up and have a screen (presumably set up by the EnergyStructures... class) with a button on it.
The user has to push the button to do load the data to work on?

Why have a button?
Why not just load the data?
Or load (as suggested above) when the button is clicked...this would probably involve a SwingWorker.

One thing I notice is you seem to be doing GUI stuff outside the EDT (that's the GUI thread).  This can cause issues, and probably needs sorting out, especially if you go down the SwingWorker route.


This user is to select the line that they are looking for and then press a button and that information is sent to an excel document.
 
Anthony Johnson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:This is going to come down to how you want the app to respond.

All I can tell from what you have at the moment is that you start up and have a screen (presumably set up by the EnergyStructures... class) with a button on it.
The user has to push the button to do load the data to work on?

Why have a button?
Why not just load the data?
Or load (as suggested above) when the button is clicked...this would probably involve a SwingWorker.

One thing I notice is you seem to be doing GUI stuff outside the EDT (that's the GUI thread).  This can cause issues, and probably needs sorting out, especially if you go down the SwingWorker route.


I figured it out yesterday. But I did basically what you are saying now. i created a function to load the file and write to the file. Then I used the action listener to call that function. Thanks everyone for responding.

I will also remember that this that I should go to the GUI threads next time. Thanks.
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Johnson wrote:I will also remember that this that I should go to the GUI threads next time. Thanks.


No worries mate, me personally I prefer people coming in here, I read the posts even ones I do not understand hoping to learn something. It is easier to learn from other's mistakes rather than being stuck for hours on my own in it
 
Dave Tolls
Ranch Hand
Posts: 2721
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I talk about the GUI thread (the EDT) I mean a Thread in the code, not on the forum.

The EDT is something you need to understand in order to work with Swing or JavaFX.

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!