Search code examples
javaswingjframejpaneljbutton

How to create an update/refresh method that reads the newly loaded object instances and applies it to the GUI?


I want to create an update/refresh method for my game that will read the data on the puzzle and stack objects and update the GUI to match the state when the game is loaded from the previous save. I have created a save and load method which works fine, however I am unsure how to get the input to display on the GUI using a update/refresh method.

public class PuzzleGUI extends JFrame implements Observer{

private Puzzle game; 
private JPanel mainGrid; 
private Stack<UserEntry> stack = null;

  private void init() { 
    game = new Puzzle(); 
    game.addObserver(this);
    stack = new Stack<UserEntry>();  

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(360, 600); 
    this.setResizable(false); 

    Container pane = this.getContentPane();
    pane.setLayout(new BorderLayout()); 

    mainGrid = new JPanel();
    mainGrid.setLayout(new FlowLayout());
    pane.add("Center", mainGrid);

    JButton load = new JButton("Load");
    mainGrid.add(load);           
    load.addActionListener(new ActionListener() 
    {
      @Override
      public void actionPerformed(ActionEvent ae){
        load();     
      }
    });

    setupGrid();
    setVisible(true); 
  }

  public void setupGrid() {
    cells = new PanelCell[5+1][5+1]; 
    relCellsRow = new RelCellRow[5+1][5+1][5+1][5+1];
    relCellsCol = new RelCellCol[5+1][5+1][5+1][5+1];
    greyBtns = new GreyButtons(); 
      
    for(int col=1; col<6; col++){
      JPanel panelx = new JPanel();   
      mainGrid.add(panelx);  
      JPanel cpanelx = new JPanel(); 
    for(int row=1; row<6; row++){ 
      cells[col][row]= new PanelCell(this,col,row);
      panelx.add(cells[col][row]);
        if(row<5){
              relCellsRow[col][row][col][row+1]= new RelCellRow(this,col,row,col,row+1);
              panelx.add(relCellsRow[col][row][col][row+1]);  
        }
        if(col<5){
          if(row<6){
              relCellsCol[col][row][col+1][row]= new RelCellCol(this,col,row,col+1,row);
              cpanelx.add(relCellsCol[col][row][col+1][row]);
              if(row<5){
                greyBtns= new GreyButtons();
                cpanelx.add(greyBtns); 
              }
          }
       }
    } 
       mainGrid.add(panelx);
       mainGrid.add(cpanelx);
    }
  }

private void save() {
    try {
    PrintStream ps = new PrintStream(new File(file));
    for (UserEntry ue : stack) {
        ps.println(ue.toStringForFile());
    }

    ps.close();
    JOptionPane.showConfirmDialog(null, 
        "Game saved successfully.", 
        "Puzzle",   
        JOptionPane.DEFAULT_OPTION);   
    } catch (IOException e) {
    JOptionPane.showConfirmDialog(null, 
        e.toString() + "\nFailed to save game.",   
        "Puzzle", 
        JOptionPane.DEFAULT_OPTION); 
    }    
}

private void load(){
    try {
    Scanner fscnr = new Scanner(new File(file));
    clear();
    while (fscnr.hasNextInt()) {
        UserEntry a = new Assign(fscnr);
        UserEntry re = new RelEntry(fscnr); 
        game.assign(a.getRow(),a.getCol(),a.getNum());
        game.addRelation(re.getGreaterRow(), re.getGreaterCol(), re.getLesserRow(), re.getLesserCol());   
        stack.push(a);
        stack.push(re); 
    }
    fscnr.close();
    JOptionPane.showConfirmDialog(null, 
        "Game loaded successfully.", 
        "Puzzle",   
        JOptionPane.DEFAULT_OPTION); 
    } catch (IOException e) {
    JOptionPane.showConfirmDialog(null, 
        e.toString() + "\nFailed to load game.",   
        "Puzzle", 
        JOptionPane.DEFAULT_OPTION); 
    } 
}

Solution

  • Found a solution by creating a new method called reloadGame which checked to see if any input was assigned in the game. Which was then declared in the load method.

    private void reloadGame(){  
      game.addObserver(this); 
        for(int col=1; col<6; col++){
          for(int row=1; row<6; row++){ 
            if(game.isAssigned(col,row)){
              cells[col][row].setText(Integer.toString(game.getNum(col,row)));             
            }else{
              cells[col][row].setText("");
            }
            if(row<5){
              if (game.containsRelEntry(col,row,col,row+1)){
                relCellsRow[col][row][col][row].setText("\u25B6");
                relCellsRow[col][row][col][row].setForeground(Color.DARK_GRAY);
            }else if(game.containsRelEntry(col,row+1,col,row)){
                relCellsRow[col][row][col][row].setText("\u25C0"); 
                relCellsRow[col][row][col][row].setForeground(Color.DARK_GRAY); 
            }else{
                relCellsRow[col][row][col][row].setText("");
            }
          }
          if(col<5){
            if(row<6){
              if (game.containsRelEntry(col+1,row,col,row)){
                relCellsCol[col][row][col][row].setText("\u25B2");
                relCellsCol[col][row][col][row].setForeground(Color.DARK_GRAY);
              }
              else if(game.containsRelEntry(col,row,col+1,row)){
                relCellsCol[col][row][col][row].setText("\u25BC"); 
                relCellsCol[col][row][col][row].setForeground(Color.DARK_GRAY);
              }else{
                relCellsCol[col][row][col][row].setText("");
              }
            }
          }
        }     
      }
    }