• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't get an ActionListener to change a JLabel text

 
Nikka Groenquist
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all I am new in here. So in advance I am sorry if this is a really noobish question. But I hope you can help me anyways
I am trying to write an application where if you click and hold down on 1 button and move the mouse to another button and release the text on the 2 buttons should be switched around.
I have a print line so I can se the text are supposed to get switched around but it is just not happening in the GUI.

I attached all my code in case the flaw was to find somewhere else in the code, but the functionality I am working on is the lines 76 to 121.



Thanks in advance.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When you want to add/remove components from an UI which is already being displayed, always invoke revalidate() and repaint() on the parent container
 
Nikka Groenquist
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to change it to

also tried

and

None of them did the trick.
But ty for the really fast reply
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That looks dreadfully complicated. All you need is an instruction to change the text in the actionPerformed method.You can probably shorten that in Java8 like thisYou should never need validate or revalidate because you should add all your components to the top‑level container before you set it visible. Don't go changing components while the display is in use. If you need to change the display, find out about Card Layout.
 
Nikka Groenquist
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't use action listener since it need to get the text from 1 button when I press it and then the text from another button when I release the mouse. Thereby a mouselistener is needed.
I am trying to get the application to switch around the text on the two buttons.

If you read my code and initial comment I explained this and you would see that I am using setText.

But otherwise thanks for the reply
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case add a MouseAdapter with the pressed and released methods overridden to the button. Don't mess about with addXYZListener(this).Don't forget the @Override annotation. If myTextComponent is not a field, it must be marked final.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!