You're burying key objects by only assigning them to local variables, variables that only exist within methods or constructors, and thus are only visible within the method or constructor of declaration, and this is preventing you from gaining access to these objects when and where needed. I don't recommend that you expose everything to the outside world, but you do need to give your class private instance fields for the key objects that outside objects need access to. And then you should grant access but in a limited fashion and by using public methods, not public fields.
For instance, you have this code here:
void addPlayerPanel(JFrame gameFrame) {
JPanel playerPanel = new JPanel();
gameFrame.add(playerPanel, BorderLayout.CENTER);
playerPanel.setBorder(new TitledBorder(new EtchedBorder(), "Registered players"));
// display is a local variable, and only visible in this method
JTextArea display = new JTextArea(5, 40);
display.setEditable(true); // set textArea to editable
JScrollPane scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
playerPanel.add(scroll);
display.append("test3\n");
}
Here you create a JTextArea and add it to a JFrame, all well and good, but the variable that you assign the JTextArea to, display, is declared within this method, only exists within this method and is thus only visible within this method. If your program needs access to the JTextArea in any way elsewhere, you're going to have a difficult time getting a handle on the reference. Instead do something like:
class MyGui {
// **** private instance field for a key object of your class ****
private JTextArea display;
void addPlayerPanel(JFrame gameFrame) {
JPanel playerPanel = new JPanel();
gameFrame.add(playerPanel, BorderLayout.CENTER);
playerPanel.setBorder(new TitledBorder(new EtchedBorder(), "Registered players"));
// *** note the change? ***
// JTextArea display = new JTextArea(5, 40);
display = new JTextArea(5, 40);
display.setEditable(true); // set textArea to editable
JScrollPane scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
playerPanel.add(scroll);
display.append("test3\n");
}
// The two methods below allow limited access to your private display field
public String getDisplayText() {
return display.getText();
}
public void appendToDisplay(String text) {
display.append(text + "\n");
}
}
Note regarding:
gameFrame. //only shows JFrame methods, no GUI class methods/variables
And this is because gameFrame is a JFrame variable not a MyGUI or GUI variable. Java (not Eclipse) will only allow you to call the method that a variable type owns. Object type isn't what matters for this, rather variable type does.