下面列出了怎么用javax.swing.text.DefaultHighlighter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
private FindSupport(TopComponent tc) {
this.tc = tc;
bar = new FindBar(this);
ActionMap actionMap = tc.getActionMap();
CallbackSystemAction a = SystemAction.get(org.openide.actions.FindAction.class);
actionMap.put(a.getActionMapKey(), new FindAction(true));
actionMap.put(FIND_NEXT_ACTION, new FindAction(true));
actionMap.put(FIND_PREVIOUS_ACTION, new FindAction(false));
// Hack ensuring the same shortcuts as editor
JEditorPane pane = new JEditorPane();
for (Action action : pane.getEditorKitForContentType("text/x-java").getActions()) { // NOI18N
Object name = action.getValue(Action.NAME);
if (FIND_NEXT_ACTION.equals(name) || FIND_PREVIOUS_ACTION.equals(name)) {
reuseShortcut(action);
}
}
// PENDING the colors below should not be hardcoded
highlighterAll = new DefaultHighlighter.DefaultHighlightPainter(new Color(255,180,66));
highlighterCurrent = new DefaultHighlighter.DefaultHighlightPainter(new Color(176,197,227));
pattern = Pattern.compile("$^"); // NOI18N
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
private static Object highlightText(JTextComponent textComponent, int fromPos, int toPos, Color color,
boolean drawsLayeredHighlights) {
if ((color != null) && (textComponent != null) && (toPos > fromPos)) {
DefaultHighlighter highlighter = (DefaultHighlighter) textComponent.getHighlighter();
highlighter.setDrawsLayeredHighlights(drawsLayeredHighlights);
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(color);
try {
return highlighter.addHighlight(
Math.max(fromPos, 0),
Math.min(toPos, textComponent.getText().length()),
painter);
} catch (BadLocationException e) {
}
}
return null;
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
int start = info.start;
int end = info.end;
if (start == -1 && end == -1)
return;
if (start == -1)
start = end;
if (end == -1)
end = start;
try {
h.addHighlight(info.start, info.end,
new DefaultHighlighter.DefaultHighlightPainter(c));
if (info.pos != -1) {
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
h.addHighlight(info.pos, info.pos + 1,
new DefaultHighlighter.DefaultHighlightPainter(c2));
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
/**
* Determines whether the SelectedTextColor should be used for painting text
* foreground for the specified highlight.
*
* Returns true only if the highlight painter for the specified highlight
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
* is null or equals to the selection color of the text component.
*
* This is a hack for fixing both bugs 4761990 and 5003294
*/
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
Highlighter.HighlightPainter painter = h.getPainter();
String painterClass = painter.getClass().getName();
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter defPainter =
(DefaultHighlighter.DefaultHighlightPainter) painter;
if (defPainter.getColor() != null &&
!defPainter.getColor().equals(c.getSelectionColor())) {
return false;
}
} catch (ClassCastException e) {
return false;
}
return true;
}
public void setHighlight(int lineNumber, Color color) throws BadLocationException {
if (textArea == null) {
return;
}
painter = new DefaultHighlighter.DefaultHighlightPainter(color);
int startIndex = textArea.getLineStartOffset(lineNumber - 1);
int endIndex = textArea.getLineEndOffset(lineNumber - 1);
try {
SwingUtilities.invokeLater(() -> {
textArea.moveCaretPosition(startIndex);
});
} catch (Error | Exception ex) {
ex.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
try {
textArea.getHighlighter().addHighlight(startIndex, endIndex, painter);
} catch (BadLocationException e) {
ISSUE_REPORTING_EXCEPTION_HANDLER.uncaughtException(Thread.currentThread(), e);
}
});
}