Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > simple stuff in swing

Reply
Thread Tools

simple stuff in swing

 
 
defcon8
Guest
Posts: n/a
 
      11-05-2005
Hello, I have just started learning swing and I have tried to make a
small programme that shows a number that goes up by 1 every increment,
I am trying to figure out how I can make it sleep and so on. Could
anyone correct my code?

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Grapho {
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(400,400);
JLabel label = new JLabel();
int i = 0;
while(i != 10) {
label.setText(Integer.toString(i));
Thread.sleep(1);
frame.getContentPane().add(label);
frame.setvisible(true);
i++;
}
}
}

 
Reply With Quote
 
 
 
 
Demetz Markus
Guest
Posts: n/a
 
      11-05-2005
hi,

you can extend Thread and do the stuff in the run method!


"defcon8" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> Hello, I have just started learning swing and I have tried to make a
> small programme that shows a number that goes up by 1 every increment,
> I am trying to figure out how I can make it sleep and so on. Could
> anyone correct my code?
>
> import javax.swing.JFrame;
> import javax.swing.JLabel;
>
> public class Grapho {
> public static void main(String[] args)
> {
> JFrame frame = new JFrame();
> frame.setSize(400,400);
> JLabel label = new JLabel();
> int i = 0;
> while(i != 10) {
> label.setText(Integer.toString(i));
> Thread.sleep(1);
> frame.getContentPane().add(label);
> frame.setvisible(true);
> i++;
> }
> }
> }
>



 
Reply With Quote
 
 
 
 
Leon
Guest
Posts: n/a
 
      11-05-2005

"Demetz Markus" <(E-Mail Removed)> wrote in message
news:436c9f0c$0$10578$(E-Mail Removed) ...
> hi,
>
> you can extend Thread and do the stuff in the run method!


You can also implement the Runnable interface, to save the extend for something
else.

class ClassX implements Runnable {

public void run() {
// some code ..
}
}

// now you can do the following:
Thread thread = new Thread( new ClassX() );
thread.start(); // will execute the run method.


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-05-2005
On 5 Nov 2005 02:00:40 -0800, "defcon8" <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

> Thread.sleep(1);


sleeping for one millisecond will not be noticeable. You also need a
try block around it in case you are prematurely awakened.

See http://mindprod.com/jgloss/sleep.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      11-05-2005
"defcon8" <(E-Mail Removed)> wrote in news:1131184840.218170.129190
@z14g2000cwz.googlegroups.com:

> Hello, I have just started learning swing and I have tried to make a
> small programme that shows a number that goes up by 1 every increment,
> I am trying to figure out how I can make it sleep and so on. Could
> anyone correct my code?
>
> import javax.swing.JFrame;
> import javax.swing.JLabel;
>
> public class Grapho {
> public static void main(String[] args)
> {
> JFrame frame = new JFrame();
> frame.setSize(400,400);
> JLabel label = new JLabel();
> int i = 0;
> while(i != 10) {
> label.setText(Integer.toString(i));
> Thread.sleep(1);
> frame.getContentPane().add(label);
> frame.setvisible(true);
> i++;
> }
> }
> }
>


Thread.sleep takes an argument in miliseconds. Try using a value of 1000:

Thread.sleep(1000);

That will make your thread sleep for 1 second.

Normally if you're mixing threads with Swing, you should use
SwingUtilities.invokeLater. For something as simple as this it doesn't
really matter, but for future reference you should look into it.

Also, you should take the frame.getContentPane().add(label) and
frame.setVisible(true); (note the capital V!) out of the while loop. Once
the label has been added and the frame displayed, you don't need to do it
again every time.
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      11-05-2005
defcon8 wrote:
> Hello, I have just started learning swing and I have tried to make a
> small programme that shows a number that goes up by 1 every increment,
> I am trying to figure out how I can make it sleep and so on. Could
> anyone correct my code?
>
> import javax.swing.JFrame;
> import javax.swing.JLabel;
>
> public class Grapho {
> public static void main(String[] args)
> {
> JFrame frame = new JFrame();


First off, you shouldn't setVisible from the outside of the AWT Event
Dispatch Thread, as Swing is not thread-safe. You could get away with
that prior to 1.5, but it appears that these days the JVM is better
optimised and shows up thread bugs more often. It's better to create all
the components on the EDT. You can do this from main, with code like:

java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
... your code here ...
}
});

All this is doing is making sure your code is working in a single
threaded environment.

> frame.setSize(400,400);
> JLabel label = new JLabel();
> int i = 0;
> while(i != 10) {
> label.setText(Integer.toString(i));
> Thread.sleep(1);


The time is specified in milliseconds. One millisecond is quite short.
To make it more explicit, from 1.5, you can write:

java.util.concurrent.TimeUnit.SECONDS.sleep(1);

You shouldn't sleep on the EDT, as it will prevent repaints.

> frame.getContentPane().add(label);
> frame.setvisible(true);


No need keep adding the label or making the frame visible.

> i++;
> }
> }
> }


