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

  • Home
  • SEARCH
  • 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 965207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:57:32+00:00 2026-05-16T01:57:32+00:00

First I would like to say that I have only worked with java for

  • 0

First I would like to say that I have only worked with java for 1 month now. This is probly a pretty simple question. I searched and the classic fruit example did not make any sense to me. I spent hours looking at it and trying to figure out how to apply this to make it work. It does not make any sense to me because everyone explains how to get and set a property with 2 lines of code and no structure statements. I would really appreciate a breakdown in how to talk to non-static from static.

I would like to setText in text box in my OBD2nerForm class from a separate and static class.

public class OBD2nerForm extends java.awt.Frame {

/** Creates new form OBD2nerForm */
public OBD2nerForm() {
     initComponents();
}   ....................................


public String setText(String text){
    this.jFormattedTextField1.setText(text);
}

I think I have a static reference to this instance of the form defined here..

public class Status {
    public static OBD2nerForm form = new OBD2nerForm();

it is called from my main like this

public class obd2ner {
 public static void main(String[] args) throws IOException {
                        Status.form.main(args);

Then when I try to call it.. Status.form.getText gives me the initial values when the form is created. When I setText, it does not change the one on the screen.

I am just displaying this to keep it simple. There are many other parts going on. I have a static monitor on a serial port and I want it to grab the next data to be sent from the text box and then increment it.

I just don’t understand how to use a getter and a setter on a non-static. It’s not quite doing what I need it to do. It seems like I am seeing one instance on my screen and it is using a new instance to perform the getting and setting.

I tried this as per an answer I received, but it did not work…

public class OBD2nerForm extends java.awt.Frame {
      String X = "";

//Trying out the runnable method of incrementing the code
public String getNewScannerValueRunnable(){

    Runnable doWorkRunnable = new Runnable() {

        @Override
        public void run() { 
           Status.form.getNewRotatingValue()
        ;}

    };
        SwingUtilities.invokeAndWait(doWorkRunnable);

    return X;
}

I could really use some other suggestions. i just don’t understand what has to happen here.

  • 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-16T01:57:32+00:00Added an answer on May 16, 2026 at 1:57 am

    Your form is being created fine, and there’s just one reference to it, and it’s ending up in that static variable. All is well up to that point.

    There’s a ‘secret’ of Swing you need to be aware of: You cannot (visibly) change the properties of GUI objects from any thread other than the Swing thread, aka the Event Dispatching Thread.

    The trick to doing it anyway is to pass the property-changing code as a Runnable to either of SwingUtilities.invokeAndWait() or SwingUtilities.invokeLater().


    EDIT:

    OK, let’s back up. Your form is AWT based, not Swing based, so I’m afraid my advice on using SwingUtilities would probably not have helped you, even if you had implemented it correctly. I’ll try to give more specific hints this time.

    You’ve created a class OBD2nerForm that’s an AWT form. That class has a constructor which calls initComponents to set up some GUI components on the screen.

    The class also has a method called setText that will put its argument text into one of the fields on the form. That method is an instance method, i.e. it’s not “static”, as you’d call it.

    You have another class, Status with a class field form. The initializer for form calls the constructor for OBD2nerForm. That will create an instance of the form and store it in form; but I haven’t seen a show() or setVisible() call being made to the form to actually display it.

    Here are the first signs of trouble:

     public class obd2ner {
     public static void main(String[] args) throws IOException {
                            Status.form.main(args);
    

    Class names (like obd2ner) should start with capital letters; but that’s a matter of style and convention, it’s not what’s causing you problems. Following the conventions helps other people read and debug your code, though.

    The bigger problem is obd2ner.main() calling your form’s main(). That could be made to work, but it’s usually a sign that you’re doing something wrong.

    While nothing stops you from coding static main methods into as many of your classes as you want, only one of those main’s can be started from the outside, i.e. when you run your application. The first main is essentially the ‘boss’ method for your program.

    The “first main” usually instantiates and initializes a few objects. In a non-GUI application, main() may then start up a loop or some other control structure, wherein it will then orchestrate the actions of the other objects. In a GUI application, main() will usually just instantiate and then show the GUI, and then end; once the GUI is visible, all further program activity is triggered by actions the user performs on the GUI.

    Without seeing your code, I’m guessing that Obd2nerForm.main() also instantiates Obd2nerForm, and shows it. So you probably indeed have one instantiated but invisible form hanging off Status.form and another one instantiated, visible and referenced from some variable in Obd2nerForm. If you want to influence that GUI, you need to make a reference to that form accessible.

    Probably the simplest would be:

    In Obd2nerForm, declare a public static Obd2nerForm form, and in Obd2nerForm.main, right after you call the constructor, copy the reference to the form into that variable. From then on, you can access the form and its methods using e.g. Obd2nerForm.form.setText().

    A reminder, though: You seem to have two main()s, and this needs fixing. All the stuff that should be done at the beginning of the app’s lifetime needs to be in one of those mains, not several.

    Finally, look at this method call:

    Status.form.main(args);
    

    That’s the syntax for calling a method on a particular instance. But Obd2nerForm.main is a class method (what you call “static”), and so there isn’t “a particular one” to call, it’s always just the one that belongs to the class itself. That’s why the syntax to call a class method is something like

    Obd2nerForm.main(args);
    

    The compiler lets you get away with the way you wrote it, but it’s not how it’s usually done, and indicates some confusion.

    There… I hope that gets you a little further along. If you still have problems, please post a more complete code sample to PasteBin and I’ll take a look!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First I would like to say that I am new to this site, never
First off I would like to say that this is my first post and
First of all, I would like to say that I have used the search
The first thing i would like to say is that this is not exactly
First off I would like to say that I have done ample research on
Say that I have simple command line application, that looks something like this #
First off I would like to say that I think that my question may
Let's say that I have string: -dog--cat--d-- I would like to find all words
first of all i would like to say i know its probably an easy
Please take a look at this screenshot first: I would like to move the

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.