[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

displaying one at a time

Posted on 2004-04-03
3
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 200 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

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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

656 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