javax.swing.text.DefaultHighlighter.DefaultHighlightPainter Java Examples

The following examples show how to use javax.swing.text.DefaultHighlighter.DefaultHighlightPainter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  int maskRange = 2;
  HighlightPainter highlightPainter = new DefaultHighlightPainter(Color.GRAY);
  JTextArea textArea = new JTextArea();
  textArea.setText("aaaaaaaasdfasdfasdfasdf\nasdfasdfasdfasdfasdfasdf\n1234567890\naaaaaaaaaaaaaaaaaasdfasd");
  ((AbstractDocument) textArea.getDocument()).setDocumentFilter(new NonEditableLineDocumentFilter(maskRange));
  try {
    Highlighter hilite = textArea.getHighlighter();
    Document doc = textArea.getDocument();
    Element root = doc.getDefaultRootElement();
    for (int i = 0; i < maskRange; i++) { // root.getElementCount(); i++) {
      Element elem = root.getElement(i);
      hilite.addHighlight(elem.getStartOffset(), elem.getEndOffset() - 1, highlightPainter);
    }
  } catch (BadLocationException ex) {
    // should never happen
    RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
    wrap.initCause(ex);
    throw wrap;
  }
  add(new JScrollPane(textArea));
  setPreferredSize(new Dimension(320, 240));
}
 
Example #2
Source File: BasicConsole.java    From pumpernickel with MIT License 5 votes vote down vote up
private void reapplyErrorHighlighting() {
	getHighlighter().removeAllHighlights();
	for (HighlightRun run : highlightRuns) {
		try {
			getHighlighter().addHighlight(run.start, run.end,
					new DefaultHighlightPainter(run.highlight));
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
}
 
Example #3
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JTextField field1 = new JTextField("0987654321");
  field1.setSelectedTextColor(Color.RED);
  field1.setSelectionColor(Color.GREEN);

  HighlightPainter selectionPainter = new DefaultHighlightPainter(Color.WHITE) {
    @Override public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
      Shape s = super.paintLayer(g, offs0, offs1, bounds, c, view);
      if (s instanceof Rectangle) {
        Rectangle r = (Rectangle) s;
        g.setColor(Color.ORANGE);
        g.fillRect(r.x, r.y + r.height - 2, r.width, 2);
      }
      return s;
    }
  };
  Caret caret = new DefaultCaret() {
    @Override protected HighlightPainter getSelectionPainter() {
      return selectionPainter;
    }
  };
  JTextField field2 = new JTextField("123465789735");
  caret.setBlinkRate(field2.getCaret().getBlinkRate());
  field2.setSelectedTextColor(Color.RED);
  field2.setCaret(caret);

  Box box = Box.createVerticalBox();
  box.add(makeTitledPanel("Default", new JTextField("12345")));
  box.add(Box.createVerticalStrut(10));
  box.add(makeTitledPanel("JTextComponent#setSelectionColor(...)", field1));
  box.add(Box.createVerticalStrut(10));
  box.add(makeTitledPanel("JTextComponent#setCaret(...)", field2));
  box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  add(box, BorderLayout.NORTH);
  setPreferredSize(new Dimension(320, 240));
}
 
Example #4
Source File: GUIWindow.java    From pretty-formula with MIT License 5 votes vote down vote up
/**
 * Creates new form GUIWindow
 */
public GUIWindow() {
   initComponents();
   this.errorHighlighter = new DefaultHighlightPainter(Color.red);
   this.fileChooser = new JFileChooser();
   this.fileChooser.setFileFilter(new FileNameExtensionFilter("SVG file", "svg", "SVG"));
}