gpt4 book ai didi

java - 右键单击选择文本 JTextPane

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:31:23 24 4
gpt4 key购买 nike

在我的 JTextPane 中,当我选择文本并单击鼠标右键时;它提供了复制文本的选项。下面是代码:

public LogPane() {
super();
JPopupMenu pop = new JPopupMenu();
final LogPane l = this;
JMenuItem copy = new JMenuItem("Copy CTRL+C");
copy.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
selected = l.getSelectedText();
if(selected==null)
return;
StringSelection clipString = new StringSelection(selected);
clipbd.setContents(clipString,clipString);
}

});
pop.add(copy);
copy.setEnabled(true);
}

所以在右键单击时,它会提供复制文本的选项。但我想要的是,当没有选择文本并且用户右键单击时 - 不应显示复制选项。应该如何合并更改?

最佳答案

PopupMenuListener 应该可以解决问题。

public LogPane() {
super();
JPopupMenu pop = new JPopupMenu();
final LogPane l = this;
final JMenuItem copy = new JMenuItem("Copy CTRL+C");
copy.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
selected = l.getSelectedText();
if(selected==null)
return;
StringSelection clipString = new StringSelection(selected);
clipbd.setContents(clipString,clipString);
}

});
pop.add(copy);
pop.addPopupMenuListener(new PopupMenuListener() {
public void popupMenuCanceled(PopupMenuEvent e) {}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
copy.setEnabled(l.getSelectedText() != null);
}
});
}

关于java - 右键单击选择文本 JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com