Now, how to solve the problem. No need to get involved with threads
yourself. Use javax.swing.Timer (not java.util.Timer!), to execute an
action that updates the label, every 1000 milliseconds. The delay will
have to be milliseconds this time, although you can store the value in a
variable whose name ends in Millis or _MILLIS.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
defcon8
Guest
Posts: n/a
 
      11-06-2005
I finally solved it in the end. And here it is:

import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;

public class Grapho {

static JFrame frame;
static int i = 0;
static int j = 0;
static int o;
static Timer timer;
static JLabel label;

public static void Reminder(int seconds) {
while(i < 31) {
o = i;
j = 0;
label.setText(Integer.toString(i));
timer.schedule(new RemindTask(), seconds*1000);
while(j != 1) {
if(o + 1 == i) {
j++;
}
}

}
label.setText("I have finished");
}
static class RemindTask extends TimerTask {
public void run() {
i++;
}
}
public static void main(String[] args)
{
label = new JLabel();
timer = new Timer();
frame = new JFrame();
frame.setSize(500,500);
frame.getContentPane().add(label);
frame.setVisible(true);
Reminder(1);

}

}

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-06-2005
On Sat, 05 Nov 2005 19:38:48 +0000, Thomas Hawtin
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>Now, how to solve the problem. No need to get involved with threads
>yourself. Use javax.swing.Timer (not java.util.Timer!), to execute an
>action that updates the label, every 1000 milliseconds. The delay will
>have to be milliseconds this time, although you can store the value in a
>variable whose name ends in Millis or _MILLIS.


for more details, see http://mindprod.com/jgloss/timer.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      11-06-2005
"defcon8" <(E-Mail Removed)> wrote in news:1131266869.606980.62900
@g43g2000cwa.googlegroups.com:

> I finally solved it in the end. And here it is:
>
> import javax.swing.*;
> import java.util.Timer;
> import java.util.TimerTask;
>
> public class Grapho {
>
> static JFrame frame;
> static int i = 0;
> static int j = 0;
> static int o;
> static Timer timer;
> static JLabel label;
>
> public static void Reminder(int seconds) {
> while(i < 31) {
> o = i;
> j = 0;
> label.setText(Integer.toString(i));
> timer.schedule(new RemindTask(), seconds*1000);


Have a look at the overloaded schedule methods in Timer. You don't need
to re-schedule it every time, you can tell the Timer to repeat every n
miliseconds after a specific time or delay.

> while(j != 1) {
> if(o + 1 == i) {
> j++;
> }
> }


What does this while loop do? Seems like you're making this way more
complicated than it should be.

>
> }
> label.setText("I have finished");
> }
> static class RemindTask extends TimerTask {
> public void run() {
> i++;
> }
> }
> public static void main(String[] args)
> {
> label = new JLabel();
> timer = new Timer();
> frame = new JFrame();
> frame.setSize(500,500);
> frame.getContentPane().add(label);
> frame.setVisible(true);
> Reminder(1);
>
> }
>
> }
>

 
Reply With Quote
 
Pradyut
Guest
Posts: n/a
 
      11-06-2005
the process consumes 100% processing power
write a simpler code, for example something as delay and implement
using sleep wakeup from the class knwn as Thread
happy coding...
Thanks

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India

defcon8 wrote:
> I finally solved it in the end. And here it is:
>
> import javax.swing.*;
> import java.util.Timer;
> import java.util.TimerTask;
>
> public class Grapho {
>
> static JFrame frame;
> static int i = 0;
> static int j = 0;
> static int o;
> static Timer timer;
> static JLabel label;
>
> public static void Reminder(int seconds) {
> while(i < 31) {
> o = i;
> j = 0;
> label.setText(Integer.toString(i));
> timer.schedule(new RemindTask(), seconds*1000);
> while(j != 1) {
> if(o + 1 == i) {
> j++;
> }
> }
>
> }
> label.setText("I have finished");
> }
> static class RemindTask extends TimerTask {
> public void run() {
> i++;
> }
> }
> public static void main(String[] args)
> {
> label = new JLabel();
> timer = new Timer();
> frame = new JFrame();
> frame.setSize(500,500);
> frame.getContentPane().add(label);
> frame.setVisible(true);
> Reminder(1);
>
> }
>
> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Swing is dead! Long live Swing. Knute Johnson Java 32 02-29-2012 05:10 PM
Why not using javax.swing.event with swing? S.T Java 2 05-25-2007 12:10 AM
javax.swing.Popup, javax.swing.PopupFactory lizard Java 0 01-30-2006 09:34 PM
Swing Model Classes Updating Swing Components on a Thread Other Than AWT mkrause Java 0 05-06-2005 04:32 PM
Java 1.2 Swing vs. Java 1.5 Swing Big Daddy Java 2 04-16-2005 01:14 PM



Advertisments