Java – gets the object from the selected JTable
•
Java
I have a JTable to display a list of books, which can be filtered and sorted
BookSwing. java
package com.bookretailer.ui; import java.awt.Component; import java.awt.EventQueue; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.RowFilter; import javax.swing.RowSorter; import javax.swing.sortOrder; import javax.swing.border.TitledBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.jtableheader; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; import com.bookretailer.modules.Book; import com.bookretailer.modules.utils.BookDB; public class BookSwing extends JFrame { private final static String TITLE = "Book Retailer - Books"; private JPanel jpInfo = new JPanel(); private JTable table; private JTextField txtFilter; private TableRowSorter<BookTableModel> sorter; private BookTableModel bookTableModel; private List<Book> booksList; private Book selectedBook; private ListSelectionModel listSelectionModel; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokelater(new Runnable() { public void run() { try { BookSwing frame = new BookSwing(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public BookSwing() { setSize(600,650); initData(); jpInfo.setBounds(100,100,600,650); jpInfo.setLayout(null); JPanel tbPanel = new JPanel(); tbPanel.setBorder(new TitledBorder(null,"",TitledBorder.LEADING,TitledBorder.TOP,null,null)); tbPanel.setBounds(10,331,570,272); jpInfo.add(tbPanel); tbPanel.setLayout(null); bookTableModel = new BookTableModel(booksList); table = new JTable(); table.setModel(bookTableModel); sorter = new TableRowSorter<BookTableModel>(bookTableModel); table.setAutoCreateRowSorter(true); table.setRowSorter(sorter); final jtableheader header = table.getTableHeader(); header.setDefaultRenderer(new HeaderRenderer(table)); List<RowSorter.sortKey> sortKeyList = new ArrayList<RowSorter.sortKey>(); sortKeyList.add(new RowSorter.sortKey(0,SortOrder.ASCENDING)); sorter.setSortKeys(sortKeyList); sorter.sort(); sorter.setSortsOnUpdates(true); txtFilter = new JTextField(); // Whenever filterText changes,invoke newFilter. txtFilter.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { filterBook(); } public void insertUpdate(DocumentEvent e) { filterBook(); } public void removeUpdate(DocumentEvent e) { filterBook(); } }); JScrollPane scrollPane = new JScrollPane(table); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { initDetail(table.getSelectedRow()); } }); scrollPane.setBounds(6,37,558,225); tbPanel.add(scrollPane); listSelectionModel = table.getSelectionModel(); table.setSelectionModel(listSelectionModel); listSelectionModel.addListSelectionListener(new ListSelectionHandler()); table.setSelectionModel(listSelectionModel); JLabel lblBookFilter = new JLabel("Book Filter"); lblBookFilter.setBounds(10,10,78,25); tbPanel.add(lblBookFilter); txtFilter.setBounds(75,489,25); tbPanel.add(txtFilter); txtFilter.setColumns(10); getContentPane().add(jpInfo); } private void initData() { booksList = BookDB.getBookList(); } protected void initDetail(int selectedRow) { selectedBook = booksList.get(selectedRow); } private void refreshTableData() { table.revalidate(); table.repaint(); table.getSelectionModel().clearSelection(); bookTableModel.fireTableDataChanged(); } private void filterBook() { RowFilter<TableModel,Object> rf = null; // If current expression doesn't parse,don't update. try { rf = RowFilter.regexFilter("(?i)" + txtFilter.getText()); } catch (java.util.regex.PatternSyntaxException e) { return; } sorter.setRowFilter(rf); } class BookTableModel extends AbstractTableModel { List<Book> booksList; String[] headerList = { "Book Code","Title","Price" }; Class[] classes = { Integer.class,String.class,Double.class }; public BookTableModel(List<Book> list) { booksList = list; } @Override public int getColumnCount() { return headerList.length; } @Override public int getRowCount() { return booksList.size(); } @Override public Class<?> getColumnClass(int arg0) { // TODO Auto-generated method stub return classes[arg0]; } // this method is called to set the value of each cell @Override public Object getValueAt(int row,int column) { Book entity = null; entity = booksList.get(row); switch (column) { case 0: return entity.getBookId(); case 1: return entity.getTitle(); case 2: return entity.getPrice(); default: return ""; } } // This method will be used to display the name of columns public String getColumnName(int col) { return headerList[col]; } @Override public void setValueAt(Object value,int row,int col) { Book entity = null; entity = booksList.get(row); switch (col) { case 0: entity.setBookId((Integer) value); break; case 1: entity.setTitle((String) value); break; case 2: entity.setPrice((Double) value); break; default: break; } fireTableCellUpdated(row,col); } } private static class HeaderRenderer implements TableCellRenderer { TableCellRenderer renderer; public HeaderRenderer(JTable table) { renderer = table.getTableHeader().getDefaultRenderer(); } @Override public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int col) { return renderer.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col); } } class ListSelectionHandler implements ListSelectionListener { @Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { ListSelectionModel model = table.getSelectionModel(); int lead = model.getLeadSelectionIndex(); setFields(lead); } } } private void setFields(int index) { System.out.println(booksList.get(index).getTitle()); } }
I want to set the selectedbook when the user clicks on the table row. When the user does not filter or sort the data, it works normally, but when the user sorts the data, the selectedbook is assigned the wrong book
How can I make it work to assign the correct book to the selected book
Solution
Use this Code:
selectedBook = booksList.get(table.convertRowIndexToModel(selectedRow));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码