Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Class Extends JFrame and implements Runnable - Where to put the code

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Class Extends JFrame and implements Runnable - Where to put the code

    Hi

    I am after some advice. I have a class that extends JFrame and also implements runnable.

    Basically it is a frame which has a JLabel used for progress message and an OK JButton.

    It also performs some command line tasks using the Runtime.exec() command which is why it implements runnable.

    This is a module which is set up and called from another Java program using:

    Thread t1 = new Thread(new ClassName());
    t1.start();

    Now, for the JFrame e.g. setSize() and setLayout() etc, do these go in the Run() method or in the Constructer?

    Should everything to do with the JFrame e.g. adding JLabel and JButton go in the constructer and the Runtime.exec() go in the Run() method?

    Just a bit confused where I could be putting what code?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Class Extends JFrame and implements Runnable - Where to put the code

    Normally Threads are used when you want to do two things at the same time. For example one could be to support the GUI and the other to do a task in the background. Is that the reason you are using a Thread?

    The constructor would setup the GUI and the run() method would do the background task.
    There is a possibility of a conflict with Swing classes and it is recommended that the initial code be run on the EDT to prevent conflicts. There are programs posted here that do it both ways: on the EDT or not.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Class Extends JFrame and implements Runnable - Where to put the code

    Yeah basically there is already a GUI open which is handling RMI requests from other clients.

    Whilst this is running this other JFrame is to perform another task whilst still being able to process the RMI requests which look things up in a database and also insert rows etc.

    so the Constructor would contain all the GUI / JFrame commands and the run() method would handle the Runtime.exec() task.

    Sounds good to me

Similar Threads

  1. Why POJO class implements Serializable Interface ?
    By noorul11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 07:34 AM
  2. implementing implements & extends
    By nischalinn in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2013, 10:00 PM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. is it possible to extends java class with another class from different package?
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 20th, 2012, 03:51 PM
  5. help taking code from java class put it in JSP page.
    By anavagomez in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2012, 10:57 AM