how reapint JLabel

R

Rafal\(sxat\)

Hi

how repaint JLabel in for loop


while (i<1000){
label.setText("i:" + i);

label.validate() - not work
label.repaint() - not work
label.paintAll(label.getGraphics()) not work...
}

this procedure is running in thread

Rf
 
A

Albert

Rafal(sxat) a écrit :
Hi

how repaint JLabel in for loop


while (i<1000){
label.setText("i:" + i);

label.validate() - not work
label.repaint() - not work
label.paintAll(label.getGraphics()) not work...
}

this procedure is running in thread

Rf

You should not repaint each JLabel separatly: you can:
1/ call repaint() once after the for loop
2/ set a timer which call repaint every 100ms (for example) during the
for loop

Anyway, don't call Swing methods from a thread which is not the EDT. Or
call EventQueue.invokeLater(Runnable).

HTH
 
A

Albert

Rafal(sxat) a écrit :
if I start loop for sending... Timer not working.... and JLabel is empty...

Damned, you use as few words as possible!

Do you use javax.swing.Timer ? Do you start it ? Timer should not have a
bug, so it must your use of it... Are all the labels added to the
containers before you start the timer ?
 
K

Knute Johnson

Rafal(sxat) said:
Hi

how repaint JLabel in for loop


while (i<1000){
label.setText("i:" + i);

label.validate() - not work
label.repaint() - not work
label.paintAll(label.getGraphics()) not work...
}

this procedure is running in thread

Rf

There should be no reason to repaint the JLabel after you call setText()
on it.
 
K

Knute Johnson

Rafal said:
always label is is blurred by text...

I don't know what that means.
all code is executable as JApplet by java.awt.EventQueue.invokelater

That's fine.

In any case I repeat my statement, you should not need to call repaint()
after calling setText() on a JLabel.
 
M

Mark Space

Rafal said:
this code is running by java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){....}


So then so us an example that 1) reproduces the problem, and 2) runs.
The example you provided does not run. Complete code please, but keep
it short enough that we will actually read it.
 
A

Albert

Rafal (sxat) a écrit :
...
Damned, you use as few words as possible!


long sendFile(DataOutputStream osw, File sp,long fromSize) throws
Exception{
FileInputStream fi=new FileInputStream(sp);
fi.skip(fromSize);
int b,maxSize=maxPost;
long curSize=fromSize;
boolean byl=false;
byte[] dane = new byte[1024];
long ofKB=(long)sp.length()/1024;
long curKB;
while ((b=fi.read(dane))!=-1){
byl=true;
int postep=(int)(100*curSize/sp.length());
curKB=(long)curSize/1024;
progress.setValue(postep);
progress.update(progress.getGraphics());
progress.repaint();
updateLabels("Wyslane: " + curSize);
osw.write(dane, 0, b);
maxSize-=b;
curSize+=b;
if(maxSize<=0) break;
if (stopFlags==1) break;
}
osw.flush();
labpostep.setText("");
System.out.println("Sending from=" + fromSize +"; to=" + curSize);
return curSize;
}

public void updateLabels(final String i) { /not working/
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
labpostep.setText(i +"");
}
});
}
public void updateLabels(final String i) { /not working/
labpostep.setText(i +"");
labpostep.repaint();
}
}



this code is running by java.awt.EventQueue.invokeLater(new Runnable()
rf

I can give you some skeleton for your application:
1/ put senFile in a Thread subclass
2/ create another Thread subclass like this:

class MonitorThread extends Thread {
public void run() {
EventQueue.invokeLater(new Runnable() {
// init on the EDT
progress.setValue(0);
});

// start the send thread
SendThread sendThread = SendThread(theFile);
sendThread.start();

// while the send thread is not finished
while (sendThread.isAlive()) {
Thread.sleep(100);

EventQueue.invokeLater(new Runnable() {
// show progress on the EDT
progress.setValue(sendThread.getProgress());
});
}

// task finished
EventQueue.invokeLater(new Runnable() {
// hide progress ? (on the EDT)
progress.setVisible(false);
});
}

Now you need to start the monitor thread (and put the parameters like
the File in it)
 
R

RedGrittyBrick

Rafal said:
always label is is blurred by text...

Humans can't read anything that persists for less than 100 ms so don't
try to change the JLabel to show all values when those values change
every few nanoseconds.

I'd track time since last update and only setText() if elapsed time > 250ms.

I'd consider using MVC and perhaps have the view decide how frequently
to update the label to reflect a fast changing observable value.
all code is executable as JApplet by java.awt.EventQueue.invokelater

Probably only the label.setText() needs to be run that way. You don't
need to repaint.
 
R

Roedy Green

how repaint JLabel in for loop
while (i<1000){
label.setText("i:" + i);

label.validate() - not work
label.repaint() - not work
label.paintAll(label.getGraphics()) not work...
}
this procedure is running in thread

You don't need to do anything other than setText and get the hell out
the way. If you tie up the EDT thread or tie up the CPU, the system
can never get round to processing your repaint event.

see http://mindprod.com/jgloss/swingthreads.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

Never discourage anyone... who continually makes progress, no matter how slow.
~ Plato 428 BC died: 348 BC at age: 80
 
R

Rafal\(sxat\)

I can give you some skeleton for your application:
1/ put senFile in a Thread subclass
2/ create another Thread subclass like this:

class MonitorThread extends Thread {
public void run() {
EventQueue.invokeLater(new Runnable() {
// init on the EDT
progress.setValue(0);


I make it... .. it works quite well, but update progress I am running in
timer because
Thread.sleep(100) resulted in a hung IE browser (applet)

I am setting delay on 1000 ms, but update but it does not perform the
equivalent time at 1 second and once every 5
how repaired it? Can anything changing priorities of threads?

Rf
 
K

Knute Johnson

Rafal(sxat) said:
I make it... .. it works quite well, but update progress I am running in
timer because
Thread.sleep(100) resulted in a hung IE browser (applet)

I am setting delay on 1000 ms, but update but it does not perform the
equivalent time at 1 second and once every 5
how repaired it? Can anything changing priorities of threads?

Rf

package com.knutejohnson.components;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

public class JTimeLabel extends JLabel implements ActionListener {
private final SimpleDateFormat sdf;
private final javax.swing.Timer timer = new
javax.swing.Timer(250,this);

public JTimeLabel(String text, int horizontalAlignment, String
pattern) {
super(text, horizontalAlignment);
sdf = new SimpleDateFormat(pattern);
}

public JTimeLabel() {
this("00:00:00",JLabel.CENTER,"HH:mm:ss");
}

public JTimeLabel(String text) {
this(text,JLabel.CENTER,"HH:mm:ss");
}

public JTimeLabel(String text, int horizontalAlignment) {
this(text,horizontalAlignment,"HH:mm:ss");
}

public void start() {
timer.start();
}

public void stop() {
timer.stop();
}

public void actionPerformed(ActionEvent ae) {
setText(sdf.format(new Date()));
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTimeLabel tl = new JTimeLabel();
tl.setFont(new Font("Arial",Font.BOLD,32));
f.add(tl);
tl.start();
f.pack();
f.setVisible(true);
}
});
}
}
 
A

Albert

Rafal(sxat) a écrit :
I make it... .. it works quite well, but update progress I am running in
timer because
Thread.sleep(100) resulted in a hung IE browser (applet)

I am setting delay on 1000 ms, but update but it does not perform the
equivalent time at 1 second and once every 5
how repaired it? Can anything changing priorities of threads?

Yes, but you could have searched a bit:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setPriority(int)

BTW, Timer has a "repeat" boolean, which you may need to set to true.
Have you done it ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top