本文整理汇总了Java中javax.swing.text.JTextComponent.requestFocus方法的典型用法代码示例。如果您正苦于以下问题:Java JTextComponent.requestFocus方法的具体用法?Java JTextComponent.requestFocus怎么用?Java JTextComponent.requestFocus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.JTextComponent
的用法示例。
在下文中一共展示了JTextComponent.requestFocus方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: showPopupMenu
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public void showPopupMenu(int x, int y) {
// First call the build-popup-menu action to possibly rebuild the popup menu
JTextComponent c = getComponent();
if (c != null) {
BaseKit kit = Utilities.getKit(c);
if (kit != null) {
Action a = kit.getActionByName(ExtKit.buildPopupMenuAction);
if (a != null) {
a.actionPerformed(new ActionEvent(c, 0, "")); // NOI18N
}
}
JPopupMenu pm = getPopupMenu();
if (pm != null) {
if (c.isShowing()) { // fix of #18808
if (!c.isFocusOwner()) {
c.requestFocus();
}
pm.show(c, x, y);
}
}
}
}
示例2: loadFileToTextArea
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Loads the contents of a selected file into a text component.
*
* @param id
* @param textComponent
*/
private void loadFileToTextArea(String id, JTextComponent textComponent) {
logger.info("Loading " + id + "...");
try {
if (TextComponentUtil.open(textComponent)) {
textComponent.requestFocus();
textComponent.setCaretPosition(0);
logger.info("Finished loading " + id);
} else {
logger.info("Canceled loading " + id);
}
} catch (Exception e) {
String message = "Failed loading " + id + ": " + e.getMessage();
logger.log(Level.SEVERE, message, e);
showErrorMessage(message);
}
}
示例3: paste
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Copies the text contents of the system clipboard to the text component.
*
* @param textComponent
*/
public static void paste(JTextComponent textComponent) {
String contents = ClipboardHandlerFactory.getClipboardHandler()
.getContents();
if (contents != null) {
StringBuffer buffer = new StringBuffer();
char c;
for (int i = 0, n = contents.length(); i < n; i++) {
c = contents.charAt(i);
// Not to copy a null character
if (c != 0) {
buffer.append(c);
}
}
textComponent.replaceSelection(buffer.toString());
}
if (!textComponent.hasFocus()) {
textComponent.requestFocus();
}
}
示例4: mouseClicked
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
if (!(e.getSource() instanceof JTextComponent)) {
return;
}
textComponent = (JTextComponent) e.getSource();
textComponent.requestFocus();
boolean enabled = textComponent.isEnabled();
boolean editable = textComponent.isEditable();
boolean nonempty = !(textComponent.getText() == null || textComponent.getText().equals(""));
boolean marked = textComponent.getSelectedText() != null;
boolean pasteAvailable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);
undoAction.setEnabled(enabled && editable && (lastActionSelected == Actions.CUT || lastActionSelected == Actions.PASTE));
cutAction.setEnabled(enabled && editable && marked);
copyAction.setEnabled(enabled && marked);
pasteAction.setEnabled(enabled && editable && pasteAvailable);
selectAllAction.setEnabled(enabled && nonempty);
int nx = e.getX();
if (nx > 500) {
nx = nx - popup.getSize().width;
}
popup.show(e.getComponent(), nx, e.getY() - popup.getSize().height);
}
}
示例5: requestFocus
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static void requestFocus(JTextComponent c) {
if (c != null) {
if (!ImplementationProvider.getDefault().activateComponent(c)) {
Frame f = EditorUI.getParentFrame(c);
if (f != null) {
f.requestFocus();
}
c.requestFocus();
}
}
}
示例6: requestFocus
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static void requestFocus(JTextComponent c) {
if (c != null) {
if (!EditorImplementation.getDefault().activateComponent(c)) {
Frame f = getParentFrame(c);
if (f != null) {
f.requestFocus();
}
c.requestFocus();
}
}
}
示例7: prepareEditor
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void prepareEditor() {
Component c = getEditor().getEditorComponent();
if (c instanceof JTextComponent) {
JTextComponent jtc = (JTextComponent) c;
String s = jtc.getText();
if ((s != null) && (s.length() > 0)) {
jtc.setSelectionStart(0);
jtc.setSelectionEnd(s.length());
}
if (tableUI) {
jtc.setBackground(getBackground());
} else {
jtc.setBackground(PropUtils.getTextFieldBackground());
}
if( tableUI )
jtc.requestFocus();
}
if (getLayout() != null) {
getLayout().layoutContainer(this);
}
repaint();
}
示例8: marathon_select
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override public boolean marathon_select(String value) {
JTextComponent tc = (JTextComponent) component;
tc.requestFocus();
Boolean clientProperty = (Boolean) tc.getClientProperty("marathon.celleditor");
clear();
if (clientProperty != null && clientProperty) {
sendKeys(value, JavaAgentKeys.ENTER);
} else {
sendKeys(value);
}
return true;
}
示例9: selectAll
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Selects all contents of a text component.
*
* @param textComponent
*/
public static void selectAll(JTextComponent textComponent) {
if (!textComponent.hasFocus()) {
textComponent.requestFocus();
}
textComponent.selectAll();
}
示例10: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
if (ignoreComboAction)
return; // not invoked by user, ignore
GuardedBlock gBlock = codeData.getGuardedBlock(category, blockIndex);
GuardBlockInfo gInfo = getGuardInfos(category)[blockIndex];
int[] blockBounds = getGuardBlockBounds(category, blockIndex);
int startOffset = blockBounds[0];
int endOffset = blockBounds[1];
int gHead = gBlock.getHeaderLength();
int gFoot = gBlock.getFooterLength();
JTextComponent editor = getEditor(category);
StyledDocument doc = (StyledDocument) editor.getDocument();
changed = true;
JComboBox combo = (JComboBox) e.getSource();
try {
docListener.setActive(false);
if (combo.getSelectedIndex() == 1) { // changing from default to custom
NbDocument.unmarkGuarded(doc, startOffset, endOffset - startOffset);
// keep last '\n' so we don't destroy next editable block's position
doc.remove(startOffset, endOffset - startOffset - 1);
// insert the custom code into the document
String customCode = gBlock.getCustomCode();
int customLength = customCode.length();
if (gInfo.customizedCode != null) { // already was edited before
customCode = customCode.substring(0, gHead)
+ gInfo.customizedCode
+ customCode.substring(customLength - gFoot);
customLength = customCode.length();
}
if (customCode.endsWith("\n")) // NOI18N
customCode = customCode.substring(0, customLength-1);
doc.insertString(startOffset, customCode, null);
gInfo.customized = true;
// make guarded "header" and "footer", select the text in between
NbDocument.markGuarded(doc, startOffset, gHead);
NbDocument.markGuarded(doc, startOffset + customLength - gFoot, gFoot);
editor.setSelectionStart(startOffset + gHead);
editor.setSelectionEnd(startOffset + customLength - gFoot);
editor.requestFocus();
combo.setToolTipText(gBlock.getCustomEntry().getToolTipText());
}
else { // changing from custom to default
// remember the customized code
gInfo.customizedCode = doc.getText(startOffset + gHead,
endOffset - gFoot - gHead - startOffset);
NbDocument.unmarkGuarded(doc, endOffset - gFoot, gFoot);
NbDocument.unmarkGuarded(doc, startOffset, gHead);
// keep last '\n' so we don't destroy next editable block's position
doc.remove(startOffset, endOffset - startOffset - 1);
String defaultCode = gBlock.getDefaultCode();
if (defaultCode.endsWith("\n")) // NOI18N
defaultCode = defaultCode.substring(0, defaultCode.length()-1);
doc.insertString(startOffset, defaultCode, null);
gInfo.customized = false;
// make the whole text guarded, cancel selection
NbDocument.markGuarded(doc, startOffset, defaultCode.length()+1); // including '\n'
if (editor.getSelectionStart() >= startOffset && editor.getSelectionEnd() <= endOffset)
editor.setCaretPosition(startOffset);
combo.setToolTipText(NbBundle.getMessage(CustomCodeData.class, "CTL_GuardCombo_Default_Hint")); // NOI18N
}
// we must create a new Position - current was moved away by inserting new string on it
gInfo.position = NbDocument.createPosition(doc, startOffset, Position.Bias.Forward);
docListener.setActive(true);
}
catch (BadLocationException ex) { // should not happen
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
}
}
示例11: mousePressed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void mousePressed(MouseEvent evt) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("mousePressed: " + logMouseEvent(evt) + ", state=" + mouseState + '\n'); // NOI18N
}
JTextComponent c = component;
if (c != null && isLeftMouseButtonExt(evt)) {
// Expand fold if offset is in collapsed fold
int offset = mouse2Offset(evt);
switch (evt.getClickCount()) {
case 1: // Single press
if (c.isEnabled() && !c.hasFocus()) {
c.requestFocus();
}
c.setDragEnabled(true);
if (evt.isShiftDown()) { // Select till offset
moveDot(offset);
adjustRectangularSelectionMouseX(evt.getX(), evt.getY()); // also fires state change
mouseState = MouseState.CHAR_SELECTION;
} else { // Regular press
// check whether selection drag is possible
if (isDragPossible(evt) && mapDragOperationFromModifiers(evt) != TransferHandler.NONE) {
mouseState = MouseState.DRAG_SELECTION_POSSIBLE;
} else { // Drag not possible
mouseState = MouseState.CHAR_SELECTION;
setDot(offset);
}
}
break;
case 2: // double-click => word selection
mouseState = MouseState.WORD_SELECTION;
// Disable drag which would otherwise occur when mouse would be over text
c.setDragEnabled(false);
// Check possible fold expansion
try {
// hack, to get knowledge of possible expansion. Editor depends on Folding, so it's not really possible
// to have Folding depend on BaseCaret (= a cycle). If BaseCaret moves to editor.lib2, this contract
// can be formalized as an interface.
Callable<Boolean> cc = (Callable<Boolean>)c.getClientProperty("org.netbeans.api.fold.expander");
if (cc == null || !cc.equals(this)) {
if (selectWordAction == null) {
selectWordAction = ((BaseKit) c.getUI().getEditorKit(
c)).getActionByName(BaseKit.selectWordAction);
}
if (selectWordAction != null) {
selectWordAction.actionPerformed(null);
}
// Select word action selects forward i.e. dot > mark
minSelectionStartOffset = getMark();
minSelectionEndOffset = getDot();
}
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
break;
case 3: // triple-click => line selection
mouseState = MouseState.LINE_SELECTION;
// Disable drag which would otherwise occur when mouse would be over text
c.setDragEnabled(false);
if (selectLineAction == null) {
selectLineAction = ((BaseKit) c.getUI().getEditorKit(
c)).getActionByName(BaseKit.selectLineAction);
}
if (selectLineAction != null) {
selectLineAction.actionPerformed(null);
// Select word action selects forward i.e. dot > mark
minSelectionStartOffset = getMark();
minSelectionEndOffset = getDot();
}
break;
default: // multi-click
}
}
}