Results 1 to 12 of 12
  1. #1
    marox is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default 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?

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    try this:

    Java Code:
    String s = new String("");
    
    for (int i=0; i<1000; i++){
    s = s + "coś";
    jEditorPane.setText(s);
    }
    Thread.sleep(200);

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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.

  4. #4
    marox is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for reply.

    Quote Originally Posted by Tolls View Post
    That doesn't actually solve the problem, does it?
    That's right.


    Quote Originally Posted by Tolls View Post
    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.
    repaint() doesnt work too.

    Any ideas??

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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.

  6. #6
    marox is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for reply,

    I'll try writting with separate threads.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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.

  8. #8
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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.
    You're right. Sorry.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  10. #10
    marox is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for help.

    Quote Originally Posted by Fubarable View Post
    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);
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by marox View Post
    Thanks for help.

    This solutions dont work.
    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    For instance if you wanted to append text into a JTextArea with a 200 ms delay, a Timer would work great:
    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++;
      }
    }
    But if you want to do something different, then please let us know.

Similar Threads

  1. Writting output to file!
    By hakan123 in forum New To Java
    Replies: 8
    Last Post: 11-19-2009, 04:39 PM
  2. Javascript in JEditorpane
    By Narayan15 in forum AWT / Swing
    Replies: 1
    Last Post: 03-27-2009, 05:49 PM
  3. writting extended ascii chars on socket........or Endianness Issue......??
    By sachinj13 in forum Threads and Synchronization
    Replies: 8
    Last Post: 09-23-2008, 02:20 PM
  4. JEditorPane Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:47 PM
  5. JEditorPane
    By drmmr11 in forum Java Applets
    Replies: 0
    Last Post: 08-02-2007, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •