Java pass object from JFrame to JPanel

I am developing a standalone Java application using NetBeans that receives and sends data through a serial port. I use a great serial communication API called java simple serial connector http://code.google.com/p/java-simple-serial-connector/

Download the following NetBeans project that I created: http://netload.in/dateiN9xmwRtn19.htm Even if you don't have netbeans, you can still view the code.

The above project is a small example that I did to explain what I want 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 JFrame, I have a method that gets a list of port names using the getPortNames () method of the jssc API. My question is: how do I pass these String port names from a JFrame to Panel 1 without using the following, as this will not work: MyJFrame myjframe = new MyJFrame (); myjframe.getPortNames ();

Hovercraft Full Eels Thanks again for your explanation, it costs gold. Now I understand that my problem is NetBeans, not Java. I am not familiar enough with this, but I would prefer to use it because of the size and complexity of my project. I uploaded 2 more screenshots below that show how I tried to pass the "this" link of the JFrame to my JPanel in NetBeans. As you can see in the screenshot of MyJFrame.java, NetBeans underlines the "this" link as an error. Could you help me with this problem? Also, in the first sentence of your explanation, you mentioned that serial port methods should be in a separate class, not in a JFrame, since they are not GUI methods. I completely agree with you, and I would prefer to do it this way, because this is the right object-oriented approach. But doing it this way, I am again facing the same problem in NetBeans. How can I now pass a reference to an object from the SerialPorts.java class in JFrame, JPanel, etc. So that I do not create a new SerialPorts object all the time, remember that I need to keep the connection open all the time (for example, without using SerialPorts sp = new SerialPorts ();). Thank you so much for your help.

Adding "this" to the code customizer for panel 1 Adding "this" in code customizer for Panel 1

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

+4
source share
1 answer

I wonder if you should have port names belonging to a non-GUI model class and derived from it, and not from a "JFrame", but no matter what your question is, a concrete example of a more general question: how do you pass information from a single GUI object to another, and this can be summarized further: how do you transfer information from one object to another.

The simple answer is that one object calls the method on another, this is what you tried and what exactly should work, but the problem with your attempt is that you are calling it on the wrong object, You have a JFrame object, which has already been created, which is visible, and which contains the necessary information, and it is an object that you should call your method on, and not a new JFrame object. Of course, the new object is built from the same class, but understand that it is a completely different object from the one that is displayed, which you need.

So now your problem comes down to this - how do I get a link to a rendered JFrame on another object? There are many ways to do this, but the easiest way to do this is to probably pass the link from one class to another through a constructor or method parameter.

...

So, for example, let's say you have a class called FooFrame that extends JFrame, and it contains 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 can create a FooPanel new FooFrame object and call this method, as you are trying to do:

 class FooPanel extends JPanel { public void otherMethod() { FooFrame fooFrame = new FooFrame(); fooFrame.someMethod(); } } 

But then you will encounter the same problem that you encounter above. The FooFrame created here is not displayable, so the contents of the JTextField will be empty and not very useful. The solution I propose is that you pass the link from the original JFrame to the JPanel using a constructor parameter, for example:

 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 a FooFrame and pass the FooFrame object ( this ) to 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(); } } 
+12
source

Source: https://habr.com/ru/post/1398725/


All Articles