0

I'm working on a textbased simple roleplaying game for my exame, but i have ran into some problems with my gui.

When the player registers, he can spend some attribute points in 3 categories. The gui is programmed to show the Raise Strength etc. button, if the player have any attribute points.

And that works cool, but then when the player clicks on a raise button, an attributepoint is taken for him, the problem is, that the gui doesn't seem to update.

if(Controller.player.getAttributePoints() > 0) {
        JLabel attriL = new JLabel("You have " + Controller.player.getAttributePoints() + " unspent Attribute points."); 
        attriL.setBounds(110, 30, 250, 30);
        hPanel.add(attriL);

        JButton setStrB = new JButton("Raise Strength");
        setStrB.setBounds(125, 60, 200, 30);
        setStrB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
             //   tabbedPane.removeAll();                    
                Controller.player.setAttributePoints(Controller.player.getAttributePoints()-1);
                Controller.player.setStrength(Controller.player.getStrength()+1);

                gameCtn.validate();
                gameCtn.repaint();
                System.out.println(Controller.player.getStrength());                    
            }
        });
        hPanel.add(setStrB);
}

As you can see i have tried using repaint and validate on my container but with no luck, also i have tried on the Frame, and the panel, nothing seems to work? Am i doing something wrong?

Thx

MartinElvar
  • 5,695
  • 6
  • 39
  • 56

2 Answers2

2

not clear if Controller... didn't invoked long timed and hard taks, basically (if you remove and then add new JComponents) to the GUI then you have to call

revalidate();
repaint();// not required on all cases

simple demonstrations what's happens, what's possible or most completed here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Sorry about that.

You have no code that says to make the button invisible.

public void actionPerformed(ActionEvent evt) {
    ...
    if (whateverIsLeft < 1) {
        JButton src = (JButton)evt.getSource();
        src.setVisible(false);
    }

    attriL.setText("You have " + whateverIsLeft + " attribute points left");
}
black panda
  • 2,842
  • 1
  • 20
  • 28