I found a popup menu program and it works standalone. And then I made another program "Note", within which I hope to add the popup menu functionality into.
The popup menu part now is below:
package my.demo;
// The original code is from link: http://www.java2s.com/Code/Java/Swing-JFC/AsimpleexampleofJPopupMenu.htm
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
public class PopupMenuExample extends JPanel {
public JPopupMenu popup;
public PopupMenuExample() {
popup = new JPopupMenu();
ActionListener menuListener;
menuListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("Popup menu item ["
+ event.getActionCommand() + "] was pressed.");
}
};
JMenuItem item;
popup.add(item = new JMenuItem("Left", new ImageIcon("1.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Center", new ImageIcon("2.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Right", new ImageIcon("3.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Full", new ImageIcon("4.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.addSeparator();
popup.add(item = new JMenuItem("Settings . . ."));
item.addActionListener(menuListener);
popup.setLabel("Justification");
popup.setBorder(new BevelBorder(BevelBorder.RAISED));
popup.addPopupMenuListener(new PopupPrintListener()); // listener of Popup menu
addMouseListener(new MousePopupListener()); // listener of mouse
}
// An inner class to check whether mouse events are the popup trigger
class MousePopupListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
checkPopup(e);
}
@Override
public void mouseClicked(MouseEvent e) {
checkPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
checkPopup(e);
}
private void checkPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(PopupMenuExample.this, e.getX(), e.getY());
}
}
}
// An inner class to show when popup events occur
class PopupPrintListener implements PopupMenuListener {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
System.out.println("Popup menu will be visible!");
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
System.out.println("Popup menu will be invisible!");
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
System.out.println("Popup menu is hidden!");
}
}
}
And the "Note" code is below (partial):
package my.demo;
import java.awt.event.*;
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import javax.swing.*;
/**
*
* @author root
*/
public class MyNoteUI extends javax.swing.JFrame {
JFrame jFrame;
JFileChooser fc;
/**
* Creates new form MyNoteUI
*/
public MyNoteUI() {
initComponents();
jFrame = new javax.swing.JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setContentPane(new PopupMenuExample());
jFrame.setTitle("My NoteUI");
}
But with these codes the "Note" can work normally while the popup menu doesn't work. I guess the jFrame related code is not correct, but I don't know how to correct it. Who can help? Thanks!
Plus: I used NetBeans to make the project and below is the compilation info (it is hard to understand).
/home/tomxue/mycode/0___GitHub/MyNote/nbproject/build-impl.xml:1026: The following error occurred while executing this line:
/home/tomxue/mycode/0___GitHub/MyNote/nbproject/build-impl.xml:853: taskdef class org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs cannot be found
using the classloader AntClassLoader[]
BUILD FAILED (total time: 0 seconds)
I found the solution as below. The key is that no JFrame instance can be found while it is there already, this means no "new" needed and just call its functions. And notice the sequence: setContentPane() and then initComponents().
public class MyNoteUI extends javax.swing.JFrame {
JFileChooser fc;
/**
* Creates new form MyNoteUI
*/
public MyNoteUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(new PopupMenuExample());
setTitle("My NoteUI");
initComponents();
}