JTable删除选中行
我的代码是:introwNum=0;rowNum=this.DataTable.getSelectedRow();DefaultTableModeltableModel=...
我的代码是:
int rowNum = 0;
rowNum = this.DataTable.getSelectedRow();
DefaultTableModel tableModel = (DefaultTableModel) DataTable.getModel();
tableModel.removeRow(rowNum);
为什么会出现这样的异常?说我类型转换错误?
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel 展开
int rowNum = 0;
rowNum = this.DataTable.getSelectedRow();
DefaultTableModel tableModel = (DefaultTableModel) DataTable.getModel();
tableModel.removeRow(rowNum);
为什么会出现这样的异常?说我类型转换错误?
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel 展开
1个回答
展开全部
/*
这是一个可运行的例子,看看是不是你要的。
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TableTest extends JFrame{
//声明组件
private JTable table;
private DefaultTableModel model;
private JButton deleteButton;
private JPanel panel;
public TableTest() {
// TODO Auto-generated constructor stub
//初始化组件
panel = new JPanel();
String[] columnNames = {"编号","用户名","密码"};
String[][]data={{"1","zhangsan","123456"},{"2","lisi","4567"}};
model = new DefaultTableModel(data, columnNames);
table = new JTable(model);
deleteButton = new JButton("删除");
panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(table);
//添加组件
panel.add(scrollPane,BorderLayout.CENTER);
panel.add(deleteButton,BorderLayout.SOUTH);
this.add(panel);
//设置窗口的基本属性.
this.setVisible(true);
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加事件监听器
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取要删除的行,没有选择是-1
int row = table.getSelectedColumn();
if(row == -1){
JOptionPane.showMessageDialog(TableTest.this,"请选择要删除的行!");
}else{
model.removeRow(row-1);
}
}
});
}
public static void main(String[] args) {
new TableTest();
}
}
这是一个可运行的例子,看看是不是你要的。
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TableTest extends JFrame{
//声明组件
private JTable table;
private DefaultTableModel model;
private JButton deleteButton;
private JPanel panel;
public TableTest() {
// TODO Auto-generated constructor stub
//初始化组件
panel = new JPanel();
String[] columnNames = {"编号","用户名","密码"};
String[][]data={{"1","zhangsan","123456"},{"2","lisi","4567"}};
model = new DefaultTableModel(data, columnNames);
table = new JTable(model);
deleteButton = new JButton("删除");
panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(table);
//添加组件
panel.add(scrollPane,BorderLayout.CENTER);
panel.add(deleteButton,BorderLayout.SOUTH);
this.add(panel);
//设置窗口的基本属性.
this.setVisible(true);
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加事件监听器
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取要删除的行,没有选择是-1
int row = table.getSelectedColumn();
if(row == -1){
JOptionPane.showMessageDialog(TableTest.this,"请选择要删除的行!");
}else{
model.removeRow(row-1);
}
}
});
}
public static void main(String[] args) {
new TableTest();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询