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;
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() {