Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Running Threads : number set by user input  RSS feed

 
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
 
Java Cowboy
Sheriff
Posts: 16084
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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!