下面列出了怎么用javax.swing.event.CaretListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
private void updateActiveEditor() {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(this);
return;
}
JTextComponent c = findActivePane();
if (c == null) {
editorReleased();
return;
}
if (activeEditor != null && activeEditor.get() == c) {
return;
}
editorReleased();
activeEditor = new WeakReference<>(c);
wCaretL = WeakListeners.create(CaretListener.class, this, c);
c.addCaretListener(this);
selectCurrentNode();
}
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);
}
}
});
}
RowColumnLabel(ISQLEntryPanel sqlEntryPanel)
{
super(" ", JLabel.CENTER);
_sqlEntryPanel = sqlEntryPanel;
sqlEntryPanel.addCaretListener(new CaretListener()
{
public void caretUpdate(CaretEvent e)
{
onCaretUpdate(e);
}
});
writePosition(0,0, 0);
setToolTipText(s_stringMgr.getString("RowColumnLabel.tooltip"));
}
public SlotConstTraitDetailPanel(final DecompiledEditorPane editor) {
slotConstEditor = new LineMarkedEditorPane();
setLayout(new BorderLayout());
add(new JScrollPane(slotConstEditor), BorderLayout.CENTER);
slotConstEditor.setFont(Configuration.getSourceFont());
slotConstEditor.changeContentType("text/flasm3");
slotConstEditor.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
if (ignoreCaret) {
return;
}
Highlighting spec = Highlighting.searchPos(specialHilights, slotConstEditor.getCaretPosition());
if (spec != null) {
editor.hilightSpecial(spec.getProperties().subtype, (int) spec.getProperties().index);
slotConstEditor.getCaret().setVisible(true);
}
}
});
}
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);
}
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);
}
}
};
}
/**
* 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();
}
/**
* Maps {@code JTextComponent.addCaretListener(CaretListener)} through queue
*/
public void addCaretListener(final CaretListener caretListener) {
runMapping(new MapVoidAction("addCaretListener") {
@Override
public void map() {
((JTextComponent) getSource()).addCaretListener(caretListener);
}
});
}
/**
* Maps {@code JTextComponent.removeCaretListener(CaretListener)}
* through queue
*/
public void removeCaretListener(final CaretListener caretListener) {
runMapping(new MapVoidAction("removeCaretListener") {
@Override
public void map() {
((JTextComponent) getSource()).removeCaretListener(caretListener);
}
});
}
@Override
public void addCaretListener(CaretListener listener) {
if (this.ipFields != null) {
for (JIPV4Field field : this.ipFields) {
field.addCaretListener(listener);
}
}
}
@Override
public void removeCaretListener(CaretListener listener) {
if (this.ipFields != null) {
for (JIPV4Field field : this.ipFields) {
field.removeCaretListener(listener);
}
}
}
protected void fireCaretEvent() {
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i--) {
if (listeners[i] == CaretListener.class) {
((CaretListener) listeners[i + 1]).caretUpdate(caretEvent);
}
}
}
private void disableMatchingHighlighter() {
for (CaretListener cl : textEditor.getCaretListeners()) {
if (cl instanceof MatchingHighlighter) {
textEditor.removeCaretListener(cl);
}
}
}
@Override
public void validateValue() {
Amf3Importer importer = new Amf3Importer();
String textVal = editor.getText();
try {
if (!textVal.trim().isEmpty()) {
importer.stringToAmf(textVal);
}
} catch (IOException | Amf3ParseException ex) {
if (ex instanceof Amf3ParseException) {
Amf3ParseException ape = (Amf3ParseException) ex;
if (ape.line > 0) {
editor.gotoLine((int) ape.line);
}
}
final CaretListener cl = new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
errorLabel.setVisible(false);
editor.removeCaretListener(this);
}
};
editor.addCaretListener(cl);
errorLabel.setText("<html>" + AppStrings.translate("error") + ":" + ex.getMessage() + "</html>");
errorLabel.setVisible(true);
throw new IllegalArgumentException("Invalid AMF value", ex);
}
}
public GtpShell(Frame owner, Listener listener,
MessageDialogs messageDialogs)
{
super(owner, i18n("TIT_SHELL"));
m_messageDialogs = messageDialogs;
m_listener = listener;
Preferences prefs = Preferences.userNodeForPackage(getClass());
m_historyMin = prefs.getInt("history-min", 2000);
m_historyMax = prefs.getInt("history-max", 3000);
JPanel panel = new JPanel(new BorderLayout());
getContentPane().add(panel, BorderLayout.CENTER);
m_gtpShellText = new GtpShellText(m_historyMin, m_historyMax, false);
CaretListener caretListener = new CaretListener()
{
public void caretUpdate(CaretEvent event)
{
if (m_listener == null)
return;
// Call the callback only if the selected text has changed.
// This avoids that the callback is called multiple times
// if the caret position changes, but the text selection
// was null before and after the change (see also bug
// #2964755)
String selectedText = m_gtpShellText.getSelectedText();
if (! ObjectUtil.equals(selectedText, m_selectedText))
{
m_listener.textSelected(selectedText);
m_selectedText = selectedText;
}
}
};
m_gtpShellText.addCaretListener(caretListener);
m_scrollPane =
new JScrollPane(m_gtpShellText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
if (Platform.isMac())
// Default Apple L&F uses no border, but Quaqua 3.7.4 does
m_scrollPane.setBorder(null);
panel.add(m_scrollPane, BorderLayout.CENTER);
panel.add(createCommandInput(), BorderLayout.SOUTH);
setMinimumSize(new Dimension(160, 112));
pack();
}
private void registerAction(EditCommand<AbstractAction> command,
final Selection selection, final boolean requiresEdits) {
final AbstractAction action = new EditMenuAction(
(String) command.getValue(AbstractAction.ACTION_COMMAND_KEY));
PropertyChangeListener pcl = new PropertyChangeListener() {
JTextComponent textComponent;
CaretListener caretListener = new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
refresh();
}
};
@Override
public void propertyChange(PropertyChangeEvent evt) {
Component c = KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusOwner();
if (c == textComponent)
return;
JTextComponent jtc = c instanceof JTextComponent ? ((JTextComponent) c)
: null;
if (textComponent != null)
textComponent.removeCaretListener(caretListener);
textComponent = jtc;
if (textComponent != null)
textComponent.addCaretListener(caretListener);
refresh();
}
private void refresh() {
if (textComponent == null) {
action.setEnabled(false);
} else if (!textComponent.isEditable() && requiresEdits) {
action.setEnabled(false);
} else if (selection == null) {
action.setEnabled(true);
} else {
action.setEnabled(selection.accepts(textComponent));
}
}
};
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("focusOwner", pcl);
command.install(action);
registerAction(action);
}
public void addCaretListener(CaretListener lis)
{
_comp.addCaretListener(lis);
}
public void removeCaretListener(CaretListener lis)
{
_comp.removeCaretListener(lis);
}
/**
* Adds a caret change listener to this text area.
*
* @param listener
* The listener
*/
public final void addCaretListener(CaretListener listener) {
listenerList.add(CaretListener.class, listener);
}
/**
* Removes a caret change listener from this text area.
*
* @param listener
* The listener
*/
public final void removeCaretListener(CaretListener listener) {
listenerList.remove(CaretListener.class, listener);
}
void addCaretListener(CaretListener lis);
void removeCaretListener(CaretListener lis);