0

hello i'm beginner in java language ... so my problem is that i want to make a thread typing in jtext area but i have no idea how to do that

i use netbines IDE

this is my code:

class md2:

import java.util.logging.Level;
import java.util.logging.Logger;

public class md2 implements Runnable{

    public void run(){  
        int i=0;
while(true){
    System.out.println("méthode 2 : "+i++); // that what i want to typing in the jtextarea

            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                Logger.getLogger(md2.class.getName()).log(Level.SEVERE, null, ex);
            }
}  

}} 

and this is my jframe :

    public class tp3 extends javax.swing.JFrame {


        public tp3() {

            initComponents();
        }
    md2 m1=new md2(); 

    Thread cp3 =new Thread(m1); 



        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
   tx = new javax.swing.JTextArea(); // this is where i want to typing
   tx.setColumns(20);
   tx.setRows(5);
        }// </editor-fold>                        

        private void sMouseClicked(java.awt.event.MouseEvent evt) {                               
            cp3.start();  // button to start the thread
        }                              
Taha Kerrouzi
  • 177
  • 2
  • 15
  • Post a [mcve] that demonstrates the problem. In addition to the code actually compiling, the code should be properly formatted and readable. Class names should be meaningful and start with upper case characters. If you want your Thread to update the text area, then the Thread needs a reference to the text area, so you would need to pass the text area as a parameter to the Thread class. – camickr Oct 25 '17 at 19:18
  • Best to start with something like [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) and [Worker Threads and SwingWorker](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) and [How to Use Swing Timers](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) may also help – MadProgrammer Oct 25 '17 at 19:31
  • [Possible duplicate Java add Typewriter effect to JTextArea](https://stackoverflow.com/questions/43333999/java-add-typewriter-effect-to-jtextarea/43334135#43334135) or [I can't run JTextArea multiple times?](https://stackoverflow.com/questions/15957845/java-i-cant-run-jtextarea-multiple-times/15957975#15957975) – MadProgrammer Oct 25 '17 at 19:34

0 Answers0