Solved

displaying one at a time

Posted on 2004-04-03
3
209 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 46
javap not working 8 58
how to debug htl and js pages 8 39
hashmap order 17 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

829 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