Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear table selection when clicking on right of table

Tags:

java

focus

swing

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.

like image 836
eternaln00b Avatar asked Jan 17 '23 12:01

eternaln00b


2 Answers

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();

like image 95
cdc Avatar answered Jan 28 '23 05:01

cdc


You could try adding a global mouse listener like below:

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener(){
            @Override
            public void eventDispatched(AWTEvent event) {
                if(event.getID() == MouseEvent.MOUSE_CLICKED) {
                    MouseEvent mevent = (MouseEvent) event;
                     int row = t.rowAtPoint(mevent.getPoint());
                     if(row == -1) {
                         t.clearSelection();
                     }
                }               
            }           
        }, AWTEvent.MOUSE_EVENT_MASK);
like image 32
Ashwinee K Jha Avatar answered Jan 28 '23 04:01

Ashwinee K Jha