• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Cannot remove a JLabel from a Button

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning!

I'm using this code:



The above method is called every time a 'unit' moves onto the 'tile' (which is a JButton). The first time, it works like a charm - it adds a label with the count '1' to show that a unit has moved onto it. But when the unit moves OFF the tile it calls this method again to count the number of occupants on the tile and returns the number as zero.

The else block then kicks into action where I'm trying to *remove* that JLabel, since there are now no occupants on the tile.

I've tried many things including simply changing the text of the label:



I've also tried adding


I've confirmed that the count definitely goes to zero - I've also confirmed that the code is reachable but it never seems to execute or do anything

Any help or suggestions?

Thanks in advance.

Stephen.
 
Ranch Foreman
Posts: 306
4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about like this?

 
Marshal
Posts: 64639
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stephen Pillinger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've looked at both those suggestions but neither seem to work.



This doesn't work because the variable 'numberOfOccupantsOnTile' is a JLabel and I get the compilation error that Java cannot convert from String to JLabel



This one doesn't work either because I'm not setting text on the button itself, I'm trying to remove a label that's already on the button but the label refuses to go.

Thanks for the suggestions though, but does anyone have any other ideas?

Thank you.
 
Randy Tong
Ranch Foreman
Posts: 306
4
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the suggestions though, but does anyone have any other ideas?

Maybe post more code here? Is your class extend to JFrame or JLabel?

I have written a sample to show how JLabel get removed in JButton when JButton is clicked.



 
Stephen Pillinger
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply and help, I've got it working now, the posts here were useful!
 
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This one doesn't work either because I'm not setting text on the button itself,


I think that was the point of the answer.

Why are you trying to add/remove a label from the button when you can just set the text of the button?

Why create multiple components that use resources when you don't need to?
 
Stephen Pillinger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair question - I'm using the set text for something else - it's quite a crowded button! The only way to position text elsewhere on the button is to use a separate label to do so - unless you've another way I'm unaware of!
 
Randy Tong
Ranch Foreman
Posts: 306
4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you for your quick reply and help, I've got it working now, the posts here were useful!


I'm curious what made it works? Because I saw you already have the remove code earlier which is not working.
 
Rob Camick
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The only way to position text elsewhere



Not sure what that means.

unless you've another way I'm unaware of!


You haven't posted a requirement.

Because I saw you already have the remove code earlier which is not working


Look at the method again.

It always creates a new instance of the JLabel. So the else logic is trying to remove an instance of the JLabel that hasn't been added to the button.
 
Stephen Pillinger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rob - "The only way to position text elsewhere" - on the button (where else?) - I have more than one text segment appearing on the button - top left, top right, bottom left and bottom right - using the in-built 'set text' method of the button, as far as I'm aware, cannot facilitate this.

"You haven't posted a requirement." - that's right, it's fixed

Randy Tong: "I'm curious what made it works? Because I saw you already have the remove code earlier which is not working." - Rob identified the problem when he said that I was creating a new instance of JLabel each time the method ran.

In the first 'if' block, it would add the JLabel. But the next time the method ran, it would create a new instance of the JLabel but jump to the else block and try to remove the *new* JLabel (which had not been added to the button) rather than the old one (which had - but was no longer referenced by the name 'numberOfOccupantsOnTile').

Anyway, thank you both for your help.
 
Rob Camick
Rancher
Posts: 3051
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have more than one text segment appearing on the button - top left, top right, bottom left and bottom right -



Well, I'm not sure how you get the text to appear in a 4 corners at the same time, since the labels overlay one another.

The text from each label should just be painting over top of the other labels, unless you have some manual code that calculates the size of each label and adjust the total size of the button automatically.

A JButton can support basic HTML, so maybe you can use that?

 
Saloon Keeper
Posts: 3291
146
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephen Pillinger wrote:Fair question - I'm using the set text for something else - it's quite a crowded button! The only way to position text elsewhere on the button is to use a separate label to do so - unless you've another way I'm unaware of!


Well,

That is easy enough. Just set a suitable LayoutManager for your button, and you can add a lot of things to that button. For instance:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!