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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:59:26+00:00 2026-05-29T19:59:26+00:00

I have a JTable which is held in a scrollpane, which in turn sits

  • 0

I have a JTable which is held in a scrollpane, which in turn sits in a panel, which is embedded in a JFrame. If the JFrame is expanded, then there is empty space, both below, as well as to the right of the JTable.

I want to clear the selection on the table if the user clicks outside the table, either below the table, or to the right of the table.

In order to clear the selection when the user clicks BELOW the table, I configured the table to fill the height of the scrollpane viewport, and added a MouseListener to the table, so that when the user clicks below the table, “rowAtPoint” returns -1, and then I clear the selection. However, this doesn’t work for the RHS of the table. The table doesn’t even receive these Mouse Events. How should I detect a click on the right of the JTable and clear the selection on the table? See code below. Please note that I haven’t bothered to make the code pretty and do things the “right way”. My focus was just on creating a SSCCE that illustrated the issue. 🙂

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TableTest {
    public static void main(String args[]) {
        JFrame f = new JFrame();
        JPanel p = new JPanel(new BorderLayout());
        JScrollPane sp = new JScrollPane();
        final JTable t = new JTable(5,5);
        t.setFillsViewportHeight(true);
        t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        for(int i = 0; i < 5; i++) {
            t.getColumnModel().getColumn(i).setWidth(50);
            t.getColumnModel().getColumn(i).setMaxWidth(75);
        }
        t.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                int row = t.rowAtPoint(e.getPoint());
                if(row == -1) {
                    t.clearSelection();
                }

            }
        });
        sp.getViewport().add(t);
        p.add(sp, BorderLayout.CENTER);
        f.add(p);

        f.pack();
        f.setVisible(true);
    }
}

EDIT : I understand that I could probably just add a mouselistener to the scrollpane or panel as well, so that the table selection is cleared when I click on them. I’m just wondering if there’s a better / cleaner solution out there, to make sure that the JTable selection is cleared when I click outside its bounds.

  • 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-29T19:59:28+00:00Added an answer on May 29, 2026 at 7:59 pm

    Investigate using focus listener (table.addFocusListener( ... );) to find out when the table/cells no longer have the focus, and when that is the case call: table.getSelectionModel().clearSelection();

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

Sidebar

Related Questions

I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
Hi I an issue with editors in a JTable. I have a column which
I have a JTable whose associated TableModel could be initially empty. Therefore, it currently
I have a JTable which uses a custom TableModel to display a series of
I have a JTable using a custom DefaultTableModel which has some Booleans in the
I have a panel containing a number of components, one of which is a
I am using a jtable which have 8 rows by default. When I add
I have a JTable and a TableRowSorter which I'd like to perform an operation
I have a GUI which has a JComboBox and a JTable . The JComboBox
I have a JTable which has 1st row blank. Now when I sort the

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.