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

The Archive Base Latest Questions

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

I was looking for this answer in here but didn’t find it, so I’m

  • 0

I was looking for this answer in here but didn’t find it, so I’m writing my own answer below if someone need it.

Feel free to correct anything if needed.

  • 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-13T18:42:36+00:00Added an answer on June 13, 2026 at 6:42 pm

    So, in order to make a solution for both cases (JList and JTable) I made a custom listener that will be applied to the lists and table in question.

    The listener is :

    public class DeselectOnClickListener extends MouseAdapter
    {   
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            clearselection(e);      
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            clearselection(e);
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);
            clearselection(e);
        }
    
        public void clearselection (MouseEvent e){      
            if (e.getComponent() instanceof JTable){
                Point pClicked = e.getPoint();
                JTable table = (JTable) e.getSource();
                int index = table.rowAtPoint(pClicked);
                if (index == -1){
                    table.clearSelection();
                }
    
            } else if (e.getComponent() instanceof JList){
                Point pClicked = e.getPoint();
                JList<?> list = (JList<?>) e.getSource();
                int index = list.locationToIndex(pClicked);
                Rectangle rec = list.getCellBounds(index, index);
                if (rec==null || !rec.contains(pClicked)){
                    list.clearSelection();
                }
            }
        }
    }
    

    Notice that this works well in SINGLE_SELECCTION, but if we want to use MULTIPLE_INTERVAL_SELECTION or SINGLE_INTERVAL_SELECTION I would recommend not to override mouseReleased(MouseEvent e) as it will clear selection if the mouse button is released outside the selection range.

    PD: for this last point I’ve done the following fix:

    @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);
            if (! isIntervalSelectionMode(e)){
                clearselection(e);
            }
        }
    
        public boolean isIntervalSelectionMode (MouseEvent e){
            boolean isIntervalSelection = false;
            if (e.getComponent() instanceof JTable){
                isIntervalSelection = ((JTable)e.getSource()).getSelectionModel().getSelectionMode() 
                            != ListSelectionModel.SINGLE_SELECTION;
            }else if (e.getComponent() instanceof JList){
                isIntervalSelection = ((JList<?>)e.getSource()).getSelectionModel().getSelectionMode() 
                        != ListSelectionModel.SINGLE_SELECTION;
            }
            return isIntervalSelection;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried looking this up on here but couldnt find a proper answer. I
I've been looking for this, but can't find the answer. I have an NSMutableArray
I saw variants of this question before, but didn't find answer yet. I have
I have been looking around for this but couldn't find an answer anywhere, so
I am getting this question due to another answer on here, but didn't explain
I have been looking for an answer to this on Stack Overflow, but I
I have been looking for an answer to this and could not find it
I've been looking for an existing answer to this question, but haven't found one.
Using Java: I didn't want to waste peoples time and post this here, but
I know this is similar to another stack overflow post but it didn't answer

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.