javax.swing.JTextField.getHighlighter()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(173)

本文整理了Java中javax.swing.JTextField.getHighlighter()方法的一些代码示例,展示了JTextField.getHighlighter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JTextField.getHighlighter()方法的具体详情如下:
包路径:javax.swing.JTextField
类名称:JTextField
方法名:getHighlighter

JTextField.getHighlighter介绍

暂无

代码示例

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public void valueChanged(ListSelectionEvent arg0) {
 TreeFromFile t = (TreeFromFile) list.getSelectedValue();
 if(t == null) {
  lastSelected = null;
  return;
 }
 JTextField curSelected = t.getLabel();
 if(lastSelected != null) {
  if(lastSelected != curSelected) { //get rid of old highlights
   lastSelected.getHighlighter().removeAllHighlights();
   lastSelected = curSelected;
   firstMouseEvent = null;
   lastSelected.repaint();
  }
 } else
  lastSelected = curSelected;
}

代码示例来源:origin: stanfordnlp/CoreNLP

private static String exportString(JComponent c) {
 JList list = (JList)c;
 Object[] values = list.getSelectedValues();
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < values.length; i++) {
  TreeFromFile val = (TreeFromFile) values[i];
  Highlighter h = val.getLabel().getHighlighter();
  Highlight[] highlights = h.getHighlights();
  if(highlights == null || highlights.length == 0) {
   sb.append(val.getLabel().getText());
  } else {
   //we have a highlight
   for(int j = 0; i < highlights.length; i++) {
    sb.append(val.getLabel().getText().substring(highlights[j].getStartOffset(), highlights[j].getEndOffset()));
   }
  }
 }
 return sb.toString();
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Highlight the given label from the first mouse event to the second
 * Returns true if the highlight was successful, false otherwise.
 */
