• Post Reply Bookmark Topic Watch Topic
  • New Topic

HTML is supposed to work in JButton, but?  RSS feed

 
Matt Hoover
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just spent 2 hours trying to figure out why my layout wasn't working, and it all came down to HTML tags. The weirdest part is they WERE working for a while.

It shows here that it should work: http://download.oracle.com/javase/tutorial/uiswing/components/html.html

However, try this code below and you will see no buttons. Remove the html tags on button2 and voila, both buttons appear again. What is going on? Like I said, for a while I had a functional HTML button, but then it started disappearing. :-(

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like it's something to do with the repainting. If you resize the window (after getting rid of the line that prevents it!) the buttons appear immediately. So maybe the HTML tags change the way the repainting is carried out.

There is one dead-easy fix, though. Move the statement myFrame.setVisible(true) to the end.

(Technically, you should probably be doing all this in the Event Dispatch thread)
 
Matt Hoover
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Looks like it's something to do with the repainting. If you resize the window (after getting rid of the line that prevents it!) the buttons appear immediately. So maybe the HTML tags change the way the repainting is carried out.

There is one dead-easy fix, though. Move the statement myFrame.setVisible(true) to the end.

(Technically, you should probably be doing all this in the Event Dispatch thread)

Weird, I had actually already tried calling a myFrame.repaint() to see if that fixed it. And in my small demo posted here, it looks like frame repaint DOES fix it. But in my larger more complex program, it does NOT! However, doing like you said and setting visible at the end does seem to work in my complex program, but this whole thing is so screwy it has just turned me off to HTML in the buttons I think, I don't trust it now.

Also, your post is the first mention I've ever heard of an "Event Dispatch thread." I guess I will have to look into that.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Concurrency in Swing about the EDT.

As for your other problem, when you add or remove components after the frame is already made visible, you'll need to revalidate() and repaint() the container you added to / removed from.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob knows far more about this that me, but if you put the lineat then end of your method, the buttons will show.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!