-1

I'm trying to update my GUI textArea with player names but can't seem to call any methods/variables in the GUI class instance. The AddPlayerButtonAction constructor adds links to the other classes but only gameEngine link works.

  public AddPlayerButtonAction(JFrame gameFrame, GameEngineImpl gameEngine) {
  this.gameFrame = gameFrame;
  this.gameEngine = gameEngine;
}

public void actionPerformed(ActionEvent event) {

  ................does stuff....................

  gameEngine.  //Eclipse shows all gameEngine methods I can use
  gameFrame. //only shows JFrame methods, no GUI class methods/variables

}

This is the code for my GUI class, made all variable available class wide and public

 public class GUI {
 public JTextArea display = new JTextArea(5, 40);
 final GameEngineImpl gameEngine = new GameEngineImpl();
 public JFrame gameFrame = new JFrame("");

public GUI() {
  addPlayerPanel(gameFrame);
  addButtons(gameEngine, gameFrame);
  //Passed gameFrame and gameEngine
}

And the PlayerPanel method in my GUI class

public void addPlayerPanel(JFrame gameFrame) {
 gameFrame.add(this.playerPanel);
  display.setEditable(true); // set textArea to editable
  JScrollPane scroll = new JScrollPane(display);
  playerPanel.add(scroll);
 }
  • I'm not quite clear what you're asking or what your code is doing, but one issue I see is that you're using a **lot** of method- and constructor-local variables, and when you do that, this makes it hard to gain a handle or reference to key objects. Why are you avoiding using instance fields for your key objects? – Hovercraft Full Of Eels May 22 '16 at 17:45
  • For instance, your JTextArea, display, is a key object, and yet you do not assign it to an instance field that's visible class-wide. How will you get a reference to it later when you need to use it? – Hovercraft Full Of Eels May 22 '16 at 17:47
  • And just what do you mean by "call-backs"? Also, for best help, create and post a [mcve]. – Hovercraft Full Of Eels May 22 '16 at 17:49
  • I want to append the textArea. Something like "gameFrame.playerPanel.display.append("test3\n"); " I write "gameFrame." and Eclipse gives me a huge list of methods that appears to be for a JFrame object. But I can't access any methods or variables in my GUI class. I've made the GUI variables available class wide. thanks for that. – user3162260 May 22 '16 at 18:15
  • Your whole program structure appears to be skunked and you probably should start over. Key objects should be assigned to instance fields to allow you to gain access to them when and where they are needed. – Hovercraft Full Of Eels May 22 '16 at 18:17
  • Question: why are you adding JPanels and components within your ActionListener? Wouldn't you instead be simply adding text to components that are already displayed? You might want to edit your question and add more relevant information and code so that we better understand your code and your problems. – Hovercraft Full Of Eels May 22 '16 at 18:37
  • by callback I mean update the GUI class with new info, I've declared the variables at the start of the GUI class and made everything public, but I still can't access any gameFrame methods. – user3162260 May 22 '16 at 19:01
  • adding the JPanel was just a test to see if it added it to the GUI gameFrame main display, but it didn't work – user3162260 May 22 '16 at 19:03
  • 1
    You're going to need to create and post a valid [mcve] to get a complete answer. – Hovercraft Full Of Eels May 22 '16 at 19:32
  • 1
    I noticed your update, and it doesn't help us solve the issue. As per my answer, you *shouldn't* make any fields public, but that's not the cause of your problem. *Again*, if you want us to be able to help you in a deep way, you should strongly consider creating a valid [mcve]. This is a a small (focus on the *small* part) but *complete* program that has only code necessary to demonstrate your problem and to compile and run, that we can copy, paste, compile and run without modification. – Hovercraft Full Of Eels May 22 '16 at 19:40
  • Also see edit regarding `"gameFrame. //only shows JFrame methods, no GUI class methods/variables"` – Hovercraft Full Of Eels May 22 '16 at 19:43

1 Answers1

3

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for the help @Hovercraft Full Of Eels. I've found more info here https://stackoverflow.com/questions/22833328/access-gui-components-from-another-class – user3162260 May 22 '16 at 20:12
  • @user3162260: you're welcome, more info coming in this answer that I'm working on. Please also work on modifying your question so we can give more specific info for your situation. Your question is still very incomplete. – Hovercraft Full Of Eels May 22 '16 at 20:14
  • @user3162260: some good answers in that question, many of the points are what I'm trying to relay to you above. – Hovercraft Full Of Eels May 22 '16 at 20:26