This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello i have a problem at extending a JLabel. What i want to do is to extend a jlabel to look like a 3d component. That is why i extend the jlabel and overload the method paintComponent().
To get into the problem i will list the code so someone can help. Thank you in advance.
Here is the code:
Joined: Aug 01, 2011
The problem with this code is that, it paints well when it shows for the first time. But, in case you resize the label (in this case the jframe in which it is contained, when program runs) it does not paint well. In this case i paint half of the label with one graphics2d and the other half with an other graphics2d, this way i gave the label a 3d look. When you reseize the label, it changes the colors. For example at tha last line of main method i tell to jlabel to paint a itself as a horizontal cylinder where the white color must shows in top and bottom of it and the blue color in center (i do this using a gradient panel). When you resize it, it interchanges these colors but keeps it 3d look. To be able to see what real problem is you must run the application and whatch the effect it does create.
I havent gone through the code carefully, or tried it myself (too early in the morning for me, and I haven't had my coffee yet), but try making super.paintComponent(grphcs); the first line of your paintComponent()
Thank you for your quick reply. I tried it, and when I do this the only changes it makes is that label's text doesn't show at all. I think that is right, because after calling super.paintComponent() which paints the text, the label's region is painted again so that is why text is gone.
i paint half of the label with one graphics2d and the other half with an other graphics2d
And you need to dispose() all Graphics objects that you create.
The code looks horribly convoluted, with multiple and chained calls to Color#darker() / brighter() at every repaint. Those colors should be cached, and if there is a possibility of the background being changed during the run of the program, register a PropertyChangeListener for the "background" property and renew the cache when that changes. The Paints could also be cached and renewed via a ComponentListener's componentResized(...)
edit: Forgot to add, that a custom appearance should be managed by a custom UI delegate -- a LabelUI extending BasicLabelUI or (for massive customization) LabelUI -- not by extending the component class.
There are no new questions, but there may be new answers.
Joined: Aug 01, 2011
Actually the line if(grp == null) is only called for the first time when the user does not provide any color. So there is not a problem there. Yes there is a posibility to do this job with only one lineargradientpanel and i did not know it. I just saw some examples on internet, so i am going to revise the code and to figure out how to avoid the problem. However, the use of two gradientpaint does not explain why this problem occurs. You can see what is the problem if you run the code. Thank you for your reply.