下面列出了怎么用javax.swing.event.CaretEvent的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* After any user input, the value of the textfield is proofed. Depending on
* being a valid date, the value is colored green or red.
*
* @param event
* the caret event
*/
public void caretUpdate(CaretEvent event) {
String text = getText().trim();
String emptyMask = maskPattern.replace('#', placeholder);
if (text.length() == 0 || text.equals(emptyMask)) {
setForeground(Color.BLACK);
return;
}
try {
Date date = dateFormatter.parse(getText());
if (dateUtil.checkDate(date)) {
setForeground(darkGreen);
} else {
setForeground(Color.RED);
}
} catch (Exception e) {
setForeground(Color.RED);
}
}
/**
* After any user input, the value of the textfield is proofed. Depending on
* being an integer, the value is colored green or red.
*
* @param e
* the caret event
*/
public void caretUpdate(CaretEvent e) {
try {
int testValue = Integer.valueOf(textField.getText()).intValue();
if ((testValue >= min) && (testValue <= max)) {
textField.setForeground(darkGreen);
setValue(testValue, false, true);
} else {
textField.setForeground(Color.red);
}
} catch (Exception ex) {
if (ex instanceof NumberFormatException) {
textField.setForeground(Color.red);
}
// Ignore all other exceptions, e.g. illegal state exception
}
textField.repaint();
}
@Override
public void caretUpdate(CaretEvent e) {
final List<Mark> old = getMarks();
marks = createMarks();
final List<Mark> nue = getMarks();
//Do not fire this event under the document's write lock
//may deadlock with other providers:
RP.post(new Runnable() {
@Override
public void run() {
firePropertyChange(PROP_MARKS, old, nue);
}
});
}
public void caretUpdate (CaretEvent e) {
if (rootNode == null) return;
ASTPath path = rootNode.findPath (e.getDot ());
if (path == null) return;
TreeNode tNode = (TreeNode) tree.getModel ().getRoot ();
List<TreeNode> treePath = new ArrayList<TreeNode> ();
Iterator it = path.listIterator ();
if (!it.hasNext ()) return;
it.next ();
treePath.add (tNode);
while (tNode instanceof TNode && it.hasNext ()) {
tNode = ((TNode) tNode).getTreeNode (it.next ());
if (tNode == null) throw new NullPointerException ();
treePath.add (tNode);
}
TreePath treePath2 = new TreePath (treePath.toArray ());
DefaultTreeModel model = new DefaultTreeModel ((TreeNode) tree.getModel ().getRoot ());
tree.setModel (model);
tree.setSelectionPath (treePath2);
tree.scrollPathToVisible (treePath2);
}
/**
* Creates new form WikiEditPanel
*/
public WikiEditPanel(String wikiLanguage, boolean editing, boolean switchable) {
this.wikiLanguage = wikiLanguage;
this.switchable = switchable;
this.wikiFormatText = "";
this.htmlFormatText = "";
initComponents();
pnlButtons.setVisible(switchable);
textCode.getDocument().addDocumentListener(new RevalidatingListener());
textPreview.getDocument().addDocumentListener(new RevalidatingListener());
textCode.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
makeCaretVisible(textCode);
}
});
textCode.getDocument().addDocumentListener(new EnablingListener());
// A11Y - Issues 163597 and 163598
UIUtils.fixFocusTraversalKeys(textCode);
UIUtils.issue163946Hack(scrollCode);
Spellchecker.register(textCode);
textPreview.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
setEditing(editing);
}
public TableViewLayoutTest() {
super("Code example for a TableView bug");
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
edit.setEditorKit(new CodeBugEditorKit());
initCodeBug();
this.getContentPane().add(new JScrollPane(edit));
this.pack();
this.setLocationRelativeTo(null);
edit.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
JTextComponent textComp = (JTextComponent) e.getSource();
try {
Rectangle rect = textComp.getUI().modelToView(textComp, e.getDot());
yCaret = rect.getY();
xCaret = rect.getX();
} catch (BadLocationException ex) {
throw new RuntimeException("Failed to get pixel position of caret", ex);
}
}
});
}
@Override
public void caretUpdate(CaretEvent e)
{
// Get the line the caret is positioned on
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
int currentLine = root.getElementIndex( caretPosition );
// Need to repaint so the correct line number can be highlighted
if (lastLine != currentLine)
{
repaint();
lastLine = currentLine;
}
}
@Override
public void caretUpdate(CaretEvent evt) {
JTextComponent comp = (JTextComponent) evt.getSource();
if (comp != null) {
if (comp.getSelectionStart() != comp.getSelectionEnd()) {
// cancel line highlighting if selection exists
removeLineHighlight(comp);
comp.repaint();
return;
}
int pos = comp.getCaretPosition();
Element elem = Utilities.getParagraphElement(comp, pos);
int start = elem.getStartOffset();
int end = elem.getEndOffset();
Document doc = comp.getDocument();
Element root = doc.getDefaultRootElement();
int line = root.getElementIndex(pos);
Debug.log(5, "LineHighlight: Caret at " + pos + " line " + line + " for " + start + "-" + end);
if (SikulixIDE.getStatusbar() != null) {
SikulixIDE.getStatusbar().setCaretPosition(line + 1, pos - start + 1);
}
removeLineHighlight(comp);
try {
highlight = comp.getHighlighter().addHighlight(start, end, painter);
comp.repaint();
} catch (BadLocationException ex) {
Debug.error(me + "Problem while highlighting line %d\n%s", pos, ex.getMessage());
}
}
}
public Comment(Listener listener)
{
m_listener = listener;
m_textPane = new JTextPane();
setFocusTraversalKeys(m_textPane);
GuiUtil.addStyle(m_textPane, "marked", Color.white,
Color.decode("#38d878"), false);
setPreferredSize();
m_textPane.getDocument().addDocumentListener(this);
CaretListener caretListener = new CaretListener()
{
public void caretUpdate(CaretEvent event)
{
if (m_listener == null)
return;
m_listener.textSelected(m_textPane.getSelectedText());
}
};
m_textPane.addCaretListener(caretListener);
setViewportView(m_textPane);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
if (Platform.isMac())
m_normalFont = new Font("Lucida Grande", Font.PLAIN, 11);
else
m_normalFont = UIManager.getFont("TextPane.font");
setMonoFont(false);
}
@Override
public void caretUpdate(CaretEvent e) {
// if (justInserted) {
// justInserted = false;
// }
// else {
// timer.stop();
// }
}
@Override
public void caretUpdate(CaretEvent e) {
JTextComponent cc = currentComponent != null ? currentComponent.get() : null;
if (e.getSource() != cc) {
((JTextComponent)e.getSource()).removeCaretListener(this);
//just a double check we do't get a persistent leak here..
return;
}
currentDot = e.getDot();
caretTask.schedule(1000);
}
public void caretUpdate(CaretEvent e) {
assert SwingUtilities.isEventDispatchThread();
if (!ignoreNextCaretUpdate) {
synchronized (this) {
selectionInfos = null;
selIndex = -1;
}
}
}
public void caretUpdate(CaretEvent e) {
JTextComponent component = componentRef == null ? null : componentRef.get();
if (component == null) {
return;
}
FileObject file = OpenedEditors.getFileObject(component);
if (file != null) {
setLastSelection(OpenedEditors.getFileObject(component), component.getSelectionStart(), component.getSelectionEnd());
rescheduleTask.schedule(timeout);
}
}
public void caretUpdate(CaretEvent e) {
JTextComponent c = component.get();
if (c == null) {
return;
}
FileObject file = OpenedEditors.getFileObject(c);
if (file != null) {
setLastPosition(file, c.getCaretPosition());
rescheduleTask.schedule(timeout);
}
}
public void caretUpdate(CaretEvent e) {
synchronized (this) {
lastCaretPosition = e.getDot();
}
LOG.fine("scheduling hints computation");
computeHint.schedule(100);
}
public void caretUpdate (final CaretEvent e) {
if (parsingTask != null) {
parsingTask.cancel ();
}
parsingTask = PROC.post (new Runnable () {
public void run () {
refresh (e.getDot ());
}
}, 1000);
}
public void caretUpdate (CaretEvent e) {
LanguagesNavigatorModel model = (LanguagesNavigatorModel) tree.getModel ();
TreePath treePath = model.getTreePath (e.getDot ());
if (treePath == null) return;
tree.setSelectionPath (treePath);
tree.scrollPathToVisible (treePath);
}
public void caretUpdate (CaretEvent e) {
int position = e.getDot ();
try {
selectPath (position);
} catch (ConcurrentModificationException ex) {
}
}
private CaretListener createCaretListenerForComponent() {
return new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
if (SearchBar.getInstance().isVisible()) {
int num = SearchBar.getInstance().getNumOfMatches();
SearchBar.getInstance().showNumberOfMatches(null, num);
}
}
};
}
public @Override void caretUpdate(CaretEvent e) {
if (!ignoreNextCaretUpdate) {
synchronized (this) {
selectionInfos = null;
selIndex = -1;
}
}
ignoreNextCaretUpdate = false;
}
/**
* Called when the caret position is updated.
*
* @param e the caret event
*/
public void caretUpdate( final CaretEvent e ) {
if ( ignoreTextEvents ) {
return;
}
editorModel.setCaretPosition( functionTextArea.getCaretPosition() );
refreshInformationPanel();
revalidateParameters( true );
}
/**
* Creates new form TaskPanel
*/
public TaskPanel (LocalTask task) {
this.task = task;
initComponents();
updateReadOnlyField(headerField);
Font font = new JLabel().getFont();
headerField.setFont(font.deriveFont((float) (font.getSize() * 1.7)));
mainScrollPane.getVerticalScrollBar().setUnitIncrement(10);
privateNotesField.addCaretListener(new CaretListener() {
@Override
public void caretUpdate (CaretEvent e) {
makeCaretVisible(privateNotesField);
}
});
// A11Y - Issues 163597 and 163598
UIUtils.fixFocusTraversalKeys(privateNotesField);
initSpellChecker();
attachmentsPanel = new AttachmentsPanel(this);
attachmentsSection.setContent(attachmentsPanel);
GroupLayout layout = (GroupLayout) attributesPanel.getLayout();
dueDatePicker = UIUtils.createDatePickerComponent();
scheduleDatePicker = new SchedulePicker();
layout.replace(dummyDueDateField, dueDatePicker.getComponent());
dueDateLabel.setLabelFor(dueDatePicker.getComponent());
layout.replace(dummyScheduleDateField, scheduleDatePicker.getComponent());
scheduleDateLabel.setLabelFor(scheduleDatePicker.getComponent());
attachListeners();
}
@Override
public void caretUpdate(final CaretEvent event) {
final JTextComponent lastEditor = lastEditorRef == null ? null : lastEditorRef.get();
if (lastEditor != null) {
Document doc = lastEditor.getDocument ();
String mimeType = DocumentUtilities.getMimeType (doc);
if (doc != null && mimeType != null) {
Source source = Source.create(doc);
if (source != null) {
((EventSupport)SourceEnvironment.forSource(source)).resetState(false, false, -1, -1, false);
}
}
}
}
public void caretUpdate(CaretEvent evt) {
if (evt.getDot() != evt.getMark()) {
surroundsWithTimer.setInitialDelay(SURROUND_WITH_DELAY);
surroundsWithTimer.restart();
} else {
surroundsWithTimer.stop();
hideSurroundWithHint();
}
}
public void caretUpdate(CaretEvent e) {
Object source = e.getSource();
// Custom delimeter field
if (source == customDelimField) {
if (customDelimRadioButton.isSelected()) {
String customDelim = customDelimField.getText();
if (customDelim != null && customDelim.length() > 0) {
this.setDelim(customDelim);
} else {
customDelimField.setForeground(Color.BLACK);
}
}
}
}
@Override
public void caretUpdate(CaretEvent e) {
int pos = e.getDot();
SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
Token token = doc.getTokenAt(pos);
removeMarkers();
if (token != null && tokenTypes.contains(token.type)) {
addMarkers(token);
}
}
@Override
public void caretUpdate(CaretEvent e) {
removeMarkers();
int pos = e.getDot();
SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
Token token = doc.getTokenAt(pos);
if (token != null && token.pairValue != 0) {
Markers.markToken(pane, token, marker);
Token other = doc.getPairFor(token);
if (other != null) {
Markers.markToken(pane, other, marker);
}
}
}
@Override
public void caretUpdate(CaretEvent e) {
// see notes in the similar docListener
synchronized (caretListener) {
dirty = true;
SwingUtilities.invokeLater(rehighlightRunnable);
}
}
@Override
public void caretUpdate(CaretEvent e) {
removeMarkers();
int pos = e.getDot();
SyntaxDocument doc = ActionUtils.getSyntaxDocument(pane);
Token token = doc.getTokenAt(pos);
if (token != null && token.pairValue != 0) {
Markers.markToken(pane, token, marker);
Token other = doc.getPairFor(token);
if (other != null) {
Markers.markToken(pane, other, marker);
}
}
}
@Override
public void caretUpdate(CaretEvent e) {
// Get the line the caret is positioned on
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
int currentLine = root.getElementIndex(caretPosition);
// Need to repaint so the correct line number can be highlighted
if (lastLine != currentLine) {
repaint();
lastLine = currentLine;
}
}