Java graphical interface halting without Thread suspension?
-
I have been programming in Java for about 6 months now, and the whole time I have been using the Netbeans IDE. However, I wanted to start making graphical applications (I had been sticking to textual ones before.) I've been using the Mitesse GUI builder since I don't know where to begin making graphical interfaces through code, but I run into one issue. In this particular instance I'm trying to increment the value of a progress bar every second, and the code I am using is: for(int seconds=0; seconds<100; seconds++) { progressBar.setValue(seconds); Thread.sleep(1000) } Now, this type of code works when you aren't working with graphics, however when I run the program the graphics (the program itself executes fine, I tested that with printing) freezes for the entire minute where Thread.sleep() is being called. I even tried making a new Thread other than the main one for the graphics and I still get the same symptoms. Is there any way around this?
-
Answer:
Swing is already a Thread in progress. The main thread in Swing is called the Event Dispatch Thread. For 99% accuracy you are supposed to start out main()... public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(n… Runnable() { public void run() { createAndShowGUI(); } }); } then in child threads // The snippet is from "Filthy Rich Clients" private void incrementProgress() { Runnable code = new Runnable() { public void run() { System.out.println("in child thread"); } }; if( SwingUtiilities.isEventDispatchThread() ) { code.run(); } else { SwingUtilties.invokeLater( code ); } } // the above snippet becomes mandatory when making flashing buttons, bouncing balls and whirling gears in a GUI. the other thing I can think of is: Are you using javax.swing.Timer. Don't use java.util.Timer when working with Swing. A lot of kinks are also overcome when we subclass. I notice you like FormsBuilder. I don't. I hand-code my layouts and such a coding style lends itself to subclassing and customizing paintComponent( Graphics g )
Destroyer of Worlds at Yahoo! Answers Visit the source
Related Q & A:
- Can a thread not block if the lock it tries to lock is not available?Best solution by Stack Overflow
- How to make a thread wait until another thread finishes?Best solution by Stack Overflow
- Is it unsafe to call methods from a thread?Best solution by stackoverflow.com
- Java: How to implement the interface?Best solution by Stack Overflow
- How do I do time.sleep in certain thread in Python?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.