• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Program should wait for pressed button

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found similar questions and concerning answers to this kind of problem but this was not helpful for me. My specific problem is: In the code below the program runs and goes directly to the last line “system.out.println(“to this point”)”. I want that it waits until one pressed the button (buttonsav) to choose some files. Then after successful done it should go to the last line System.out.println("to this Point"). Later these files are processed. The program cannot run without these files. So, how to let the program wait for buttonclick. I tried it with a while loop and boolean var, but I failed. Here is the code:

 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

stefanie ohm wrote:I found similar questions and concerning answers to this kind of problem but this was not helpful for me. My specific problem is: In the code below the program runs and goes directly to the last line “system.out.println(“to this point”)”.


That's how Swing (and AWT) works. When you show a frame (by calling setVisible(true)), this does not block the current thread - it just continues. (Note that modal dialogs, like the file chooser dialog, will block the current thread, but no other windows will.)

I want that it waits until one pressed the button (buttonsav) to choose some files. Then after successful done it should go to the last line System.out.println("to this Point"). Later these files are processed. The program cannot run without these files. So, how to let the program wait for buttonclick.


That's what the button's action listeners are for (there can be multiple). Move the println statement inside your action listener code, like Swastik has shown.
 
stefanie ohm
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, in principle yes this works. I have got a much larger code with additional buttons to be pressed before to proceed.
From the remarks I see the whole has something to do with threads. I am not familiar with that topic. In VB we have shown_form() where
you can do everything when the window becomes visible. I do not think that we have got something equivalent in Java.
Is it possible to stop the thread which goes to system.out.println...(go to system.out.println...) ?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

stefanie ohm wrote:In VB we have shown_form() where
you can do everything when the window becomes visible. I do not think that we have got something equivalent in Java.


It's there, and it uses the same principle as VB does - using event handlers. The thing is, in Java you have to attach event listeners to user interface objects. In this case it would be a java.awt.event.WindowListener (windowOpened), or possibly a java.awt.event.ComponentListener (componentShown).

Is it possible to stop the thread which goes to system.out.println...(go to system.out.println...) ?


Like I said, in AWT and Swing, all code in the main method, as well as in event handlers, will continue to run until either the method ends, or until a modal dialog is shown (e.g. using JOptionPane). In case of a modal dialog, the method will continue as soon as the modal dialog is disposed (closed).
 
stefanie ohm
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, when I understand it correctly the problem cannot be solved or ?
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

stefanie ohm wrote:Thanks, in principle yes this works. I have got a much larger code with additional buttons to be pressed before to proceed.
From the remarks I see the whole has something to do with threads. I am not familiar with that topic. In VB we have shown_form() where
you can do everything when the window becomes visible. I do not think that we have got something equivalent in Java.
Is it possible to stop the thread which goes to system.out.println...(go to system.out.println...) ?



Hi, Stephanie, please take a look at SwingWorker class for Java Swing.

With best regards,

Anton.
 
No more fooling around. Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic