坑爹的JTable.getSelectedRows()

还是在搞课程设计,今天遇到一个很蛋疼的问题,我想实现同时删除JTable上的多条数据,于是写了如下代码

int[] selRow = jTable.getSelectedRows(); //jTable是我的表格
for (int i=0;i<selRow.length;i++){
tm.removeRow(selRow[i]); //tm是我的TableModel;
}

然后一直报数组越界,我就奇了怪了,怎么会数组越界呢???搞了好久,最后总算搞明白了,tm没removeRow一次,jTable的行号就改变一下,于是改成了如下就可以了

int[] selRow = jTable.getSelectedRows();    //jTable是我的表格
initRow = selRow[0];
for (int i=0;i<selRow.length;i++){
    tm.removeRow(selRow[0]);   //tm是我的TableModel;
}

坑爹呢!!!!

String arr[] = { "ISBN", "book name", "author", "price" }; String comm[][] = new String[row][4]; rs.next(); for (int i = 0; i < row; i++) { comm[i][0] = rs.getString("ISBN"); comm[i][1] = rs.getString("book_name"); comm[i][2] = rs.getString("author"); comm[i][3] = rs.getString("price"); rs.next(); } JTable jTable = new JTable(comm, arr); jTable.setBounds(0, 10, 980, 400); JTableHeader head = jTable.getTableHeader(); head.setPreferredSize(new Dimension(head.getWidth(), 35)); head.setFont(new Font("Arial", Font.PLAIN, 30)); jTable.setFont(new Font("Menu.font", Font.PLAIN, 20)); jTable.setRowHeight(25); JTextField filterField = new JTextField(); filterField.setBounds(10, 10, 150, 20); this.add(filterField); JButton filterButton = new JButton("Filter"); filterButton.setBounds(170, 10, 80, 20); this.add(filterButton); filterButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String filterText = filterField.getText(); for (int i = 0; i < row; i++) { if (comm[i][1].equals(filterText)) { jTable.setRowHeight(i, 25); } else { jTable.setRowHeight(i, 0); } } } }); jTable.setPreferredScrollableViewportSize(new Dimension(800, 300));// 设置可滚动视图的大小 JScrollPane jScrollPane = new JScrollPane(jTable); jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); this.add(jScrollPane, BorderLayout.CENTER);// 将滚动条放到窗体 setDefaultCloseOperation(EXIT_ON_CLOSE); jTable.setVisible(true); this.setVisible(true);// 设置窗体可见 this.validate();// 设置组件可见 this.pack();// 自动调整组建大小使每个组键都能显示 this.setLocationRelativeTo(null); this.setTitle("Booklist"); this.setSize(1000, 600); this.setLayout(null); jScrollPane.setBounds(0, 70, 980, 300);// 添加这一行 这段代码为什么报错:New row height less than 1
05-24
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值