Solved

displaying one at a time

Posted on 2004-04-03
3
206 Views
Last Modified: 2010-05-18
hi, i wrote a simple program that contains a button and a jtextarea

when i click on the button, it will do the following...
jtextarea.append("a\n");        //statement a
jtextarea.append("b\n");        //statement b
jtextarea.append("c\n");        //statement c

wat i reli want is that...it shows statement a on the jtextarea first...after a few seconds, statement b....a few more seconds, statement c

i tried using this.sleep(1000), but it will just hang for a few seconds and then finally display everything at once

how do i display those statements once at a time? i'm using actionPerformed() for the button, is this correct?


thx,
gildan2020
0
Comment
Question by:gildan2020
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 50 total points
Comment Utility
You need to do the append in a new thread:

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

public class MyClass extends JPanel implements ActionListener {

  JTextArea myJTextArea = new JTextArea();
  JButton myJButton = new JButton("Click Me");

  // Constructor
  public MyClass() {
    this.setLayout(new BorderLayout());
    this.add(myJButton, BorderLayout.NORTH);
    this.add(myJTextArea, BorderLayout.CENTER);
    myJButton.addActionListener(this);
  }

  // ActionListener implementation
  public void actionPerformed(ActionEvent ae) {
    System.out.println("+++  Action Performed  +++");
    Thread appendThread = new Thread() {
      public void run() {
        try {
          myJTextArea.append("a\n");        //statement a
          sleep(1000);
          myJTextArea.append("b\n");        //statement b
          sleep(1000);
          myJTextArea.append("c\n");        //statement c
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    };
    appendThread.start();
  }

  private static final String FRAME_TITLE = "My Class";
  private static final int FRAME_WIDTH  = 500;
  private static final int FRAME_HEIGHT = 300;

  public void showFrame() {
    JFrame myFrame = new JFrame(FRAME_TITLE) {
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); }
      }
    };
    myFrame.getContentPane().add(this, BorderLayout.CENTER);
    myFrame.pack();
    myFrame.setSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
    myFrame.setVisible(true);
  }

  public static void main(String[] args) {
    MyClass myMyClass = new MyClass();
    myMyClass.showFrame();
  }

}
0
 

Author Comment

by:gildan2020
Comment Utility
thanks a lot
that's exactly wat i wanted
:-)

gildan2020
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
:°)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now