Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java pass object from JFrame to JPanel

I am developing a standalone application, in Java using NetBeans, that gets and sends data via Serial Port. I am using a great API for Serial communication called java simple serial connector http://code.google.com/p/java-simple-serial-connector/

Please download the following NetBeans project that I created: http://netload.in/dateiN9xmwRtn19.htm Even if you do not have netbeans you can still view the code.

The above is a small example project that I made to explain what I wish to accomplish. The example contains a JFrame that contains the main method. This JFrame contains 2 panels: a Panel with navigation buttons, and a Panel that displays Panel 1 and Panel 2 using CardLayout.

enter image description here

enter image description here

In the JFrame I have a method that gets port names list using the getPortNames() method of the jssc API. My question is, how do I pass these port names String values from JFrame to my Panel 1, without using the following since it will not work: MyJFrame myjframe = new MyJFrame(); myjframe.getPortNames();

Hovercraft Full Of Eels Once again thank you very much for your explanation, it is worth gold. I now realize that my problem is NetBeans rather than Java. I am not familiar with it enough but I would still prefer to use it due to the size and complexity of my project. I have uploaded 2 more screenshots below, that show how I tried to pass the "this" reference of the JFrame object to my JPanel object in NetBeans. As you can see in the MyJFrame.java screenshot, NetBeans has underlined "this" reference as an error. Would you please be able to help me with this problem? Also, in the first sentence of your explanation you mention that the serial port methods should be in a separate class, not in JFrame, since they are non-GUI methods. I fully agree with you and I would prefer to do it that way since it is the correct object-oriented approach. But doing it that way, I am once again facing the same problem in NetBeans. How do I now pass the object reference from SerialPorts.java class to JFrame, JPanel etc in a way so that I am not creating a new SerialPorts object all the time, remember I need the connection to stay open all the time (ex. without using SerialPorts sp = new SerialPorts();). Thank you so so so much for your help.

Adding "this" in code customizer for Panel 1 Adding "this" in code customizer for Panel 1

MyJFrame.java - editor underlines reference "this" as error MyJFrame.java - editor underlines reference "this" as error

like image 682
jadrijan Avatar asked Dec 12 '22 04:12

jadrijan


1 Answers

I wonder if you should have the port names held by a non-GUI model class and obtained from it rather than from the "JFrame", but regardless your question is a specific example of a more of a general question: how do you pass information from one GUI object to another, and that can be generalized further to: how do you pass information from one object to another.

The simple answer is to have one object call a method on the other, which is what you've tried, and which is what should work, but the problem with your attempt is that you're calling it on the wrong object. You have a JFrame object that has already been constructed, that is visible, and that holds the information you need, and that is the object which you should be calling your method on, not a new JFrame object. Sure the new object is built from the same class, but understand that it is a completely different object from the one that's displayed which is the one you want.

So now your problem boils down to this -- how do I get a reference to the visualized JFrame into another object? There are many ways to do this, but the easiest way to do this is to probably pass a reference from one class to the other via a constructor or method parameter.

...

So for instance, say you've got a class called FooFrame that extends JFrame, and it holds a FooPanel object that extends JPanel, and suppose you want the FooPanel object to call the someMethod() method of FooFrame:

public class FooFrame extends JFrame {

   public String someMethod() {
      return myTextField .getText();
   }
}

You could have FooPanel create a new FooFrame object and call this method as you're trying to do:

class FooPanel extends JPanel {

   public void otherMethod() {
      FooFrame fooFrame = new FooFrame();
      fooFrame.someMethod();
   }
}

But then you run into the same problem you're running into above. The FooFrame object created here is not the one being displayed, and so the contents of the JTextField will be empty and not very helpful. The solution I'm suggesting is that you pass a reference from the original JFrame into the JPanel via a constructor parameter like so:

class FooPanel extends JPanel {
   private FooFrame fooFrame;

   // constructor accepts a reference to a FooFrame object
   public FooPanel(FooFrame fooFrame) {
      this.fooFrame = fooFrame; // and sets its field with it
   }

   public void otherMethod() {
      // no longer needed!
      // FooFrame fooFrame = new FooFrame(); 


      // now method is called on the right object!
      fooFrame.someMethod(); 
   }
}

Then you can create your FooPanel object in FooFrame and pass the FooFrame object (this) into it.

public class FooFrame extends JFrame {

   private JTextField myTextField = new JTextField(10);
   private FooPanel fooPanel;

   public FooFrame() {
      // the current FooFrame object is this!
      fooPanel = new FooPanel(this);
      add(fooPanel);
   }

   public String someMethod() {
      return myTextField .getText();
   }
}
like image 178
Hovercraft Full Of Eels Avatar answered Dec 24 '22 21:12

Hovercraft Full Of Eels