Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JLabel text not updated after ActionEvent

 
Andreas Johnsson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Java and I have a very frustrating problem.

We have a college assignment and the task is to create a "Rock, paper, scissor" game with five different classes.
The classes are: SSPMain, SSPPlayer, SSPViewer, SSPController and SSPUserInput.

The problem I have is that the text in the JLabel "humanSelectionLabel" (SSPViewer) is not changed to "Rock" after I have clicked on the JButton "rockButton" (SSPUserInput).
I have added a System.out.println in the method which receives the selection-String and it prints "Rock", so the method receives what it should but the setText instruction does not work.

SSPMain


SSPPlayer



SSPViewer



SSPController



SSPUserInput
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

After you have updated the label object, you must update the display, which is probably most easily done by calling the repaint() method on the frame.
I do not, I am afraid, like the design of your listener class. If the different buttons do different things, they need a class each. Don't use get/source() == Use a different class (or a different λ) for each button.
 
Andreas Johnsson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome

I added repaint(); at line 42 in SSPViewer but it does not update the JLabel text

Do you have any other ideas?
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am afraid there is something wrong with your design. You have two frames, which you shou‍ld not do. The viewer object you are adding to the second frame is a different one from that used by the controller, so there is no connection between the two. I think you are going to have to rethink your design completely.
Avoid subclassing display classes. Add them all in the constructor. Rather than having a display class which extends JPanel, have a display class which has a panel as a variable and add all the labels to it. Then you can later add the panel to the frame.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic