How do i use threads in a GUI in java?
-
I am trying to make a command line program and i can't seem to understand how to use threads in a GUI. Please explain in plain english and give examples. Thank y
-
Answer:
I made you a simple example using a JLabel, with an increasing number using a thread well actually i let the class extend thead, so the class is a Thread itself, maybe its helpful: import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; public class LabelThread extends Thread implements Runnable{ int i=1; private JLabel label=new JLabel(i+""); private boolean running=true; public static void main(String [ ] args) { new LabelThread(); } public LabelThread() { JFrame f=new JFrame(); f.setSize(200,200); f.setVisible(true); f.add(label,BorderLayout.CENTER); start(); } @Override public void run() { while(running) { i++; System.out.println(i+""); label.setText(i+""); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(i==100) { running=false; } } } }
Robert S at Yahoo! Answers Visit the source
Other answers
What does a GUI have to do with threads? Threads are quite a complex concept (especially in dealing with concurrency), maybe if you need to ask this question then you are not yet experienced enough to learn about threads.
I Don't Eat Vodka
Related Q & A:
- How can I use a button to retrieve a phone 'number' from contacts?Best solution by Stack Overflow
- How do I create a digital signature and how do I use it?Best solution by support.office.com
- How do I use a custom avatar like a picture of me?Best solution by Yahoo! Answers
- How do I use chat on a mac?Best solution by Yahoo! Answers
- How can I use recipient in a sentence?Best solution by sentence.yourdictionary.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.