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.