简体   繁体   中英

How to access Variable in Main class from another class

I'm using my main class as my UI for a program I'm working on, and in an another class I have a loop running until it loses network connection. said field:

lastCrashField = new JLabel("",SwingConstants.CENTER);
    lastCrashField.setBounds(80,180,200,30);
    lastCrashField.setBorder(blackLine);

totalCrashField = new JLabel("",SwingConstants.CENTER);
    totalCrashField.setBounds(80,240,200,30);
    totalCrashField.setBorder(blackLine);

How can I update these 2 fields in the UI from a different class, been looking around for some time now and so far I'm drawing a blank thinking maybe I need to re-think the way I handle these fields from their base...

You said:

How to access Variable in Main class from another class

To directly answer your question, choose one of these approaches:

  • Pass a reference to each of the fields as arguments to a method on an object of the other class.
  • Make instance members of the two JLabel widgets. Mark them non- private for direct access by an object of the other class.

Code for the first bullet.

public class CollisionForm extends … 
{
    // Member fields
    private JLabel lastCrashField, totalCrashField ;

    // In constructor, populate those two field references.
    …

    // Pass references to each of the two fields to your logic method doing some work to produce fresh data.
    void whatever() 
    {
        SomeHelperObject someHelperObject = new SomeHelperObject() ;
        someHelperObject.updateCrashFields( lastCrashField , totalCrashField ) ;
    }
}

public class SomeHelperClass
{
    // Passing two references to JLabel objects.
    void someLogic( JLabel lastCrash , JLabel totalCrash ) 
    {
        lastCrash.setText( … ) ;
        totalCrash.setText( … ) ;
    }
}

Code for the second bullet.

public class CollisionForm extends … 
{
    // Member fields
    public JLabel lastCrashField, totalCrashField ;  // Public, not private.

    // In constructor, populate those two field references.
    …

    void whatever() 
    {
        SomeHelperObject someHelperObject = new SomeHelperObject( this ) ;  // Pass instance of Swing form (or controller) to the helper class constructor.
        someHelperObject.updateCrashFields() ;
    }
}

public class SomeHelperClass
{
    // Member fields
    CollisionForm form ;

    // Receiving instance of `CollisionForm` in the constructor of this class.
    SomeHelperClass( CollisionForm collisionForm ) 
    {
        this.form = collisionForm ;
    }

    void someLogic() 
    {
        this.form.lastCrashField.setText( … ) ;
        this.form.totalCrashField.setText( … ) ;
    }
}

But I do not recommend either of these approaches for working with Swing widgets. Read on.

You said:

2 fields in the UI from a different class

Generally you should not have other classes directly manipulating your GUI widgets.

Better to have the object containing/managing the widgets (a GUI layout object, or a controller object) ask other classes to do work. Any result data should be returned to the layout/controller which then updates the widgets.

public class CollisionForm extends … 
{
    // Member fields
    private JLabel lastCrashField, totalCrashField ;  

    // In constructor, populate those two field references.
    …

    void whatever() 
    {
        SomeHelperObject someHelperObject = new SomeHelperObject() ;  // Passing nothing about widgets. This other class remains blissfully ignorant of anything related to Swing.
        List< Integer > freshCrashData = someHelperObject.someLogic() ;
        this.lastCrashField.setText( freshCrashData.get( 0 ) ) ;
        this.totalCrashField.setText( freshCrashData.get( 1 ) ) ;
    }
}

// This version of our `SomeHelperClass` class knows nothing about Swing nor the widgets on our form.
public class SomeHelperClass
{
    List< Integer > someLogic()  // <-- Instead of `void`, return data
    {
        return List.of(
            … ,  // Last crash data.
            …    // Total crash data.
        );       // The calling method on the Swing form uses this data in this returned `List` to update its own widgets. 
    }
}

Tip: In Java 16+ I would use the new record feature to define a class, an instance of which would be returned by this method rather than a clumsy ambiguous List .

You said:

I have a loop running until it loses network connection

This sounds like you are using background threads.

BEWARE: Never access your Swing widgets from another thread. Use the "SwingWorker" classes through which the background thread asks Swing's own UI thread to update its widgets.

Threading with Swing has been covered extensively already on Stack Overflow. So search this site to learn more. And read tutorial on concurrency with Swing by Oracle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM