[英]How can i get caret position in Java
I want to have the X and Y position of the caret of my JTextPane.我想要我的 JTextPane 的插入符号的 X 和 Y position。
Here is what i have now:这是我现在拥有的:
Rectangle2D rectangle = textPane.modelToView2D(textPane.getCaretPosition());
popupMenu.show(frame, (int)rectangle.getX(), 80 + (int)rectangle.getY());
This code is from: https://stackoverflow.com/a/18864392/14911094此代码来自: https://stackoverflow.com/a/18864392/14911094
But there is a problem with it!但是它有一个问题!
This is ok but i also have a scroll pane with the JTextPane (this is code for the JFrame constructor):这没关系,但我也有一个带有 JTextPane 的滚动窗格(这是 JFrame 构造函数的代码):
editorPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(editorPane);
this.setLayout(new GridLayout(1, 1, 0, 0));
this.add(scrollPane);
And the code to show the popupmenu is being called by another function in a diff class:另一个 function 在差异 class 中调用了显示弹出菜单的代码:
popupMenu.removeAll();
for (String item : stringItems)
popupMenu.add(new JMenuItem(item));
Rectangle2D rectangle = editorPane.modelToView2D(editorPane.getCaretPosition());`
popupMenu.show(frame, (int)rectangle.getX(), 80 + (int)rectangle.getY());
popupMenu.updateUI();
So when i get down to lower lines the popup moves out of screen as the rectangle.getY() is very high.因此,当我下降到较低的行时,弹出窗口会移出屏幕,因为 rectangle.getY() 非常高。
How can i solve this problem!我怎么解决这个问题!
Here is a minimal reproducible example:这是一个最小的可重现示例:
public class Try {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame();
JTextPane textPane = new JTextPane();
JScrollPane pane = new JScrollPane(textPane);
JPopupMenu popupMenu = new JPopupMenu();
textPane.setText("SOME RANDOME TEXT FHDFFHNGHNFKJ!");
textPane.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
try {
Rectangle2D rectangle = textPane.modelToView2D(textPane.getCaretPosition());
popupMenu.show(frame, (int) rectangle.getX(), 80 + (int) rectangle.getY());
} catch (Exception ex) {}
frame.requestFocus();
frame.requestFocusInWindow();
textPane.requestFocusInWindow();
}
});
String itms[] = {
"HI",
"Hello"
};
ArrayList < String > items = new ArrayList < > (Arrays.asList(itms));
for (String item: items)
popupMenu.add(new JMenuItem(item));
frame.add(pane);
frame.setSize(900, 500);
frame.setVisible(true);
}
}
The answer was suggested by https://stackoverflow.com/users/131872/camickr is the comments! https://stackoverflow.com/users/131872/camickr建议的答案是评论!
The problem with my code was that I was showing the popupmenu with respect to the frame instead of the textPane我的代码的问题是我显示的是相对于框架而不是 textPane 的弹出菜单
Here would be a solution.这将是一个解决方案。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
public class Try {
public static final int CARET_HEIGHT = 15;
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame();
JTextPane textPane = new JTextPane();
JScrollPane pane = new JScrollPane(textPane);
JPopupMenu popupMenu = new JPopupMenu();
textPane.setText("SOME RANDOME TEXT FHDFFHNGHNFKJ!");
textPane.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
try {
Rectangle2D rectangle = ((JTextPane)(e.getSource())).modelToView2D(textPane.getCaretPosition());
popupMenu.show(((JTextPane)(e.getSource())), (int) rectangle.getX(), CARET_HEIGHT + (int) rectangle.getY());
} catch (Exception ex) {}
frame.requestFocus();
frame.requestFocusInWindow();
textPane.requestFocusInWindow();
}
});
String itms[] = {
"HI",
"Hello"
};
ArrayList < String > items = new ArrayList < > (Arrays.asList(itms));
for (String item: items)
popupMenu.add(new JMenuItem(item));
frame.add(pane);
frame.setSize(900, 500);
frame.setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.