Solved

displaying one at a time

Posted on 2004-04-03
3
208 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
ID: 10747499
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
ID: 10747575
thanks a lot
that's exactly wat i wanted
:-)

gildan2020
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10747849
:°)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

770 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