本文整理了Java中javax.swing.JTextArea.getCaretPosition()
方法的一些代码示例,展示了JTextArea.getCaretPosition()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JTextArea.getCaretPosition()
方法的具体详情如下:
包路径:javax.swing.JTextArea
类名称:JTextArea
方法名:getCaretPosition
暂无
代码示例来源:origin: net.sf.meka/meka
/**
* Returns the current caret position.
*
* @return the position (0-based)
*/
public int getCaretPosition() {
return m_TextCode.getCaretPosition();
}
代码示例来源:origin: Waikato/meka
/**
* Returns the current caret position.
*
* @return the position (0-based)
*/
public int getCaretPosition() {
return m_TextCode.getCaretPosition();
}
代码示例来源:origin: senbox-org/snap-desktop
private void selectIndex(int index) {
final int position = textArea.getCaretPosition();
list.setSelectedIndex(index);
SwingUtilities.invokeLater(() -> textArea.setCaretPosition(position));
}
}
代码示例来源:origin: jaltekruse/OpenNotebook
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_ENTER && enterJustPressed)
{// had issue with hitting enter to confirm dialog, dialog disappeared but enter key release event
// was still received here if an object was selected, hence the boolean set in the keypressed
// method and checked here
String s = textArea.getText();
int caretPos = textArea.getCaretPosition() - 1;
s = s.substring(0, textArea.getCaretPosition() - 1) + s.substring(textArea.getCaretPosition());
textArea.setText(s);
textArea.setCaretPosition(caretPos);
enterJustPressed = false;
}
}
代码示例来源:origin: apache/axis2-java
private void setModule(String module){
int position = desArea.getCaretPosition();
String str = " <module ref=\"" + module + "\" />\n";
desArea .insert(str, position + 1);
}
}
代码示例来源:origin: net.java.openjdk.cacio/cacio-shared
@Override
public int getCaretPosition() {
return getTextArea().getCaretPosition();
}
代码示例来源:origin: com.darwinsys/darwinsys-api
public void actionPerformed(ActionEvent e) {
ta.insert(new Date().toString(), ta.getCaretPosition());
}
});
代码示例来源:origin: IanDarwin/darwinsys-api
public void actionPerformed(ActionEvent e) {
ta.insert(new Date().toString(), ta.getCaretPosition());
}
});
代码示例来源:origin: de.mhus.lib/mhu-lib-core
static public String getSelectedPart(JTextArea text) {
String s = text.getSelectedText();
if (s == null) {
s = text.getText();
int start = s.lastIndexOf("\n\n", text.getCaretPosition());
if (start < 0)
start = 0;
else
start += 2;
int end = s.indexOf("\n\n", text.getCaretPosition());
if (end < 0)
end = s.length();
s = s.substring(start, end);
if (s == null)
s = text.getText();
}
return s;
}
代码示例来源:origin: stackoverflow.com
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
final JTextArea jta = new JTextArea("Hello world\nHello world\nHello world");
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int pos = jta.getCaretPosition(); //get the cursor position
jta.insert("Some more", pos); //insert your text
}
});
frame.add(jta, BorderLayout.CENTER);
frame.add(btn, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
代码示例来源:origin: JGillam/burp-co2
private void showPrompt() {
if (cwd.equals(".")) {
txtConsole.append("laudanum> ");
} else {
txtConsole.append("laudanum[" + cwd + "]> ");
}
txtConsole.setCaretPosition(txtConsole.getText().length());
commandStart = txtConsole.getCaretPosition();
if (!txtConsole.hasFocus()) {
txtConsole.grabFocus();
}
}
代码示例来源:origin: com.github.fracpete/jshell-scripting
/**
* Processes the incoming line.
*
* @param line the line to process
* @param stdout whether stdout or stderr
*/
public void processOutput(String line, boolean stdout) {
boolean moveToEnd;
moveToEnd = (m_TextOutput.getDocument().getLength() == m_TextOutput.getCaretPosition());
m_TextOutput.append((stdout ? "[OUT] " : "[ERR] ") + line + "\n");
if (moveToEnd)
m_TextOutput.setCaretPosition(m_TextOutput.getDocument().getLength());
}
代码示例来源:origin: apache/axis2-java
private void setParameter(ParameterObj obj){
int position = desArea.getCaretPosition();
System.out.println(desArea.getLineCount());
System.out.println(desArea.getCaretPosition());
String str = " <parameter name=\"" + obj.getName() + "\" locked=\"false\">"
+ obj.getValue() +
"</parameter>\n";
desArea.insert(str, position + 1);
}
private void setModule(String module){
代码示例来源:origin: pvto/konte-art
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
int ind = findNext(target.getCaretPosition(), false, findF.getText());
if (ind >= 0)
{
target.setCaretPosition(ind);
target.setSelectionStart(ind);
target.setSelectionEnd(end == ind ? ind + findF.getText().length()+1 : end);
}
else
{
target.setCaretPosition(0);
}
}//GEN-LAST:event_jButton1ActionPerformed
代码示例来源:origin: pvto/konte-art
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
int ind = findNext(target.getCaretPosition(), true, findF.getText());
if (ind >= 0)
{
target.setCaretPosition(ind);
target.setSelectionStart(ind);
target.setSelectionEnd(end);
}
else
{
target.setCaretPosition(0);
}
}//GEN-LAST:event_jButton2ActionPerformed
代码示例来源:origin: stackoverflow.com
public Point getCaretPixelPosition(JTextArea a) {
try {
int cpos = a.getCaretPosition();
Font font = a.getStyledDocument().getFont(a.getStyledDocument().getLogicalStyle(cpos));
FontMetrics metrics = getFontMetrics(font);
int lineNum = a.getLineOfOffset(cpos);
int y = lineNum * metrics.getHeight();
int lineStart = a.getLineStartOffset(lineNum);
int x = metrics.stringWidth(a.getText().substring(lineStart, cpos));
return new Point(x,y);
} catch(BadLocationException e) {}
return null;
}
代码示例来源:origin: Vhati/Slipstream-Mod-Manager
private void updateCaretStatus() {
JTextArea currentArea = getCurrentArea();
if ( currentArea == null ) return;
try {
int offset = currentArea.getCaretPosition();
int line = currentArea.getLineOfOffset( offset );
int lineStart = currentArea.getLineStartOffset( line );
int col = offset - lineStart;
int lineCount = currentArea.getLineCount();
statusLbl.setText( String.format( "Line: %4d/%4d Col: %3d", line+1, lineCount, col+1 ) );
}
catch ( BadLocationException e ) {
statusLbl.setText( String.format( "Line: ???/ ??? Col: ???" ) );
}
}
代码示例来源:origin: raydac/netbeans-mmd-plugin
@Override
public void keyTyped(@Nonnull final KeyEvent e) {
if (lockIfNotDisposed()) {
try {
if (config.isKeyEvent(MindMapPanelConfig.KEY_TOPIC_TEXT_NEXT_LINE, e)) {
e.consume();
textEditor.insert("\n", textEditor.getCaretPosition()); //NOI18N
} else if (e.getKeyChar() == KeyEvent.VK_ENTER && (e.getModifiers() & ALL_SUPPORTED_MODIFIERS) == 0) {
e.consume();
endEdit(true);
}
} finally {
unlock();
}
}
}
代码示例来源:origin: stackoverflow.com
JTextArea txt = ...;
int caretOffset = txt.getCaretPosition();
int lineNumber = txt.getLineOfOffset(caretOffset);
int startOffset = txt.getLineStartOffset(lineNumber);
int endOffset = txt.getLineEndOffset(lineNumber);
txt.replaceRange("Replaced Text", startOffset, endOffset);
代码示例来源:origin: JGillam/burp-co2
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
int currentPosition = txtConsole.getCaretPosition();
try {
String command = txtConsole.getText(commandStart, (currentPosition - commandStart - 1));
runCommand(command, false);
} catch (BadLocationException e1) {
callbacks.printError(e1.toString());
} catch (MalformedURLException e1) {
callbacks.printError(e1.toString());
} catch (NumberFormatException e1) {
callbacks.printError(e1.toString());
}
} else {
super.keyReleased(e);
}
}
内容来源于网络,如有侵权,请联系作者删除!