I have JTabbedPane and each tab has a JTextPane.
Each JTextPane has a popup-menu but I want all of them to share the exact same popup-menu. Why? Because when I switch tabs, I want the same options to be highlighted on each popup.
How can I do this? I tried adding a static PopupMenu instance to each pane but when I add it to one, it disappears from the others..
EDIT: In the following SSCCE, when the checkbox is checked on TabOne, it is not Checked on TabTwo. I want it Checked On BOTH tabs. All the other options can be unique to each tab except for that one.
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());
}
}
});
}
}
}
If you want to share an Object then you need to create the Object and pass the Object as a parameter to your other classes. Something like:
JPopupMenu popup = createSharedPopupMenu();
Pane.addTab("TabOne", M.new DebugBox(500, 500, popup));
Pane.addTab("TabTwo", M.new DebugBox(500, 500, popup));
Then you need to create generic Actions that will work on any text component. The DefaultEditorKit provides some of these Actions for you:
public JPopupMenu createSharedPopupMenu()
{
JPopupMenu popup = new JPopupMenu()
JMenuItem copy = new JMenuItem( new DefaultEditorKit.CopyAction() );
popup.add( copy );
...
return popup.
}
If the editor kit doesn't provide an Action for you then you need to create your own and you should extend TextAction
instead of AbstractAction. The TextAction class has a method that will return the text component that has focus. So you can implement the Action in a generic way.
Also you don't need to use a MouseListener to invoke the popup. You can just use the following JComponent method:
JComponent.setComponentPopupMenu(JPopupMenu popup)