import com.jniwrapper.win32.jexcel.*; import com.jniwrapper.win32.jexcel.ui.JWorkbook; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * The sample demonstrates how to add a custom context menu instead of the default one to a worksheet. */ public class ContextMenuSample extends JFrame { private final ContextMenu contextMenu = new ContextMenu(); public ContextMenuSample() throws Exception { super("Context Menu sample"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 600); final JWorkbook workbook; workbook = new JWorkbook(); setContentPane(workbook); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { workbook.close(); } }); workbook.getWorksheet(1).setEventHandler(new WorksheetEventHandler() { @Override public boolean beforeDoubleClick(WorksheetEventObject worksheetEventObject) { return true; } @Override public boolean beforeRightClick(WorksheetEventObject worksheetEventObject) { try { Point currentPointerLocation = MouseInfo.getPointerInfo().getLocation(); SwingUtilities.convertPointFromScreen(currentPointerLocation, workbook); contextMenu.show(workbook, currentPointerLocation.x, currentPointerLocation.y); } catch (Exception e) { e.printStackTrace(); } return false; } }); } public static void main(String[] args) throws Exception { new ContextMenuSample().setVisible(true); } public static class ContextMenu extends JPopupMenu { JMenuItem anItem; public ContextMenu(){ anItem = new JMenuItem("Click Me!"); anItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Menu item clicked"); } }); add(anItem); } } } |
Samples >