我有JTabbedPane,每个标签都有一个JTextPane。
每个JTextPane都有一个弹出菜单,但我希望所有这些菜单共享完全相同的弹出菜单。为什么?因为当我切换标签时,我希望在每个弹出窗口中突出显示相同的选项。
我该怎么做?我尝试在每个窗格中添加一个静态PopupMenu实例,但是当我将其添加到一个窗格时,它会从其他窗格中消失。
编辑:在下面的SSCCE中,当在TabOne上选中复选框时,它不会在TabTwo上选中。我想要它勾选两个标签。除了那个选项卡之外,所有其他选项对于每个选项卡都是唯一的。
SSCCE:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
public class Main {
public static void main(String[] Args) {
final Main M = new Main();
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JFrame F = new JFrame("SSCCE");
JTabbedPane Pane = new JTabbedPane();
Pane.addTab("TabOne", M.new DebugBox(500, 500));
Pane.addTab("TabTwo", M.new DebugBox(500, 500));
F.setLayout(new BorderLayout());
F.add(Pane, BorderLayout.NORTH);
F.pack();
F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
F.setVisible(true);
}
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}
}
public class DebugBox extends JTextPane {
private JScrollPane ScrollPane = null;
private final JPopupMenu Menu = new JPopupMenu();
private static final long serialVersionUID = 7731036968185936516L;
public DebugBox(int Width, int Height) {
this.ScrollPane = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.setPreferredSize(new Dimension(Width, Height));
JCheckBoxMenuItem Debug = new JCheckBoxMenuItem(new AbstractAction() {
private static final long serialVersionUID = -336209978671944858L;
@Override
public void actionPerformed(ActionEvent e) {
//DO something here that affects ALL the DebugBoxes because they all share this Menu Option somehow :S
//Change name of this menu option, all instances have their names changed too.
}
});
JMenuItem Copy = new JMenuItem(new AbstractAction() {
private static final long serialVersionUID = -6774461986513304498L;
@Override
public void actionPerformed(ActionEvent e) {
DebugBox.this.copy();
}
});
JMenuItem Clear = new JMenuItem(new AbstractAction() {
private static final long serialVersionUID = -5567371173360543484L;
@Override
public void actionPerformed(ActionEvent e) {
DebugBox.this.setText(null);
}
});
JMenuItem SelectAll = new JMenuItem(new AbstractAction() {
private static final long serialVersionUID = -8792250195980016624L;
@Override
public void actionPerformed(ActionEvent e) {
DebugBox.this.selectAll();
}
});
this.Menu.add(Copy);
this.Menu.add(Clear);
this.Menu.add(SelectAll);
this.Menu.add(Debug);
Copy.setText("Copy");
Clear.setText("Clear");
SelectAll.setText("Select All");
Debug.setText("Show Debug Box");
this.setEditable(false);
this.add(this.Menu);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
DebugBox.this.Menu.show(DebugBox.this, e.getX(), e.getY());
}
}
});
}
}
}
答案 0 :(得分:2)
如果要共享Object,则需要创建Object并将Object作为参数传递给其他类。类似的东西:
JPopupMenu popup = createSharedPopupMenu();
Pane.addTab("TabOne", M.new DebugBox(500, 500, popup));
Pane.addTab("TabTwo", M.new DebugBox(500, 500, popup));
然后,您需要创建适用于任何文本组件的通用操作。 DefaultEditorKit为您提供了一些这些操作:
public JPopupMenu createSharedPopupMenu()
{
JPopupMenu popup = new JPopupMenu()
JMenuItem copy = new JMenuItem( new DefaultEditorKit.CopyAction() );
popup.add( copy );
...
return popup.
}
如果编辑器工具包没有为您提供操作,那么您需要创建自己的操作,并且应该扩展TextAction
而不是AbstractAction。 TextAction类有一个方法,它将返回具有焦点的文本组件。因此,您可以通用方式实现Action。
此外,您不需要使用MouseListener来调用弹出窗口。您可以使用以下JComponent方法:
JComponent.setComponentPopupMenu(JPopupMenu popup)