I'm currently trying to display an image (with a Jlabel) onto a JEditorPane. I'm working with Swixml and a Form Layout, I have placed JLabel and JEditorPane at the same place. So I simply set "setVisible" to true or false to make the image appear.
So far, everythings good. But when someone try to select the text in the JEditorPane, here the ugly thing :
I try some things:
Disable JEditorPane :
No effect, even if the selection is'nt visible, the text appear on the JLabel.
Repaint :
When i repaint the JLabel, it's push to front again, but i have to do it repeateadly (like once every 50ms) to make it not visible;
I also tried to repaint on selection, but it dont work well, some case are just ignored, or to slow and it still ugly
Last idea was to prevent mouse to enter in JEditorPane when JLabel is displayer, but the MouseEnter event do not alow me to get the point out of EditorPane, so it became hard to put the cursor at a good place, out of JEditorPane...
I tried setComponentZOrder, but i was not able to make it work well (I certainly misused it), but i dont think it can solve my problem, because it only change the painting order (the last to be paint is on the top with Java), but it's obviously a repainting problem.
It should be usefull to point out that I'm forced to use Java 1.6 (conflict between to identicaly named class I have to use in Java 1.7 and 1.8, Sun's change log talks about a fix in 1.9 ...)
The context is an Applet, and in case I would not be understandable (English is not my mother tongue), don't hesitate to correct me and ask me.
Thanks for helping !
Let's take a look at DefaultHighlighter. JEditorPane uses it to render selecton (highlights) It has method
public void paint(Graphics g) {
// PENDING(prinz) - should cull ranges not visible
int len = highlights.size();
for (int i = 0; i < len; i++) {
HighlightInfo info = highlights.elementAt(i);
if (!(info instanceof LayeredHighlightInfo)) {
// Avoid allocing unless we need it.
Rectangle a = component.getBounds();
Insets insets = component.getInsets();
a.x = insets.left;
a.y = insets.top;
a.width -= insets.left + insets.right;
a.height -= insets.top + insets.bottom;
for (; i < len; i++) {
info = highlights.elementAt(i);
if (!(info instanceof LayeredHighlightInfo)) {
Highlighter.HighlightPainter p = info.getPainter();
p.paint(g, info.getStartOffset(), info.getEndOffset(),
a, component);
}
}
}
}
}
As you can see it paints all highlights.
You can replace the highlight painter (instance of DefaultHighlighter) of you JEditorPane with your extension. Override the paint() method and add custom clip to the Graphics instance. The clip should have custom Shape based on current clip where the JLabel rectangle is subtracted.