Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problems with Java from Matlab

Subject: Problems with Java from Matlab

From: Pascal Steiss

Date: 29 Aug, 2009 14:54:12

Message: 1 of 2

Hi All!

I wrote a couple of classes in Java.
If I start the program from console, it works fine.
The Main class is a descendant from JFrame.

If I want to create the object in Matlab, I see an empty JFrame form -
without contents (which is a descendent of JPanel)

Does anyone have any ideas?

Thanks
Pascal

-- snip --
public class Main extends JFrame {
     protected static ODView myODView;
     protected boolean mode3D;

     /** Creates a new instance of Main */
     public Main() {
         super("Fenster");
         setSize(300,300);
         setLocation(300,300);
         //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         setVisible(true);
     }

     /**
      * @param args the command line arguments
      */
     public static void main(String[] args) {
          Main g = new Main();
          myODView = new ODView2D();
          g.add(myODView);
          double myData[][] = new double[55][55];
          Random myRandom = new Random();
          for (int i=0; i<55; i++)
              for (int j=0; j<55; j++) {
                 myData[i][j] = myRandom.nextInt();
              }
          myODView.SetData(myData);
     }

     protected void Init() {
         mode3D = false;
         Set2D();
     }

     public void Set3D() {
         if (mode3D == false) {

             mode3D = true;
         }

     }

     public void Set2D() {
         if (mode3D == true) {
             myODView = new ODView2D();
             mode3D = false;
         }
     }


     public void SetData(double[][] newData) {
         myODView.SetData(newData);
     }
}
-- snap --

Subject: Problems with Java from Matlab

From: Yair Altman

Date: 29 Aug, 2009 20:28:03

Message: 2 of 2

Your GUI actions in main() probably need to be done asynchronously, on the EDT. This may solve a threading race-condition that may be the cause of your problem. There are plenty of examples and explanations online. Here's one snippet:

import java.awt.EventQueue;
import javax.swing.*;
 
public class B
{
  JFrame f;
 
  B()
  {
    f = new JFrame();
  }
 
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        B b = new B();
        b.f.setVisible(true);
      }
    });
  }
}


Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us