title: AWTEvent and EventQueue
body: <p>I have an external device that is sending me data 1 character at a time. I'm writing this to a StyledDocument on a JTextPane. This data is sent to me on a thread that is not the AWT thread so I need to create AWTEvents and push them to the EventQueue so AWT handles the writing so that I do not get an Exception.</p>
  
  <p>I have a funny issue now...</p>
  
  <p>My text is printing to the document backwards. </p>
  
  <p>This is obviously because I am pushing the characters to the Event queue 1 at a time as i receive them. A queue is obviously last pushed is first popped. I'm trying to thing of a way that I can fire the event before I add a new one or something similar so that I can get the events to fire in order.</p>
  
  <p><a href="http://www.kauss.org/Stephan/swing/index.html" rel="nofollow">http://www.kauss.org/Stephan/swing/index.html</a> is the example I used to create the events.</p>
  
  <pre><code>private class RUMAddTextEvent extends AWTEvent {
  
      public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1;
      private int index;
      private String str;
      private AttributeSet as;
  
      public RUMAddTextEvent(Component target, int index, String str, AttributeSet as) {
          super(target, EVENT_ID);
          this.index = index;
          this.str = str;
          this.as = as;
      }
  
      public int getIndex() {
          return index;
      }
  
      public String getStr() {
          return str;
      }
  
      public AttributeSet getAs() {
          return as;
      }
  }
  
  private class RUMRemoveTextEvent extends AWTEvent {
  
      public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1;
      int index;
      int size;
  
      RUMRemoveTextEvent(Component target, int index, int size) {
          super(target, EVENT_ID);
          this.index = index;
          this.size = size;
      }
  
      public int getIndex() {
          return index;
      }
  
      public int getSize() {
          return size;
      }
  }
  
  /**
   * Prints a character at a time to the RUMComm window.
   *
   * @param c
   */
  public void simpleOut(Character c) {
      cursor.x++;
      if (lines.isEmpty()) {
          this.lines.add(c.toString());
      } else {
          this.lines.add(cursor.y, this.lines.get(cursor.y).concat(c.toString()));
          this.lines.remove(cursor.y + 1);
  
      }
  
      try {
          //doc.insertString(doc.getLength(), c.toString(), as);
  
          eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
          eventQueue.postEvent(new RUMAddTextEvent(this, doc.getLength(), c.toString(), as));
          getCaret().setDot(doc.getLength());
      } catch (Exception ex) {
          //Exceptions.printStackTrace(ex);
      }
  }
  
  /**
   * Creates a new line
   */
  public void newLine() {
      cursor.y++;
      cursor.x = 0;
      this.lines.add("");
  
      //doc.insertString(doc.getLength(), "\n", null);
      eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
      eventQueue.postEvent(new RUMAddTextEvent(this, doc.getLength(), "\n", null));
      getCaret().setDot(doc.getLength());
  
  }
  
  /**
   * Backspace implementation.
   *
   */
  public void deleteLast() {
      int endPos = doc.getLength();
      //doc.remove(endPos - 1, 1);
      eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
      eventQueue.postEvent(new RUMRemoveTextEvent(this, endPos - 1, 1));
      cursor.x--;
  
  }
  
   @Override
  protected void processEvent(AWTEvent awte) {
      //super.processEvent(awte);
      if (awte instanceof RUMAddTextEvent) {
          RUMAddTextEvent ev = (RUMAddTextEvent) awte;
          try {
              doc.insertString(ev.getIndex(), ev.getStr(), ev.getAs());
          } catch (BadLocationException ex) {
              Exceptions.printStackTrace(ex);
          }
      } else if (awte instanceof RUMRemoveTextEvent) {
          RUMRemoveTextEvent ev = (RUMRemoveTextEvent) awte;
          try {
              doc.remove(ev.getIndex(), ev.getSize());
          } catch (BadLocationException ex) {
              Exceptions.printStackTrace(ex);
          }
  
      } else {
          super.processEvent(awte);
      }
  }
  </code></pre>