Reputation: 561
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
Upvotes: 0
Views: 77
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
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
Upvotes: 2