i made a jtable in java with 2 columns

when i select some rows and click a button it should get the selected rows

but this array comes back empty

here are some sniplets

JScrollPane scroller = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroller.setBorder(null);
        this.add(scroller,BorderLayout.CENTER);
        customers = new JTable();
        scroller.setViewportView(customers);
        customers.setAutoCreateRowSorter(true);
        customers.setModel(new DefaultTableModel(new Object[][]{}, new String[]{"Naam", "Nummer"}) {

            @Override
            public Class getColumnClass(int columnIndex) {
                return String.class;
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                if (columnIndex == 0) {
                    return true;
                }
                return false;
            }
        });
        customers.setColumnSelectionAllowed(false);
        customers.getTableHeader().setReorderingAllowed(false);
        customers.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        for (String user : users) {
            ((DefaultTableModel)customers.getModel()).addRow(new Object[]{"", user});
        }

the methode that checks selected rows (i'm sure it gets excecuted)

public void analyseSelection() {
        System.out.println("aantal rijen: " + customers.getRowCount());
        int[] selected = customers.getSelectedRows();
        System.out.println("selected rows: " + selected.length);
        System.out.println("first selected rows: " + customers.getSelectedRow());

        names = new String[selected.length];
        numbers = new String[selected.length];

        for (int i : selected) {
            names[i] = customers.getValueAt(i, 0).toString();
            numbers[i] = customers.getValueAt(i, 1).toString();
        }
    }

output of this methode when i select row 2 to 5

aantal rijen: 8
selected rows: 0
first selected rows: -1

Recommended Answers

All 5 Replies

Why did you wrote this:

customers.setColumnSelectionAllowed(false);

?
That wont let you select anything. Modify it and tell me the output. Delete it or set it to true.

Sorry for the last post... I thought it was about the rows.. it was about the columns... so don't follow it. You use GUI or make the table programatically?

Sorry for the last post... I thought it was about the rows.. it was about the columns... so don't follow it. You use GUI or make the table programatically?

first post: thats to make sure whole rows get selected at once :)

i made the table programatically


i think my fault is somewhere in the tablemodel
shouldn't i ad any more methods to handle selection, or does that get done automatically?

don't know... I only used it with GUI... probably is the table model... that should be a default table model and be set as the table model after you update the information in it.

found the problem

my main jframe is a singleton
and in the main function i called:

new Main().setVisible(true);

while it should be:

Main.getInstance().setVisible(true);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.