0

I am new to Java and I have one of the typical problem.I came to know that Java do not support multiple inheritence. So I want to know how to implement the below class

class JPT extends JPanel extends Thread;

Multilevel inhertence will not help me for obvious reasons that I cannot edit the library classed, I may end up in problem due to dependencies if I edit.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user2819669
  • 59
  • 2
  • 5

3 Answers3

4

You should use the Runnable interface....

class JPT extends JPanel implements Runnable

This way you would simply create a new Thread instance, passing it a reference of you JPT class and start it...

JTP jpt = new JPT();
Thread thread = new Thread(jpt);
thread.start();

Now, I have to tell you, that just scares me.

Swing is a single threaded framework. That is, all interactions and modifications to the UI are expected to be executed from within the context of the Event Dispatching Thread, meaning you should never try and modify any UI component from any thread other than the EDT.

Take a look at Concurrency in Swing for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

If you want to create a Thread on a sub-class of Panel, you can do

class JPT extends JPanel implements Runnable
{
    JPT()
    {
        new Thread(this).start();
    }

    public void run()
    {
        // Code to run in new thread here.
    }
}

Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
1

Of course, Java does not support multiple inheritance, but you can implement multiple interfaces within a class. In your case, I would use:

class JPT extends Jpanel implements Runnable {

Anything that implements Runnable, can then be passed as an argument to the Thread constructor. E.g.

JPT JPT = new JPT();
Thread t = new Thread(JPT);
T.start();
blackpanther
  • 10,998
  • 11
  • 48
  • 78