下面列出了javax.swing.text.DefaultHighlighter#DefaultHighlightPainter ( ) 实例代码,或者点击链接到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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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);
}
});
}
/**
* 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 int appendText(JTextArea area, String text, boolean highlight) {
int start = area.getDocument().getLength();
if (text == null || text.length() == 0) {
return start;
}
int end = start + text.length();
try {
area.getDocument().insertString(start, text, null);
if (highlight) {
Highlighter hilite = area.getHighlighter();
HighlightPainter painter =
new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
hilite.addHighlight(start, end - 1, painter);
}
} catch (BadLocationException e) {
logger.error(e.getMessage(), e);
}
return end;
}
/**
* 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;
}
/**
* Initialize highlighter and painter.
*/
private void initHighlighter() {
highlighter = new DefaultHighlighter();
painter = new DefaultHighlighter.DefaultHighlightPainter(
chooseHighlightColor());
textPane.setHighlighter(highlighter);
}
private static void setHighlight(JTextArea highlight, JSONError error) {
Highlighter.HighlightPainter errorHighlighter =
new DefaultHighlighter.DefaultHighlightPainter(PINK);
try {
highlight.getHighlighter().addHighlight(error.getStartIndex(),
error.getEndIndex(),
errorHighlighter);
} catch (Exception ble) {
ble.printStackTrace();
}
}
private void highlightText(JTextArea area, int start, int end) {
Highlighter hilite = area.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
try {
hilite.addHighlight(start, end, painter);
} catch (BadLocationException e) {
logger.error(e.getMessage(), e);
}
}
public static Highlighter.HighlightPainter getFadingHighlighter() {
return new DefaultHighlighter.DefaultHighlightPainter(getFadingForeground());
}
public static Highlighter.HighlightPainter getCustomHighliter(Color color) {
return new DefaultHighlighter.DefaultHighlightPainter(color);
}
public static Highlighter.HighlightPainter getVividHighliter() {
return new DefaultHighlighter.DefaultHighlightPainter(getVividForeground());
}