5

Happy New Year for everyone! :)

I have a JTable inside JScrollPane (fillsViewportHeight is true) and want to enable row selection from the end when drag starts outside the table (as shown on the pic) enter image description here

SSCCE:

public class SimpleTableDemo extends JPanel {

    public SimpleTableDemo() {
        super(new BorderLayout(0, 0));

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

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        JScrollPane scrollPane = new JScrollPane(table);

        add(scrollPane, BorderLayout.CENTER);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 400);

        SimpleTableDemo newContentPane = new SimpleTableDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

How can I do that?

UPD:

The default behavior of JTable is to select all rows from start to current one, if I start selection lower the last row. I want to select only that rows which was under the mouse while I've dragged the mouse up (as I've shown in picture).

SeniorJD
  • 6,946
  • 4
  • 36
  • 53
  • whats happends in the case that you set [ListSelectionModel.SINGLE_INTERVAL_SELECTION](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection), default value is `MULTIPLE_INTERVAL_SELECTION` – mKorbel Jan 02 '14 at 11:58
  • 1. you have to use SINGLE_..., 2. question you wan to select all rows from one selected row to the end of JTables view (in this case doeasn't matter if is sorted or filtered, selection is only about JTables view), e.g. then selection to 1st row to select all rows in JTable, it's true??? 3. do you want to store this selection in SelectionModel – mKorbel Jan 02 '14 at 12:13
  • can you use the approach shown in [how to make a row in jtable nonselectable?](http://stackoverflow.com/questions/15586118/how-to-make-a-row-in-jtable-nonselectable) – trashgod Jan 02 '14 at 12:58
  • @mKorbel 1. I said, it changes nothing; 2. *sorry I don't understand what you mean*; 3. Yes, of course. – SeniorJD Jan 02 '14 at 13:15
  • @trashgod thanks, but it is not what I mean – SeniorJD Jan 02 '14 at 13:16
  • table.setRowSelectionInterval(table.getRowCount() - 1, table.getRowCount() - 1); and by reseting table.removeRowSelectionInterval(table.getRowCount() - 1, table.getRowCount() - 1); ??? – mKorbel Jan 02 '14 at 14:29

1 Answers1

3

Ingredients:

  • a mouseListener detecting the mousePressed "below" the last row
  • if so, set the anchor of the row selection model to the last row

Something like:

MouseListener l = new MouseAdapter() {

    @Override
    public void mousePressed(MouseEvent e) {
        Rectangle lastRow = table.getCellRect(table.getRowCount() - 1, 0, false);
        if (e.getY() < lastRow.y + lastRow.height) return;
        System.out.println(table);
        table.getSelectionModel().setAnchorSelectionIndex(table.getRowCount()-1);
    }

};
table.addMouseListener(l);
kleopatra
  • 51,061
  • 28
  • 99
  • 211