Discussion:
Semi-transparent JTextField?
(too old to reply)
Allysia
2005-01-21 21:45:15 UTC
Permalink
Is there any easy way to make a JTextField semi-transparent (maybe 50%
alpha)?

I tried setting the background color to "new Color(255,255,255,70)", but
the JTextField's background pickedup all sorts of weird things, and became
less transparent with each keystroke.

Do I have to create an extension of JTextField and overload the paint()
function?

-Ally
Steve W. Jackson
2005-01-21 22:13:11 UTC
Permalink
:Is there any easy way to make a JTextField semi-transparent (maybe 50%
:alpha)?
:I tried setting the background color to "new Color(255,255,255,70)", but
:the JTextField's background pickedup all sorts of weird things, and became
:less transparent with each keystroke.
:Do I have to create an extension of JTextField and overload the paint()
:function?
:-Ally
That's method, not function -- let's be OO purists. :-)

Actually, you should not generally override paint() in a Swing
component. You might be interested in a good page on the topic at
<http://java.sun.com/products/jfc/tsc/articles/painting/index.html>. In
fact, it might even offer clues to the actual source of your trouble.

Without detailed code samples and other things, I can't do much more
than speculate, but it may be that opacity is involved in this as well.
Once you read that article, you'll understand what paint() actually does
in Swing -- which is to paint the component itself, then its border,
then its children. Swing components, as subclasses of JComponent, all
have the setOpaque(boolean) method. This lets you create a contract
between the RepaintManager and your Swing component indicating whether
or not you'll draw the entire background when painted. Judicious use of
this call, and a good understanding of what happens when it's true or
false, could help to get you where you want to be.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
Andrey Kuznetsov
2005-01-22 02:25:35 UTC
Permalink
Post by Steve W. Jackson
:I tried setting the background color to "new Color(255,255,255,70)", but
:the JTextField's background pickedup all sorts of weird things, and became
:less transparent with each keystroke.
:Do I have to create an extension of JTextField and overload the paint()
:function?
:-Ally
That's method, not function -- let's be OO purists. :-)
Actually, you should not generally override paint() in a Swing
component. You might be interested in a good page on the topic at
<http://java.sun.com/products/jfc/tsc/articles/painting/index.html>. In
fact, it might even offer clues to the actual source of your trouble.
Without detailed code samples and other things, I can't do much more
than speculate, but it may be that opacity is involved in this as well.
Once you read that article, you'll understand what paint() actually does
in Swing -- which is to paint the component itself, then its border,
then its children. Swing components, as subclasses of JComponent, all
have the setOpaque(boolean) method. This lets you create a contract
between the RepaintManager and your Swing component indicating whether
or not you'll draw the entire background when painted. Judicious use of
this call, and a good understanding of what happens when it's true or
false, could help to get you where you want to be.
You should also be aware, that caret of JTextField is repainted
with paintImmediately(x, y, w, h) what could lead you
to additional problems.
--
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Loading...