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

  • Home
  • SEARCH
  • 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 498993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:56:24+00:00 2026-05-13T05:56:24+00:00

I prepared a small test case below. My problem is when i right click

  • 0

I prepared a small test case below. My problem is when i right click on the window. JPopupMenu show up but if i click anywhere outside the JWindow menu does not disappear. I have to click somewhere on the window to get rid of it which is not the expected behavior.

EDIT:
after reading akf’s answer i switched to JFrame, when frame is in focus and pop up menu is showing it disappears when you click on another window. but if the window does not have focus and you click somewhere menu does not disappear.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class test {

    static class window extends JWindow
    implements MouseListener, MouseMotionListener{

    JPopupMenu popMenu;
    JPanel panel = new JPanel();

    Point location;
    MouseEvent pressed;

    public window(){

        addMouseListener( this );
        addMouseMotionListener( this );

        JLabel label = new JLabel("JWindow", JLabel.CENTER);

        initPopMenu();
        add(label);
        setVisible(true);
        setAlwaysOnTop(true);
        setLocationRelativeTo(null);
        pack();
    }

    public void initPopMenu(){
        popMenu = new JPopupMenu();
        JMenuItem item;

        item = new JMenuItem( "Title" );
        item.setEnabled(false);
        popMenu.add(item);
        popMenu.addSeparator();

        item = new JMenuItem( "Item One" );
        popMenu.add(item);

        item = new JMenuItem( "Item 2" );
        popMenu.add(item);

        item = new JMenuItem( "Item 3" );
        popMenu.add(item);
    }

    public void mousePressed(MouseEvent e)
    {
        pressed = e;
        int nModifier = e.getModifiers();
        if (((nModifier & InputEvent.BUTTON2_MASK) != 0)||
        ((nModifier & InputEvent.BUTTON3_MASK) != 0))
        popMenu.show( this, e.getX(), e.getY() );
    }

    public void mouseClicked(MouseEvent e) {
    }


    public void mouseReleased(MouseEvent e) {}

    public void mouseDragged(MouseEvent me){
    }

    public void mouseMoved(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    }
    public static void main(String[] args) {
    window dw = new window();
    }
}
  • 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-05-13T05:56:24+00:00Added an answer on May 13, 2026 at 5:56 am

    Take a look at the Java Doc for JWindow.isFocusableWindow
    A JWindow cannot be the focused window unless it has an owner and the owner is visible.
    You’re using the default constructor, so your JWindow has the shared owner asn is not focusable. When it is not focusable, it cannot detect the loss of focus when you click somewhere else.

    I changed JWindow to JFrame and added a call to setUndecorated(true); before the call to setVisible and it’s working for me. If these changes do not make it work for you, please post the version of Java you are using: java -fullversion

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use placement new: new (m_data) T(); Placement new is really… May 14, 2026 at 7:19 pm
  • Editorial Team
    Editorial Team added an answer First, the implicit operator allows for implcit conversions (no casting… May 14, 2026 at 7:19 pm
  • Editorial Team
    Editorial Team added an answer gethostbyname echo gethostbyname('stackoverflow.com'); // 69.59.196.211 You can use parse_url to… May 14, 2026 at 7:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.