0

I'm trying to allocate a JPanel that implements Runnable interface in a JFrame. I'd made this sample for interpret my idea. I want to add a multi-threading panel that shows a text as demo to a window with a String as parameter of a new instance. The panel should have independent process so I implemented Runnable interface. But when I try to create a new instance o panel with a new instance of my class, It doesn't work.

What I am doing wrong?

imagePanel Panel class:

public class imagePanel extends JPanel implements Runnable{
    JLabel imageTest;

    public imagePanel(String textLabel)
    {
        this.setPreferredSize(new Dimension(300,300));
        imageTest = new JLabel(textLabel);
        imageTest.setPreferredSize(this.getPreferredSize());
    }

    public void setImageText(String newText){
        imageTest.setText(newText);
    }

    public void run(){
        this.add(imageTest);
    }

}

Main class test class:

public class test {
    public static void main(){
        JFrame frame = new JFrame("Test Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(300,300));            

        JPanel panel = new imagePanel("Text label");
        panel.setPreferredSize(frame.getPreferredSize());

        frame.add(panel);
        frame.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Donnis
  • 1
  • 1
    It's not very pretty to create a "multithreaded panel". Panel is a user interface component, it shouldn't do anything with threads. Also in your code `run()` will never be called, so it's "broken" too. – Kayaman Mar 29 '16 at 07:30
  • You've forget to add label to your panel – Sergiy Medvynskyy Mar 29 '16 at 07:31
  • @SergiyMedvynskyy While the example posted certainly won't add the label, assuming they've create a `Thread` which wraps the instance of `panel` at some point in their actual code, `this.add(imageTest);` should get called ... but that's a lot of "if"s – MadProgrammer Mar 29 '16 at 07:33
  • Sorry this is just a sample of what I'm trying do work that I'd wrote it fast, I'm working with face recognition with camera using Opencv APIs, need a independient process for that class and show directly in panel. – Donnis Mar 29 '16 at 07:38
  • Re: "a JPanel that implements Runnable" Sounds like a clear violation of the Single Responsibility Principle (https://en.wikipedia.org/wiki/Single_responsibility_principle) – Solomon Slow Mar 29 '16 at 15:20

2 Answers2

2
  1. Swing is not thread safe, meaning updates to the UI should only be made from within the context of the Event Dispatching Thread. See Concurrency in Swing for more details and Worker Threads and SwingWorker for a possible solution
  2. Swing layouts are lazy, which is a good thing, they won't update till you tell them (or some other event requires them to update, for example, the container is resized). You can trigger an update by calling revalidate and repaint on the container you are updating
  3. Creating a new instance of class doesn't magically connect all the instances you might have, that would make the program rather difficult to manage
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

there several things missing

VERY IMPORTANT - you should not update UI in other than the AWT EventDispatcher threads. Using Swing it even may lead to deadlocks. this post

you have to use the SwingUtilities to update the Swing UI components.

SwingUtilities.invokeLater(new Runnable(){
    public void run(){
        // update your UI components
    }
});

Next information - How you create a multi-threaded thread? What you miss is the entry point to run your "run" method:

public class MyMultiThreadedType implements Runnable {
    public void run() {
        // this will run in parallel
    }

    public static void main(String[] args) {
    MyMultiThreadedType mmt = new MyMultiThreadedType ();
    Thread t = new Thread(mmt);
    t.start(); // this will start a parallel thread
  } 
}
Community
  • 1
  • 1
gusto2
  • 11,210
  • 2
  • 17
  • 36