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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:47:04+00:00 2026-05-27T20:47:04+00:00

I am having problems with a popup menu on a JTable and the fact

  • 0

I am having problems with a popup menu on a JTable and the fact that this JTable allows for Multiple Interval Selection.
I’m going to explain in detail my situation, making it as clear as possible, hopefully.

I have a basic data class, lets call it Item, with a string id (name) and two boolean fields, online and active (with relative getters).
The idea behind the JTable is that, for each item in the dataset, it will show its name in the first colum and its status in the second column, where by ‘status’ I mean that, it will show “ACTIVE/NOT ACTIVE” if the Item is Online, otherwise it will show “OFFLINE”.
I have implemented a TableModel that does the job and it works.

I also want, when the user right clicks on a row, a popup to appear (if the selected Item is ONLINE) allowing to Activate/Deactivate the item, depending on its status.
This worked perfectly as long as the Selection Model was SINGLE SELECTION, but when I changed it to MULTIPLE INTERVALS SELECTION, I could not make it work properly anymore.

The behaviour that I want is that, on right-click, a popup appears where the click is performed, the row is added to the selection and highlighted and all the previously selected rows stay selected! This I cannot manage to do!

Here is the code I have in the MouseListener:

tblModel.addMouseListener(new MouseAdapter() {
    void showPopup(MouseEvent e){
            int r = tblModel.rowAtPoint(e.getPoint());
            if (r >= 0 && r < tblModel.getRowCount()) {
                    //tblModel.setRowSelectionInterval(r, r);
                } else {
                    tblModel.clearSelection();
                }   
            int[] viewRowIndexes = tblModel.getSelectedRows();          
            int rowViewIndex = tblModel.getSelectedRow();

            if (rowViewIndex < 0)
                return;

            int rowModelIndex = tblModel.convertRowIndexToModel(rowViewIndex);
            if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
                Action changeActiveAction;
                Action changeInactiveAction;
                List<String> actives = new ArrayList<String>();
                List<String> inactives = new ArrayList<String>();
                DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) tblModel.getSelectionModel();

                for (int viewRowIndex : viewRowIndexes) {
                    int modelRowIndex = tblModel.convertRowIndexToModel(viewRowIndex);
                    if (selectionModel.isSelectedIndex(viewRowIndex)) {
                        boolean online = ((MyTableModel) tblModel.getModel()).isItemOnline(modelRowIndex);
                        if (!online)
                            continue;

                        boolean active = ((MyTableModel) tblModel.getModel()).isItemActive(modelRowIndex);
                        String idItem = (String) ((MyTableModel) tblModel.getModel()).getValueAt(modelRowIndex,0);
                        if (active) {
                            actives.add(idItem);
                        } else {
                            inactives.add(idItem);
                        }
                    }
                }

                if (actives.size() > 0 || inactives.size() > 0) {
                    popup = new JPopupMenu();
                    if (actives.size() > 0) {
                        changeActiveAction = new ChangeAction("Deactivate ACTIVE Items","This will deactivate all the selected ACTIVE items",actives, false);
                        popup.add(new JMenuItem(changeActiveAction));
                    }
                    if (inactives.size() > 0) {
                        changeInactiveAction = new ChangeAction("Activate INACTIVE Items","This will activate all the selected INACTIVE items",inactives, true);
                        popup.add(new JMenuItem(changeInactiveAction));
                    }
                    popup.show(e.getComponent(), e.getX(),e.getY());
                }
            }
    }
    @Override
    public void mousePressed(MouseEvent e) {
        showPopup(e);
    }

    @Override   
    public void mouseReleased(MouseEvent e) {   
        showPopup(e);   
    }
};

The behaviour is functionally correct, but the selection of rows is not working.
Having commented the line

//tblModel.setRowSelectionInterval(r, r);

when I right-click on a row, a popup appears, but it ignores the row on which I clicked.

On the other hand, if uncommented, that line will select only the clicked row, losing all the rest of the selection….

I am sorry for the long post, but I didn’t know how to explain my problem without giving all the details of my situation….
Hopefully this is a trivial thing and you can tell me how I can fix/change it.

Thank you in advance.

  • 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-27T20:47:04+00:00Added an answer on May 27, 2026 at 8:47 pm

    One part of the answer is:

    if (tblModel.isSelectedIndex(r)) {
       tblModel.removeSelectionInterval(r, r);
    } else {
       tblModel.addSelectionInterval(r, r);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im having problems starting a codeigniter project, the problem is that when i do
I'm having problems using textures that are larger than the OpenGL window or the
Having problems with IE8... I have a button that onclick fires the showImageBrowser() function.
I am having two problems. First: Trying to submit this form using jquery. When
I'm having a few problems with an application that integrates sharepoint, SQL reporting services
Okay, I'm having serious problems here. On my own computer, this code works just
I feel like this should be an easy task, but I am having problems.
I'm having a problem with using a TStringGrid and Popup menu I want to
Having problems with a small awk script, Im trying to choose the newest of
Having problems iterating. Problem has to do with const correctness, I think. I assume

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.