Re-draw text after painting in a JTextField

N

newbo

Hi all,

I'm trying to create an extension of JTextField with a highlight. I'm
using paintComponent, setting opaque to true, etc etc. The JTextField
highlight needs to be able to be turned off and on--and that part
works. However, when the highlight is being painted, then my text
disappears! I kinda get that I'm painting over it--especially since
it is a pattern--but how do I repaint the text over the highlight?

I feel like this should be a fairly straightforward answer--probably
one line of code or less that I am just clueless about--but I've tried
to drawstring and stuff and it just isn't working for me.

Thanks in advance.
newbo

public void paintComponent(Graphics g) {
super.paintComponent(g);
String text = getText();
if (isUsingHighlights && hasMultipleValues) {

Color back = sameValsColorBackground;
Color fore = sameValsColorForeground;

if (useDiffValsHighlight) {
back = diffValsColorBackground;
fore = diffValsColorForeground;
}

if (alphaColor != 0) {
back = new Color(back.getRed(), back.getGreen(), back.getBlue
(), alphaColor);
}

int w = getWidth();
int h = getHeight();
int leftW = w / 2;
int topH = h / 2;
// Paint the top left and bottom right in red.
g.setColor(back);
g.fillRect(0, 0, leftW, topH);
g.fillRect(leftW, topH, w - leftW, h - topH);

// Paint the bottom left and top right in white.
g.setColor(Color.WHITE);
g.fillRect(0, topH, leftW, h - topH);
g.fillRect(leftW, 0, w - leftW, topH);


}

}
 
R

Roedy Green

I'm trying to create an extension of JTextField with a highlight. I'm
using paintComponent, setting opaque to true, etc etc. The JTextField
highlight needs to be able to be turned off and on--and that part
works. However, when the highlight is being painted, then my text
disappears! I kinda get that I'm painting over it--especially since
it is a pattern--but how do I repaint the text over the highlight?

It looks as though you painted over top of your text. I can think of
four approaches.

1. do your highlight THEN call the super.paintComponent and get it to
paint over your carefully constructed background.

2. redraw the text over your highlights with drawString. You might as
well have started with Canvas or JPanel. This is what JDisplay does.
See http://mindprod.com/products1.html#JDISPLAY.

3. markup your text with HTML and put it in a JEditor pane and let
Swing deal with the highlighting. see
http://mindprod.com/jgloss/jeditorpane.html

4. use a JTable with a CustomCellRenderer
--
Roedy Green Canadian Mind Products
http://mindprod.com

"It’s a job that’s never started that takes the longest to finish."
~ J. R. R. Tolkien
 
N

newbo

It looks as though you painted over top of your text.  I can think of
four approaches.

1. do your highlight THEN call the super.paintComponent and get it topaintover your carefully constructedbackground.

2. redraw the text over your highlights with drawString. You might as
well have started with Canvas or JPanel.  This is what JDisplay does.
Seehttp://mindprod.com/products1.html#JDISPLAY.

3. markup your text with HTML and put it in a JEditor pane and let
Swing deal with the highlighting. seehttp://mindprod.com/jgloss/jeditorpane.html

4. use a JTable with a CustomCellRenderer
--
Roedy Green Canadian Mind Productshttp://mindprod.com

"It’s a job that’s never started that takes the longest to finish."
~ J. R. R. Tolkien

Thank you. The first one worked like a charm.
newbo
 
R

Roedy Green

Thank you. The first one worked like a charm.
newbo

good going. That can be tricky fiddling the opacity to get he
background cleared appropriately.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Think of the earth as a living organism that is being attacked by billions of bacteria whose numbers double every forty years. Either the host dies, or the virus dies, or both die."
~ Gore Vidal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top