2

I am writing an application that uses a JTable and I would like to be able to select individual cells so I can interact with the selected cells. The problem is that when I select a certain number of cells, the JTable thinks I have clicked more than I actually have. For instance, I click 3 cells and the JTable automatically selects a 4th.

Here is an image of my issue. I select 3 cells in the order numbered in the image, and a 4th one is selected after selecting the 3rd. enter image description here

How can I solve this issue?

Runnable example. My table application looks identical to this(obviously no menus or extra crap but this is how it is setup).

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class TestTable extends JFrame {

    private JPanel contentPane;
    private DefaultTableModel dbModel;
    private JTable table;
    private JScrollPane scrollPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    TestTable frame = new TestTable();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestTable() {
        super("Test Table");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 500);

        String[] columnNames = {"Name","Sport","Years Played"};
        Object[][] data = {{"Tim","Baseball","4"},
                {"Josh","Hockey","6"},
                {"Jessica","Volleyball","3"}};

        dbModel = new DefaultTableModel(data, columnNames);
        table = new JTable(dbModel);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        table.getColumnModel().setColumnSelectionAllowed(true);
        table.setCellSelectionEnabled(true);
        scrollPane = new JScrollPane(table);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        contentPane.add(scrollPane, BorderLayout.CENTER);
        setContentPane(contentPane);
    }
}
BitWar
  • 43
  • 1
  • 2
  • 7
  • This sounds like your using a custom cell render but aren't resetting it on each run. Consider posting a runnable demonstration of your problem – MadProgrammer Feb 25 '14 at 20:13
  • I am not using a custom cell renderer. I just looked at the TableSelectionDemo by Oracle and the same thing that happens in my application, happens in the demo. – BitWar Feb 26 '14 at 03:04
  • 1
    Can you provide a runnable example? – MadProgrammer Feb 26 '14 at 03:10
  • Just added a runnable example. It is exactly how my application looks minus all the extra junk. They both behave the same with respect to selecting cells. – BitWar Feb 26 '14 at 03:58
  • Hmm, works fine for me, I click 1, 2, 3 without any issues. Running Windows 7 and Java 6 & 7 – MadProgrammer Feb 26 '14 at 04:11
  • Perhaps I was being vague in my question. I want to be able to select multiple cells at a time(e.g., click the 1st cell, control+click the 2nd cell, then control+click the 3rd cell and have only those 3 cells selected). When I click and control+click in a pattern similar to the picture I posted, a 4th cell gets highlighted or selected when it wasn't clicked on in the first place. – BitWar Feb 26 '14 at 04:19
  • Yeah, it is rather frustrating. – BitWar Feb 26 '14 at 04:25

1 Answers1

4

To enable single cell selection, use setCellSelectionEnabled(true), which "treats the intersection of the row and column selection models as the selected cells." See also this related answer.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Setting 'setCellSelectionEnabled()' to true did not fix the issue. I just looked at the TableSelectionDemo by Oracle and the same thing that happens in my application, happens in the demo. – BitWar Feb 26 '14 at 03:05
  • You might look at this alternative [approach](http://stackoverflow.com/a/7620726/230513) for _non_-contiguous selection. – trashgod Feb 26 '14 at 04:00