我想一次从JTable中删除几行,但我一次只能删除一行。 JTable一次只允许一个选择吗?如果我想通过选择删除多行,java允许我们这样做吗?即使我选择了多行,给定的代码也只能一次删除一行。
DBConnection
是一个连接MySql数据库的类。
staffTable1
是JTable
ShowStaffInStaffTable()
是一个使用Getter Setter Class从MySql导入数据来更新JTable
行和列的函数
这是我尝试过的代码:
int row = staffTable1.getSelectedRow();
DefaultTableModel model = (DefaultTableModel) staffTable1.getModel();
String selected = model.getValueAt(row, 9).toString();
if (row >= 0) {
try {
DBConnection DB12 = new DBConnection();
Connection conn = DB12.Connect();
String sql = "delete from staff where Contact_No= ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, selected);
stmt.executeUpdate();
form12.ShowStaffInStaffTable();
} catch (Exception w) {
JOptionPane.showMessageDialog(this, "Connection Error!"+w);
w.printStackTrace();
}
}
答案 0 :(得分:2)
以下显示如何从JTable(和JList)中删除多个选定的行:
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
public class ItemDeletion extends JPanel
{
private JList<String> list;
private JTable table;
public ItemDeletion()
{
setLayout( new BorderLayout(5, 5) );
String[] items =
{
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"
};
// Add the list
DefaultListModel<String> listModel = new DefaultListModel<String>();
for (String item: items)
listModel.addElement( item );
list = new JList<String>( listModel );
JButton listDelete = new JButton( "Delete From List" );
listDelete.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
DefaultListModel model = (DefaultListModel)list.getModel();
int row = list.getSelectedIndex();
while (row != -1)
{
model.removeElementAt( row );
row = list.getSelectedIndex();
}
}
});
JPanel listPanel = new JPanel( new BorderLayout(5, 5) );
listPanel.add(new JScrollPane( list ), BorderLayout.CENTER);
listPanel.add(listDelete, BorderLayout.PAGE_END);
// Add the table
DefaultTableModel tableModel = new DefaultTableModel(0, 1);
List<String> tableItems = Arrays.asList( items );
Collections.shuffle( tableItems );
for (String item: tableItems)
{
System.out.println( item );
tableModel.addRow( new String[]{item} );
}
table = new JTable( tableModel );
table.setAutoCreateRowSorter(true);
((DefaultRowSorter)table.getRowSorter()).toggleSortOrder(0);
JButton tableDelete = new JButton( "Delete From Table" );
tableDelete.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
DefaultTableModel model = (DefaultTableModel)table.getModel();
int row = table.getSelectedRow();
while (row != -1)
{
int modelRow = table.convertRowIndexToModel( row );
model.removeRow( modelRow );
row = table.getSelectedRow();
}
}
});
JPanel tablePanel = new JPanel( new BorderLayout(5, 5) );
tablePanel.add(new JScrollPane( table ), BorderLayout.CENTER);
tablePanel.add(tableDelete, BorderLayout.PAGE_END);
add(listPanel, BorderLayout.LINE_START);
add(tablePanel, BorderLayout.LINE_END);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Multiple Item Deletion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ItemDeletion(), BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
显然,您还需要修改代码以删除数据库中的行。