• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running Threads : number set by user input  RSS feed

 
Nick de Waal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to run a certain amount of threads that print out the numbers the user inputs.
I have a Jframe with 3 Jtextfields.
1 is which number to start
2 is which number to stop at
3 is how many threads to run.

this needs to be done using subclassing so class extends thread.

this is what I have:


and in the Jframe:



This is wrong because in the ThreadTest class I cannot use txtFrom.getText() because its part of the Jframe
I am really unsure how to make this work
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do this:

  • Add a constructor to your ThreadTest class that takes two parameters, the "to" and "from" values (as integers).
  • Add two fields to the ThreadTest class to hold these values.
  • In the constructor, set the value of the fields from the parameters of the constructor.
  • In the run() method, use the fields instead of Integer.parseInt(txtFrom.getText()) etc.
  • In btnRunActionPerformed, get the values from the txtFrom and txtTo text boxes and pass them to the new ThreadTest object you're creating.

  •  
    Nick de Waal
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Jesper ,

    I have not tried this yet but just looking at it I realize it will work
    thank you very much, I will implement this soon and mark this post as solved,
    I will let you know how I get on.
     
    Nick de Waal
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:You could do this:

  • Add a constructor to your ThreadTest class that takes two parameters, the "to" and "from" values (as integers).
  • Add two fields to the ThreadTest class to hold these values.
  • In the constructor, set the value of the fields from the parameters of the constructor.
  • In the run() method, use the fields instead of Integer.parseInt(txtFrom.getText()) etc.
  • In btnRunActionPerformed, get the values from the txtFrom and txtTo text boxes and pass them to the new ThreadTest object you're creating.



  • Hi Jesper,

    Sorry for the late reply, this is what I have :

    Jframe:


    ThreadTest class:


    please feel free to mention any problems or if I could have done this better.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!