Solved

displaying one at a time

Posted on 2004-04-03
3
210 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

756 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