下面列出了javax.swing.text.JTextComponent#selectAll ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void valueChanged(ListSelectionEvent e) {
table.editCellAt(table.getSelectedRow(), table.getSelectedColumn());
Component editor = table.getEditorComponent();
if (editor != null) {
editor.requestFocus();
}
if (editor instanceof JTextComponent) {
JTextComponent textComp = (JTextComponent) editor;
textComp.selectAll();
}
}
public void valueChanged(ListSelectionEvent e) {
table.editCellAt(table.getSelectedRow(), table.getSelectedColumn());
Component editor = table.getEditorComponent();
if (editor instanceof JComboBox) {
editor = ((JComboBox) editor).getEditor().getEditorComponent();
}
if (editor != null) {
editor.requestFocus();
}
if (editor instanceof JTextComponent) {
JTextComponent textComp = (JTextComponent) editor;
textComp.selectAll();
}
}
@Override
public void focusGained(FocusEvent e) {
if (!e.isTemporary()) {
JTextComponent textComp = (JTextComponent) e.getSource();
if (textComp.getText().length() != 0) {
textComp.selectAll();
}
}
}
public void valueChanged(ListSelectionEvent e) {
optionsTable.editCellAt(optionsTable.getSelectedRow(), optionsTable.getSelectedColumn());
Component editor = optionsTable.getEditorComponent();
if (editor != null) {
editor.requestFocus();
}
if (editor instanceof JTextComponent) {
JTextComponent textComp = (JTextComponent) editor;
textComp.selectAll();
}
updateButtons();
}
public void actionPerformed(ActionEvent e) {
JTextComponent tc = (JTextComponent) getInvoker();
String sel = tc.getSelectedText();
if (e.getSource() == cutItem) {
tc.cut();
} else if (e.getSource() == copyItem) {
tc.copy();
} else if (e.getSource() == pasteItem) {
tc.paste();
} else if (e.getSource() == selectAllItem) {
tc.selectAll();
} else if (e.getSource() == deleteItem) {
Document doc = tc.getDocument();
int start = tc.getSelectionStart();
int end = tc.getSelectionEnd();
try {
Position p0 = doc.createPosition(start);
Position p1 = doc.createPosition(end);
if ((p0 != null) && (p1 != null)
&& (p0.getOffset() != p1.getOffset())) {
doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
}
} catch (BadLocationException be) {
}
}
}
/**
* Selects all the text when a field gains the focus.
*
* @param e the focus event.
*/
public void focusGained(final FocusEvent e) {
if (e.getSource() instanceof JTextComponent) {
final JTextComponent tex = (JTextComponent) e.getSource();
tex.selectAll();
}
}
public void actionPerformed(ActionEvent e) {
JTextComponent tc = (JTextComponent) getInvoker();
String sel = tc.getSelectedText();
if (e.getSource() == cutItem) {
tc.cut();
} else if (e.getSource() == copyItem) {
tc.copy();
} else if (e.getSource() == pasteItem) {
tc.paste();
} else if (e.getSource() == selectAllItem) {
tc.selectAll();
} else if (e.getSource() == deleteItem) {
Document doc = tc.getDocument();
int start = tc.getSelectionStart();
int end = tc.getSelectionEnd();
try {
Position p0 = doc.createPosition(start);
Position p1 = doc.createPosition(end);
if ((p0 != null) && (p1 != null)
&& (p0.getOffset() != p1.getOffset())) {
doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
}
} catch (BadLocationException be) {
}
}
}
public void actionPerformed(ActionEvent e) {
JTextComponent tc = (JTextComponent) getInvoker();
String sel = tc.getSelectedText();
if (e.getSource() == cutItem) {
tc.cut();
} else if (e.getSource() == copyItem) {
tc.copy();
} else if (e.getSource() == pasteItem) {
tc.paste();
} else if (e.getSource() == selectAllItem) {
tc.selectAll();
} else if (e.getSource() == deleteItem) {
Document doc = tc.getDocument();
int start = tc.getSelectionStart();
int end = tc.getSelectionEnd();
try {
Position p0 = doc.createPosition(start);
Position p1 = doc.createPosition(end);
if ((p0 != null) && (p1 != null)
&& (p0.getOffset() != p1.getOffset())) {
doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
}
} catch (BadLocationException ignored) {
}
}
}
@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
Component component = super.prepareEditor(editor, row, column);
if (component instanceof JTextComponent) {
JTextComponent jTextComponent = (JTextComponent) component;
TextManager.installTextComponent(jTextComponent);
jTextComponent.selectAll();
} else if (component instanceof Container) {
TextManager.installAll((Container) component);
}
return component;
}
@Override
public void actionPerformed(@Nonnull final ActionEvent e) {
final JTextComponent component = getFocusedComponent();
if (component != null) {
component.selectAll();
component.requestFocusInWindow();
}
}
public void actionPerformed(ActionEvent e) {
JTextComponent tc = (JTextComponent) getInvoker();
@SuppressWarnings("unused")
String sel = tc.getSelectedText();
if (e.getSource() == cutItem) {
tc.cut();
} else if (e.getSource() == copyItem) {
tc.copy();
} else if (e.getSource() == pasteItem) {
tc.paste();
} else if (e.getSource() == selectAllItem) {
tc.selectAll();
} else if (e.getSource() == deleteItem) {
Document doc = tc.getDocument();
int start = tc.getSelectionStart();
int end = tc.getSelectionEnd();
try {
Position p0 = doc.createPosition(start);
Position p1 = doc.createPosition(end);
if ((p0 != null) && (p1 != null)
&& (p0.getOffset() != p1.getOffset())) {
doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
}
} catch (BadLocationException be) {
}
}
}
@Override public void show(Component c, int x, int y) {
System.out.println(c.getClass().getName() + ": " + c.getName());
if (c instanceof JTextComponent) {
JTextComponent tc = (JTextComponent) c;
tc.requestFocusInWindow();
boolean hasSelectedText = Objects.nonNull(tc.getSelectedText());
if (tc instanceof JTextField && !tc.isFocusOwner() && !hasSelectedText) {
tc.selectAll();
hasSelectedText = true;
}
cutAction.setEnabled(hasSelectedText);
copyAction.setEnabled(hasSelectedText);
super.show(c, x, y);
}
}
private void createUI() {
final JPanel dialogPane = GridBagUtils.createPanel();
final GridBagConstraints gbc = GridBagUtils.createDefaultConstraints();
int line = 0;
GridBagUtils.addToPanel(dialogPane, _paramName.getEditor().getLabelComponent(), gbc,
"fill=BOTH, weightx=1, insets.top=3");
GridBagUtils.addToPanel(dialogPane, _paramName.getEditor().getComponent(), gbc);
GridBagUtils.addToPanel(dialogPane, _paramDesc.getEditor().getLabelComponent(), gbc, "gridy=" + ++line);
GridBagUtils.addToPanel(dialogPane, _paramDesc.getEditor().getComponent(), gbc);
GridBagUtils.addToPanel(dialogPane, _paramUnit.getEditor().getLabelComponent(), gbc, "gridy=" + ++line);
GridBagUtils.addToPanel(dialogPane, _paramUnit.getEditor().getComponent(), gbc);
GridBagUtils.addToPanel(dialogPane, _paramDataType.getEditor().getLabelComponent(), gbc, "gridy=" + ++line);
GridBagUtils.addToPanel(dialogPane, _paramDataType.getEditor().getComponent(), gbc);
_paramDataType.setUIEnabled(false);
GridBagUtils.addToPanel(dialogPane, createInfoPanel(), gbc, "gridy=" + ++line + ", insets.top=10, gridwidth=2");
setContent(dialogPane);
final JComponent editorComponent = _paramName.getEditor().getEditorComponent();
if (editorComponent instanceof JTextComponent) {
final JTextComponent tc = (JTextComponent) editorComponent;
tc.selectAll();
tc.requestFocus();
}
}
public void actionPerformed(ActionEvent e) {
final Component c = getInvoker();
if (c instanceof JTextComponent) {
JTextComponent text = (JTextComponent)c;
text.requestFocus();
if (c instanceof JFormattedTextField)
text.setText(text.getText());
text.selectAll();
}
}
private static void replaceTextInTextComponentEnsuringSelection(@Nonnull String textToSet, JTextComponent component) {
String existingText = component.getText();
if (!existingText.equals(textToSet)) {
component.setText(textToSet);
// textToSet should be selected even if we have no selection before (if we have the selection then setText will remain it)
if (component.getSelectionStart() == component.getSelectionEnd()) component.selectAll();
}
}
public void startEditing(Object cell, EventObject evt) {
if (editingCell != null) {
stopEditing(true);
}
mxCellState state = graphComponent.getGraph().getView().getState(cell);
if (state != null) {
editingCell = cell;
trigger = evt;
double scale = Math.max(minimumEditorScale, graphComponent.getGraph().getView().getScale());
scrollPane.setBounds(getEditorBounds(state, scale));
scrollPane.setVisible(true);
String value = getInitialValue(state, evt);
JTextComponent currentEditor = null;
// Configures the style of the in-place editor
textArea.setFont(mxUtils.getFont(state.getStyle(), scale));
Color fontColor = mxUtils.getColor(state.getStyle(), mxConstants.STYLE_FONTCOLOR, Color.black);
textArea.setForeground(fontColor);
textArea.setText(value);
scrollPane.setViewportView(textArea);
currentEditor = textArea;
graphComponent.getGraphControl().add(scrollPane, 0);
if (isHideLabel(state)) {
graphComponent.redraw(state);
}
currentEditor.revalidate();
currentEditor.requestFocusInWindow();
currentEditor.selectAll();
configureActionMaps();
}
}
public void startEditing(Object cell, EventObject evt)
{
if (editingCell != null)
{
stopEditing(true);
}
mxCellState state = graphComponent.getGraph().getView().getState(cell);
if (state != null)
{
editingCell = cell;
trigger = evt;
double scale = Math.max(minimumEditorScale, graphComponent
.getGraph().getView().getScale());
scrollPane.setBounds(getEditorBounds(state, scale));
scrollPane.setVisible(true);
String value = getInitialValue(state, evt);
JTextComponent currentEditor = null;
// Configures the style of the in-place editor
if (graphComponent.getGraph().isHtmlLabel(cell))
{
if (isExtractHtmlBody())
{
value = mxUtils.getBodyMarkup(value,
isReplaceHtmlLinefeeds());
}
editorPane.setDocument(mxUtils.createHtmlDocumentObject(
state.getStyle(), scale));
editorPane.setText(value);
// Workaround for wordwrapping in editor pane
// FIXME: Cursor not visible at end of line
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.setOpaque(false);
wrapper.add(editorPane, BorderLayout.CENTER);
scrollPane.setViewportView(wrapper);
currentEditor = editorPane;
}
else
{
textArea.setFont(mxUtils.getFont(state.getStyle(), scale));
Color fontColor = mxUtils.getColor(state.getStyle(),
mxConstants.STYLE_FONTCOLOR, Color.black);
textArea.setForeground(fontColor);
textArea.setText(value);
scrollPane.setViewportView(textArea);
currentEditor = textArea;
}
graphComponent.getGraphControl().add(scrollPane, 0);
if (isHideLabel(state))
{
graphComponent.redraw(state);
}
currentEditor.revalidate();
currentEditor.requestFocusInWindow();
currentEditor.selectAll();
configureActionMaps();
}
}
@Override
public void actionPerformed(ActionEvent e) {
JTextComponent component = getFocusedComponent();
component.selectAll();
}
private void createUI() {
createButtonsAndLabels();
int line = 0;
JPanel dialogPane = GridBagUtils.createPanel();
final GridBagConstraints gbc = GridBagUtils.createDefaultConstraints();
gbc.gridy = ++line;
GridBagUtils.addToPanel(dialogPane, paramNewName.getEditor().getLabelComponent(), gbc,
"fill=BOTH, weightx=0, insets.top=3");
GridBagUtils.addToPanel(dialogPane, paramNewName.getEditor().getComponent(), gbc, "weightx=1, gridwidth=3");
gbc.gridy = ++line;
GridBagUtils.addToPanel(dialogPane, paramNewDesc.getEditor().getLabelComponent(), gbc,
"weightx=0, gridwidth=1");
GridBagUtils.addToPanel(dialogPane, paramNewDesc.getEditor().getComponent(), gbc, "weightx=1, gridwidth=3");
gbc.gridy = ++line;
GridBagUtils.addToPanel(dialogPane, paramSourceProduct.getEditor().getLabelComponent(), gbc,
"fill=NONE, gridwidth=4, insets.top=15");
gbc.gridy = ++line;
GridBagUtils.addToPanel(dialogPane, paramSourceProduct.getEditor().getComponent(), gbc,
"fill=HORIZONTAL, insets.top=3");
gbc.gridy = ++line;
final JPanel radioPanel = new JPanel(new BorderLayout());
radioPanel.add(copyAllRButton, BorderLayout.WEST);
radioPanel.add(geocodingRButton);
GridBagUtils.addToPanel(dialogPane, radioPanel, gbc, "fill=NONE, gridwidth=2");
GridBagUtils.addToPanel(dialogPane, subsetRButton, gbc, "gridwidth=1, weightx=300, anchor=EAST");
GridBagUtils.addToPanel(dialogPane, subsetButton, gbc, "fill=NONE, weightx=1, anchor=EAST");
gbc.gridy = ++line;
GridBagUtils.addToPanel(dialogPane, createInfoPanel(), gbc,
"fill=BOTH, anchor=WEST, insets.top=10, gridwidth=4");
setContent(dialogPane);
final JComponent editorComponent = paramNewName.getEditor().getEditorComponent();
if (editorComponent instanceof JTextComponent) {
JTextComponent tf = (JTextComponent) editorComponent;
tf.selectAll();
tf.requestFocus();
}
}