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);
}
}