简体   繁体   中英

Getting an object from JTable

I want to get information about user from the JTable. Everything is okay, but when I sort the array by name, the objects get read wrong. Example: Here is everything ok

Above everything is ok. I choose 4 value from the table, shows me 4 items from the Users list. Two books are displayed. But now I sort by 'number of loans', and i choose user with two loans. But the array reads as 'you chose the first value' and shows the first value from the User list.

After sorting

I'd like to receive a specific user after selecting from the board. Thanks.

My code:

        tablicaWypozyczen.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int row = tablicaWypozyczen.rowAtPoint(e.getPoint());
                int col = tablicaWypozyczen.columnAtPoint(e.getPoint());
                if (row >= 0 && col >= 0) {
                    JOptionPane.showMessageDialog(null, Dane.uzytkownicyZWypozyczeniami.get(row).toString(), "Informacje o użytkowniku", 1);
                    System.out.println(tablicaWypozyczen.getSelectedRow());
                }
            }
        }
    });

but when I sort the array by name,

Why are you sorting the Array?

Data is stored in the TableModel. The sorting should be done on the table, not on data in some external array. You don't want data in two places, it is too hard to keep the data in sync.

Read the section from the Swing tutorial on Sorting and Filtering for more information.

If you want to get the sorted value from the table you use:

table.getValueAt(table.getSelectedRow(), theColumn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM