Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:37:45+00:00 2026-06-09T04:37:45+00:00

I found a popup menu program and it works standalone. And then I made

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:37:47+00:00Added an answer on June 9, 2026 at 4:37 am

    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();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this tutorial which pretty much does exactly what I want: http://webstutorial.com/jquery-popup-jquery-slide-popup/jquery Except
I found WaitForPageToLoad,WaitForCondition and popup. I have an ajax request which is creating some
I'd like to create a pop-up menu similar to the one found in the
I found a neat plugin that displays a popup balloon. Seems like it's intended
I have a dialog application in which I want to have clickable menu items
I am trying to create a popup menu and I keep getting java.lang.NoClassDefFoundError: android.widget.PopupMenu
I'm writing a program that, among other things, needs to display a context menu
In my main window, after clicking a menu item, a dialog box appears which
I finally found the perfect popup style box for my facebook application, However it
I am new to jquery ui autocomplete ! I found that the popup suggestion's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.