Java Code Examples for javax.swing.text.JTextComponent#selectAll()

The following examples show how to use javax.swing.text.JTextComponent#selectAll() . 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: JAutoColumnTable.java    From jeveassets with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 2
Source File: mxCellEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 3
Source File: SearchReplaceComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 4
Source File: StandardEditingPopupMenu.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
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();
	}
}
 
Example 5
Source File: NewBandDialog.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
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();
    }
}
 
Example 6
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@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);
  }
}
 
Example 7
Source File: TextComponentPopupMenu.java    From xtunnel with GNU General Public License v2.0 5 votes vote down vote up
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) {
		}
	}
}
 
Example 8
Source File: SwingUtils.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(@Nonnull final ActionEvent e) {
  final JTextComponent component = getFocusedComponent();
  if (component != null) {
    component.selectAll();
    component.requestFocusInWindow();
  }
}
 
Example 9
Source File: TextComponentPopupMenu.java    From finalspeed with GNU General Public License v2.0 5 votes vote down vote up
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) {
        }
    }
}
 
Example 10
Source File: TextComponentPopupMenu.java    From finalspeed-91yun with GNU General Public License v2.0 5 votes vote down vote up
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) {
		}
	}
}
 
Example 11
Source File: JTextObserver.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 12
Source File: TextComponentPopupMenu.java    From NSS with Apache License 2.0 5 votes vote down vote up
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) {
		}
	}
}
 
Example 13
Source File: OptionsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 14
Source File: TextFieldFocusListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void focusGained(FocusEvent e) {
    if (!e.isTemporary()) {
        JTextComponent textComp = (JTextComponent) e.getSource();
        if (textComp.getText().length() != 0) {
            textComp.selectAll();
        }
    }
}
 
Example 15
Source File: ParametersPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 16
Source File: ExceptionsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 17
Source File: mxCellEditor.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
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();
	}
}
 
Example 18
Source File: SelectAllAction.java    From i18n-editor with MIT License 4 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
       JTextComponent component = getFocusedComponent();
       component.selectAll();
   }
 
Example 19
Source File: NewProductDialog.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
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();
    }
}