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 8764345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:00:42+00:00 2026-06-13T16:00:42+00:00

I want to display a small context menu when a user right clicks a

  • 0

I want to display a small context menu when a user right clicks a table row in my application. My plan was to use a custom made MouseListener for this that calls the show() method. Here is my code:

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

class TableMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
    JTable table = (JTable)(e.getSource());
    Point p = e.getPoint();
    int row = table.rowAtPoint(p);
    int col = table.columnAtPoint(p);

// The autoscroller can generate drag events outside the Tables range.
if ((col == -1) || (row == -1)) {
        return;
}

    if (SwingUtilities.isRightMouseButton(e)) {
        JPopupMenu pop = new JPopupMenu("Row "+row);
        JMenuItem menu1 = new JMenuItem("Wijzigen");
        menu1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //do things, still have to make that
            }
        });
        pop.show(menu1, p.x, p.y);

    }
}
}

Now my problem is: when i run my application, and I right click a table row, it pops out this error:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source)
at java.awt.Component.getLocationOnScreen(Unknown Source)
at javax.swing.JPopupMenu.show(Unknown Source)
at TableMouseListener.mousePressed(TableMouseListener.java:34)

What exactly is going wrong here?

  • 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-13T16:00:43+00:00Added an answer on June 13, 2026 at 4:00 pm
    • JMenuItem isn’t added to the JPopup, then JMenuItem shoudn’t be isDisplayable

    • prepare this container, dont’t to create on fly ,

    • create whole popup container as local variable or as returns from class, void, whatever

      a) prepare with JMenuItems too

      b) override maybeShowPopup, then there you can manage whatever (must be done on EDT)

    • rest of importantn methods

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

Sidebar

Related Questions

I want to display a small view that does not cover the whole screen
I'm want to display different image version: first article: big banner second: small banner
I want to write a small Android app with a GUI that should display
I have a simple quiz application and I want display a nice timer /
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
Hello everyone I'm writing a small asp.net mvc application and i want to make
I want to try out a small, custom ValueConverter in a class which after
I am trying to display a small web content in my application. Is there
After the user clicks a link, I want to load the corresponding page within
I want display data from database in Listbox...Here is my code, It is not

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.