Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7562039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:18:51+00:00 2026-05-30T13:18:51+00:00

I am developing a standalone application, in Java using NetBeans, that gets and sends

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:18:52+00:00Added an answer on May 30, 2026 at 1:18 pm

    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();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a standalone application in Java using swing API. I need experts
Hai I am developing a standalone application using Java in which I am using
We are developing a standalone application that stores its data in a Database. We
I'm developing a standalone application that accesses a common database on a server. It
I'm developing an application that is so far using HttpListener to provide a small
i am developing a standalone application using html and want to call method residing
I'm developing an application that which will work as a standalone website and will
I have been working with databases recently and before that I was developing standalone
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
I am developing an application on the Google App Engine using Python (and Django,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.