/**
*
*/
package de.tutorials;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
/**
* @author Tom
*
*/
public class PopupMenuInJEditorPaneExample extends JFrame {
public PopupMenuInJEditorPaneExample() {
super("PopupMenuInJEditorPaneExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JEditorPane editorPane = new JEditorPane();
final JPopupMenu popupMenu = new JPopupMenu("ExampleActions");
popupMenu.add(new AbstractAction("Foo") {
public void actionPerformed(ActionEvent e) {
System.out.println("foo action");
}
});
popupMenu.add(new AbstractAction("Bar") {
public void actionPerformed(ActionEvent e) {
System.out.println("bar action");
}
});
editorPane.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
JScrollPane scrollPane = new JScrollPane(editorPane);
scrollPane.setPreferredSize(new Dimension(320, 240));
add(scrollPane);
pack();
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new PopupMenuInJEditorPaneExample();
}
}