0

I am currently developing MineSweeper in java.In that I have to update JTextFields(as shown in Fig.) When I call setText(), Logger shows it is properly called. But in output there is no response.

enter image description here

My Actual code is as Below...

class GameInfoDisplayer{
          ...............
void setGameStatusText(String s){
    statusDisplayer.setStatus(s);
}
          ..............
 <....Definition of TimeController Inner class Comes here....>
    ..............
private class StatusDisplayer{
    JTextField displayField;
    Logger logger = Logger.getLogger("StatusDisplayer");
    StatusDisplayer(){
        displayField = new JTextField();
        gameInfoFieldsContainer.add(displayField);
    }
    void setStatus(String s){
            gameInfoFieldsContainer.remove(displayField);
            displayField.setText(s);        
            gameInfoFieldsContainer.add(displayField);
            gameInfoFieldsContainer.revalidate();
            gameInfoFieldsContainer.repaint();  
            logger.info("I am in updating Display content...:"+displayField.getText());
        }
    }

I did all repainting, revalidating as given in similar question but there is no use..

Community
  • 1
  • 1
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 01 '11 at 10:45
  • 3
    It is difficult to tell the problem with the info available. Many Swing problems with 'strange' behavior occur because updating is not done on the event dispatch thread; make sure you are using invokeAndWait or invokeLater as needed. – arcy Dec 01 '11 at 11:24
  • Are you calling `new StatusDisplayer()` or something of the sort before calling `setStatus()`? The textfield isn't actually defined until you call `new StatusDisplayer()`, so calling `setStatus()` first causes a NullPointerException. – fireshadow52 Dec 01 '11 at 11:37
  • @fireshadow52 I call new StatusDisplayer() in the constructor of GameInfoDisplayer. (That was not shown here). – Muthu Ganapathy Nathan Dec 01 '11 at 11:40
  • @EAGER_STUDENT *Before* calling `setGameStatusText()` I'm assuming? – fireshadow52 Dec 01 '11 at 11:42
  • 3
    Why would you remove and then add back the same component? All you need to do is invoke the setText() method (assuming you have a reference to the text field that is displayed on the GUI). – camickr Dec 01 '11 at 15:54

1 Answers1

1

Try the following

private class StatusDisplayer{
    JTextField displayField;
    Logger logger = Logger.getLogger("StatusDisplayer");
    StatusDisplayer(){
        displayField = new JTextField();
        gameInfoFieldsContainer.add(displayField);
    }
    void setStatus(String s){
            displayField.setText(s);        
            logger.info("I am in updating Display content...:"+displayField.getText());
        }
    }
}
GETah
  • 20,922
  • 7
  • 61
  • 103