• Post Reply Bookmark Topic Watch Topic
  • New Topic

Right click in text editor  RSS feed

 
matt gimbl
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I basically have a text editor program that uses a JEditorPane for the input portion of the app. I want to have a JPopupMenu(?) so when the user right clicks on the JEditorPane, a menu will pop up with commands like cut, copy, past, etc. ... your normal right click commands. I also added the JEditorPane to a JScrollPane.
My problem is I can get either the JEditorPane or the JPopupMenu, but not both at the same time. I use:
scrollPane.setViewportView(textBox); // add the textbox to the scrollpane
getContentPane().add(scrollPane); // add the scrollpane to the gui
(where scrollPane is a JScrollPane and textBox is the JEditorPane).
I have tried textBox.add(new MouseMenu()); and scrollPane.add(new MouseMenu()); .... but those commands do nothing.
(where MouseMenu() is the JPopupMenu).
if I do:
getContentPane().add(textScrollPane);
getContentPane().add(new MouseMenu());
only the mouse menu shows and I get a gray area where the JEditorPane should be.
Can someone help me so I can get both of the things at once. Is JPopupMenu the right thing to do? Should I add a MouseClick listener to the scrollPane to detect a right click and then create a new MouseMenu()? I dont know what to do.
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your code, i see scrollPane and I see textScrollPane. Do you have two scrollPanes?
It should go something like this:

If this does not work, then post your MouseMenu code.
Good Luck!
 
matt gimbl
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got this where I create my GUI: ----------------------------
// Initialize variables
Container c = getContentPane();
textBox = new JEditorPane();
textBox.setDragEnabled(true);
// Adds popupmenu to textBox
textBox.add(new MouseMenu());
// Adds everything to the container
c.add(toolBar, BorderLayout.NORTH); // No problem with this
c.add(new JScrollPane(textBox), BorderLayout.CENTER);
c.add(statusBar, BorderLayout.SOUTH); // No problem with this either
This is my MouseMenu(): ----------------------------------------
public class MouseMenu extends JPanel {
private JPopupMenu popup;
private JMenuItem item;
public MouseMenu() {
popup = new JPopupMenu();
add(new JMenuItem("test"));
add(new JMenuItem("test2"));
popup.setInvoker(this);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
}
}
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not have to add your JPopupMenu to a JPanel. Just extend the JPopupMenu. Also you added the JMenuItems to the JPanel and not to the JPopupMenu.
Here's what you need:

I didn't test this so look for typo's. But this is what you need.
 
matt gimbl
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally got it to work ... had to go about it a different way.
In my gui .. I declare this:
textBox.addMouseListener(new MouseMenu());
In MouseMenu() ... I declare this:
public class MouseMenu extends MouseAdapter {
private JPopupMenu menu;
private JMenuItem item;
public MouseMenu() {
menu = new JPopupMenu();
item = new JMenuItem("Test1");
menu.add(item);
}
public void mouseReleased(MouseEvent e) {
showPopup(e);
}
public void mousePressed(MouseEvent e) {
showPopup(e);
}
private void showPopup(MouseEvent e) {
if (e.isPopupTrigger() && menu != null) {
GUI.setText(e.toString());
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
}
Of course I could go without calling showPopup() but I am lazy and would rather only write the code once there instead of twice in both listeners.
Thanks for all the help and ideas!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!