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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:14:57+00:00 2026-05-25T22:14:57+00:00

Is there any clean way to allow a user to select multiple non continuos

  • 0

Is there any clean way to allow a user to select multiple non continuos cells of a JTable?
Or I’m forced to implement my own ListSelectionModel?

I played around with setCellSelectionEnabled() and setSelectionModel() methods on JTable but I can only select groups of continuous cells.

EDIT:

I tried @mKorbel nice SSCCE. It works fine for list but it seems not fully working on tables. Here’s an SSCCE:

import java.awt.Component;

import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class TableSelection extends JFrame{
    String[] columnNames = {"First Name",
            "Last Name",
            "Sport",
            "# of Years",
            "Vegetarian"};
    Object[][] data = {
            {"Kathy", "Smith",
             "Snowboarding", new Integer(5), new Boolean(false)},
            {"John", "Doe",
             "Rowing", new Integer(3), new Boolean(true)},
            {"Sue", "Black",
             "Knitting", new Integer(2), new Boolean(false)},
            {"Jane", "White",
             "Speed reading", new Integer(20), new Boolean(true)},
            {"Joe", "Brown",
             "Pool", new Integer(10), new Boolean(false)}
        };

    public TableSelection(){
        JPanel main= new JPanel();
        JTable table = new JTable(data, columnNames){
             @Override
                protected void processMouseEvent(MouseEvent e) {
                    int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
                    // change the modifiers to believe that control key is down
                    int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
                    // can I use this anywhere?  I don't see how to change the modifiersEx of the MouseEvent
                    MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers, e.getX(),
                            e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
                    super.processMouseEvent(myME);
                }

        };
        JScrollPane pane = new JScrollPane(table);
        main.add(pane);
        this.add(main);

        this.setSize(800, 600);
        this.setVisible(true);
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TableSelection();
    }

}

I can select non-contiguous row but not single cells. I mean, I would like to be able to select cell 0,0 and 3,3 for example.

  • 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-25T22:14:57+00:00Added an answer on May 25, 2026 at 10:14 pm
    1. If isn’t defined for JTable#setSelectionMode(ListSelectionModel.SINGLE_SELECTION), then CTRL + MOUSE_CLICK

    2. Or do you mean remember last selected?

    3. ListSelectionModel is used by both JTable and JList.

    enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

    import java.awt.Component;
    import java.awt.event.InputEvent;
    import java.awt.event.MouseEvent;
    import javax.swing.*;
    
    public class Ctrl_Down_JList {
    
        private static void createAndShowUI() {
            String[] items = {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"};
            JList myJList = new JList(items) {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                protected void processMouseEvent(MouseEvent e) {
                    int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
                    // change the modifiers to believe that control key is down
                    int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
                    // can I use this anywhere?  I don't see how to change the modifiersEx of the MouseEvent
                    MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers, e.getX(),
                            e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
                    super.processMouseEvent(myME);
                }
            };
            JFrame frame = new JFrame("Ctrl_Down_JList");
            frame.getContentPane().add(new JScrollPane(myJList));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowUI();
                }
            });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to clean up this type of loop using LINQ? List<Car>
Is there any easy/general way to clean an XML based data source prior to
Are there any automated tools that will parse through XAML code and clean out
Is there any way to check whether a file is locked without using a
Is there any way to clear the STDIN buffer in Perl? A part of
Is there any clear documentation on the binary formats used to serialize the various
Is there any option to clear the console screen in Java as clrscr() in
Is there any equivalent function of memset for vectors in C++ ? (Not clear()
Is there any free or commercial component written in .NET (no COM interop) that
Is there any query which can return me the number of revisions made to

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.