public static boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2) {
 FontMetrics fm = label.getFontMetrics(label.getFont());
 int firstXpos = mouseEvent1.getX();
 int lastXpos = mouseEvent2.getX();
 int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
 int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
 if(lastOffset != firstOffset) {
  if(firstOffset > lastOffset) {
   int tmp = firstOffset;
   firstOffset = lastOffset;
   lastOffset = tmp;
  }
  try {
   label.getHighlighter().removeAllHighlights();
   label.getHighlighter().addHighlight(firstOffset, lastOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
   return true;
  } catch (BadLocationException e1) {
   return false;
  }
 } else
  return false;
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public void mousePressed(MouseEvent e) {
 if (MatchesPanel.getInstance().isEmpty()) return;
 if(firstMouseEvent == null) {
  firstMouseEvent = e;
 }
 e.consume();
 if(((e.getModifiersEx()) & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) {
  //shift is being held
  addHighlight(textField, firstMouseEvent, e);
 } else if(!HighlightUtils.isInHighlight(e, textField, textField.getHighlighter())) {
  textField.getHighlighter().removeAllHighlights();
  firstMouseEvent = e;
  dragNDrop = false;
  textField.repaint();
 } else {
  //in a highlight, if we drag after this, we'll be DnDing
  dragNDrop = true;
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public void mousePressed(MouseEvent e) {
 if (MatchesPanel.getInstance().isEmpty()) return;
 if(firstMouseEvent == null) {
  firstMouseEvent = e;
 }
 e.consume();
 TreeFromFile selectedValue = (TreeFromFile) list.getSelectedValue();
 if(selectedValue == null) return;
 JTextField label = selectedValue.getLabel();
 if(((e.getModifiersEx()) & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) {
  //shift is being held
  addHighlight(label, firstMouseEvent, e);
 } else if(!HighlightUtils.isInHighlight(e, label, label.getHighlighter())) {
  label.getHighlighter().removeAllHighlights();
  firstMouseEvent = e;
  dragNDrop = false;
  list.repaint();
 } else {
  //in a highlight, if we drag after this, we'll be DnDing
  dragNDrop = true;
 }
}

代码示例来源:origin: stackoverflow.com

void highlightWhitespaceText(JTextField text)
 {
   text.setHighlighter(AbstractTableCellRenderer.defaultHighlighter);
   try
   {
     Matcher m = AbstractTableCellRenderer.whitespaceStartPattern.matcher(text.getText());
     if (m.matches())
     {
       text.getHighlighter().addHighlight(m.start(1), m.end(1), AbstractTableCellRenderer.highlightPainter);
     }
     m = AbstractTableCellRenderer.whitespaceEndPattern.matcher(text.getText());
     if (m.matches())
     {
       text.getHighlighter().addHighlight(m.start(1), m.end(1), AbstractTableCellRenderer.highlightPainter);
     }
   }
   catch (BadLocationException ble)
   {
     //
   }
 }

代码示例来源:origin: stackoverflow.com

TableCellRenderer r = new TableCellRenderer() {
   JTextField f = new JTextField();
   @Override
   public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
     if(arg1 != null){
       f.setText(arg1.toString());
       try {
         f.getHighlighter().addHighlight(0,3,new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.RED));
       } catch (BadLocationException e) {
         e.printStackTrace();
       }
     } else {
       f.setText("");
       f.getHighlighter().removeAllHighlights();
     }
     return f;
   }
 };

代码示例来源:origin: edu.stanford.nlp/stanford-corenlp

private static String exportString(JComponent c) {
 JList list = (JList)c;
 Object[] values = list.getSelectedValues();
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < values.length; i++) {
  TreeFromFile val = (TreeFromFile) values[i];
  Highlighter h = val.getLabel().getHighlighter();
  Highlight[] highlights = h.getHighlights();
  if(highlights == null || highlights.length == 0) {
   sb.append(val.getLabel().getText());
  } else {
   //we have a highlight
   for(int j = 0; i < highlights.length; i++) {
    sb.append(val.getLabel().getText().substring(highlights[j].getStartOffset(), highlights[j].getEndOffset()));
   }
  }
 }
 return sb.toString();
}

代码示例来源:origin: edu.stanford.nlp/stanford-corenlp

@Override
public void valueChanged(ListSelectionEvent arg0) {
 TreeFromFile t = (TreeFromFile) list.getSelectedValue();
 if(t == null) {
  lastSelected = null;
  return;
 }
 JTextField curSelected = t.getLabel();
 if(lastSelected != null) {
  if(lastSelected != curSelected) { //get rid of old highlights
   lastSelected.getHighlighter().removeAllHighlights();
   lastSelected = curSelected;
   firstMouseEvent = null;
   lastSelected.repaint();
  }
 } else
  lastSelected = curSelected;
}

代码示例来源:origin: edu.stanford.nlp/stanford-corenlp

/**
 * Highlight the given label from the first mouse event to the second
 * Returns true if the highlight was successful, false otherwise.
 */
public static boolean addHighlight(JTextField label, MouseEvent mouseEvent1, MouseEvent mouseEvent2) {
 FontMetrics fm = label.getFontMetrics(label.getFont());
 int firstXpos = mouseEvent1.getX();
 int lastXpos = mouseEvent2.getX();
 int firstOffset = getCharOffset(fm, label.getText(), firstXpos);
 int lastOffset = getCharOffset(fm, label.getText(), lastXpos);
 if(lastOffset != firstOffset) {
  if(firstOffset > lastOffset) {
   int tmp = firstOffset;
   firstOffset = lastOffset;
   lastOffset = tmp;
  }
  try {
   label.getHighlighter().removeAllHighlights();
   label.getHighlighter().addHighlight(firstOffset, lastOffset, new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
   return true;
  } catch (BadLocationException e1) {
   return false;
  }
 } else
  return false;
}

代码示例来源:origin: edu.stanford.nlp/stanford-corenlp

@Override
public void mousePressed(MouseEvent e) {
 if (MatchesPanel.getInstance().isEmpty()) return;
 if(firstMouseEvent == null) {
  firstMouseEvent = e;
 }
 e.consume();
 if(((e.getModifiersEx()) & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) {
  //shift is being held
  addHighlight(textField, firstMouseEvent, e);
 } else if(!HighlightUtils.isInHighlight(e, textField, textField.getHighlighter())) {
  textField.getHighlighter().removeAllHighlights();
  firstMouseEvent = e;
  dragNDrop = false;
  textField.repaint();
 } else {
  //in a highlight, if we drag after this, we'll be DnDing
  dragNDrop = true;
 }
}

代码示例来源:origin: IanDarwin/darwinsys-api

stringTF = new JTextField(40);
stringTF.getDocument().addDocumentListener(new StringListener());
highlighter = stringTF.getHighlighter();
onlyHighlight = highlighter.addHighlight(0, 0, DefaultHighlighter.DefaultPainter);
strPane.add(stringTF);

代码示例来源:origin: com.darwinsys/darwinsys-api

stringTF = new JTextField(40);
stringTF.getDocument().addDocumentListener(new StringListener());
highlighter = stringTF.getHighlighter();
onlyHighlight = highlighter.addHighlight(0, 0, DefaultHighlighter.DefaultPainter);
strPane.add(stringTF);

代码示例来源:origin: edu.stanford.nlp/stanford-corenlp

@Override
public void mousePressed(MouseEvent e) {
 if (MatchesPanel.getInstance().isEmpty()) return;
 if(firstMouseEvent == null) {
  firstMouseEvent = e;
 }
 e.consume();
 TreeFromFile selectedValue = (TreeFromFile) list.getSelectedValue();
 if(selectedValue == null) return;
 JTextField label = selectedValue.getLabel();
 if(((e.getModifiersEx()) & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) {
  //shift is being held
  addHighlight(label, firstMouseEvent, e);
 } else if(!HighlightUtils.isInHighlight(e, label, label.getHighlighter())) {
  label.getHighlighter().removeAllHighlights();
  firstMouseEvent = e;
  dragNDrop = false;
  list.repaint();
 } else {
  //in a highlight, if we drag after this, we'll be DnDing
  dragNDrop = true;
 }
}

相关文章

JTextField类方法