Higeath
Higeath

Reputation: 561

Accessing GUI object from a different method

I am trying to access a variable that is a part of my GUIClass so I have created a new class where I instantiate my static GUI and give it a setter and from a different GUI class I call this setter to get all the variables I need.

Is this the correct approach or is there a better way?

public class Master {

    static NormalDistributionUI ndUI;

    public static NormalDistributionUI getNdUI() {
        return ndUI;
    }

    public static void main(String[] args) {
        ndUI = new NormalDistributionUI();

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ndUI.setVisible(true);
            }
        });
    }

}

One more thing since I have moved main to this class I am not sure what to do now with:

   try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }

or rather where this should be executed, in GUI constructor?

EDIT: Picture showing what I am trying to achieve enter image description here

Upvotes: 0

Views: 77

Answers (2)

FMiscia
FMiscia

Reputation: 223

Make your GUI class singleton. Your code should look like the following at the end. (and of course use the @Hovercraft Full Of Eels suggestions)

 public class NormalDistributionUI { 

         private static NormalDistributionUI _instance = null; 

         /*It's important that the constructor is private*/ 
         private NormalDistributionUI(){ 
                 //constructor 
         } 

         // your class body 


         /* public static method to retrieve your instance */
         public static NormalDistributionUI getInstance(){ 
                 if(_instance == null) 
                         _instance = new NormalDistributionUI(); 
                 return _instance; 
         } 
 } 

 /* your Master class goes like this */
 public class Master { 

         //your class 

         public static void main(String[] args) { 

                 java.awt.EventQueue.invokeLater(new Runnable() { 
                         public void run() { 
                                 NormalDistributionUI.getInstance().setVisible(true); 
                         } 
                 }); 
         } 

 } 

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I would look into using the M-V-C or Model-View-Controller design pattern or one of its many variants for this where

  • Your model contains your data table
  • It also contains information on selected integration type
  • The control will listen to responses made in the Preferences dialog and notify the model of the selections made
  • This will change the state of the model: its integration type would change, and likely its data would be re-calculated based on this integration type.
  • The view listens to changes in the model (in one version of MVC) and then re-displays the new data when notified by the model of data changes.

Upvotes: 2

Related Questions