• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HTML is supposed to work in JButton, but?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. :-(

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic