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);
}
}
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);
}
}