Гринюк Андрей Гринюк Андрей - 7 months ago 13
Java Question

How to fix components display if run one class from another?

Have a class with some components on it like jpanel with buttons etc. When i run this class from a main method inside this class it works fine. The problem is that when i try to run this class constructor from another class the components disappar.

CanvasFrame canvas = new CanvasFrame("Web Cam");
CanvasFrame capturecanvas =new CanvasFrame("Preview");


Canvas capturecanva = new Canvas();

JPanel controlPanel = new JPanel();
JLabel capturepanel=new JLabel();
JButton captureBtn;
JButton acceptBtn;
JButton retakeBtn;
String currentCapture;
JPanel panel = new JPanel();

int total_Faces;

public FaceDetector(int interruptionCommnand) throws IOException
{
captureInterrupt();
}
public FaceDetector() throws IOException, FrameGrabber.Exception
{

FrameGrabber grabber = new VideoInputFrameGrabber(0);
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
capturecanvas.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
canvas.setSize(800,600);
canvas.setLayout(new MigLayout("insets 0, gap 10" ));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int locationX = (screenSize.width-240) / 2;
int locationY = (screenSize.height -550) / 2;
canvas.setBounds(locationX, locationY,840, 550);
panel.add(captureBtn = new JButton("Capture"), " w 100:100:100, h 40:40:40");
controlPanel.add(acceptBtn =new JButton("Accept"), "w 100:100:100, h 40:40:40");
controlPanel.add(retakeBtn=new JButton("Retake"), "w 100:100:100, h 40:40:40");
ActionListener RetakeActionListener = new RetakeActionListener();
retakeBtn.addActionListener(RetakeActionListener);
ActionListener AcceptActionListener = new AcceptActionListener();
acceptBtn.addActionListener(AcceptActionListener);
ActionListener CaptureActionListener = new CaptureActionListener();
captureBtn.addActionListener(CaptureActionListener);


canvas.add(panel,"south");
capturecanvas.setLayout(new MigLayout("wrap 3"));
capturecanvas.setVisible(false);
}

public static void main(String[] args) throws IOException, FrameGrabber.Exception
{
new FaceDetector();
}


When i try to run it like this from my another class
new FaceDetector();
the components are missing

Code after changes:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Thread thread = new Thread(new Runnable(){
public void run() {
try {
new FaceDetector();
} catch (IOException | FrameGrabber.Exception ex) {
Logger.getLogger(RegisterService.class.getName()).log(Level.SEVERE, null, ex);
}
}
});



thread.start();
while (thread.isAlive()==false)
{
try {
DatabaseConnectivity ndbc = new DatabaseConnectivity("select","");
ndbc.FrameShow();
} catch (SQLException ex) {
Logger.getLogger(RegisterService.class.getName()).log(Level.SEVERE, null, ex);
}

}


}

Answer

Try to run it within a Runnable.

Thread thread = new Thread(new Runnable(){
     public void run() {
         new FaceDetector();
     }
});

thread.start();