Results 1 to 12 of 12
- 04-20-2010, 10:08 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
Problem with writting in JEditorPane.
Hello everybody,
I have problem with JEditorPane:
I have:
String s = new String("");
for (int i=0; i<1000; i++){
s = s + "coś";
jEditorPane.setText(s);
Thread.sleep(200);
}
It works, but jEditorPane write this text after 1000 * 200 miliseconds.
Why jEditorPane dont write the text 1000 times with 200 miliseconds pause?
What can I do that?
- 04-20-2010, 10:23 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
try this:
Java Code:String s = new String(""); for (int i=0; i<1000; i++){ s = s + "coś"; jEditorPane.setText(s); } Thread.sleep(200);
- 04-20-2010, 11:58 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That doesn't actually solve the problem, does it? That'll pause the thread after the full string has been built, not after each iteration.
Try a repaint(). I suspect it's only repainting some point after the loop has finished. Not that I'm a Swing expert or anything.
- 04-20-2010, 01:01 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
- 04-20-2010, 01:08 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Is this processing being done in a worker thread or similar?
If it's in the EDT (ie the main Swing thread) then you won't see anything.
- 04-20-2010, 02:33 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
Thanks for reply,
I'll try writting with separate threads.
- 04-20-2010, 02:55 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Essentially the EDT won't redraw (it can't in fact) while you're doing stuff on the EDT thread. That is why you should do any processing stuff on a separate thread.
- 04-20-2010, 03:22 PM #8
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
You're right. Sorry.That doesn't actually solve the problem, does it? That'll pause the thread after the full string has been built, not after each iteration.
-
- 04-20-2010, 10:47 PM #10
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
Thanks for help.
This solutions dont work. This is the same problem: what should I do to repaint my jEditorPane. Nevermind if I do this using Swing Timer or make this here:
String s = new String("");
for (int i=0; i<1000; i++){
s = s + "coś";
jEditorPane.setText(s);
//do something to refresh, but how?
Thread.sleep(200);
}
-
Then you may be using it wrong as it works fine for me; but how you're doing it wrong -- I've no idea without code. I suggest that you show your attempt with a Swing Timer in a small bit of compilable code that we can run unaltered on our own computers. Also, describe as best possible what effect you're trying to achieve here (sorry, but it's not clear to me), and why you're using a JEditorPane rather than the simpler JTextArea (you could have a very good reason, but again, it's not clear to me). Best of luck.
Last edited by Fubarable; 04-20-2010 at 11:19 PM.
-
For instance if you wanted to append text into a JTextArea with a 200 ms delay, a Timer would work great:
But if you want to do something different, then please let us know.Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class TimerText { private static void createAndShowGUI() { JFrame frame = new JFrame("Timer Text"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new FlashPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } class FlashPanel extends JPanel { private static final int MAX_COUNT = 30; private JTextArea area1 = new JTextArea(20, 20); private JButton startBtn = new JButton("Start"); private int count = 0; FlashPanel() { add(startBtn); add(new JScrollPane(area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)); startBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { count = 0; startBtn.setEnabled(false); new javax.swing.Timer(200, new ActionListener() { public void actionPerformed(ActionEvent e) { timerActionPerformed(e); } }).start(); } }); } private void timerActionPerformed(ActionEvent e) { if (count >= MAX_COUNT) { javax.swing.Timer timer = (Timer) e.getSource(); timer.stop(); startBtn.setEnabled(true); } area1.append("area1 foo " + count + "\n"); count++; } }
Similar Threads
-
Writting output to file!
By hakan123 in forum New To JavaReplies: 8Last Post: 11-19-2009, 04:39 PM -
Javascript in JEditorpane
By Narayan15 in forum AWT / SwingReplies: 1Last Post: 03-27-2009, 05:49 PM -
writting extended ascii chars on socket........or Endianness Issue......??
By sachinj13 in forum Threads and SynchronizationReplies: 8Last Post: 09-23-2008, 02:20 PM -
JEditorPane Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:47 PM -
JEditorPane
By drmmr11 in forum Java AppletsReplies: 0Last Post: 08-02-2007, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks