java 怎么删除JTable里的某一行
展开全部
用int row= jt.getSelectedRow();if(row!=-1)dtm.removeRow(row);就可以删除指定行了。
我给你个例子,你看了就明白了。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class G {
private Object[][] cells={{"some",0},{"any",1},{"even",2},
{"text",0},{"and",1},{"text",2}};
private String[] columnNames={"Column 1","Column 2"};
JFrame jf;
JButton jb;
JTable jt;
JPanel jp,jp1;
JScrollPane scrollPane;
DefaultTableModel dtm;
G(){
jf=new JFrame("JTable");
jb=new JButton("删除");
jb.addActionListener(new AddActionListener());
dtm = new DefaultTableModel(cells,columnNames);
jt = new JTable(dtm);
jt.setPreferredScrollableViewportSize(new Dimension(300,120));
scrollPane = new JScrollPane(jt);
jp=new JPanel();
jp1=new JPanel();
jp1.add(jb);
jp.setLayout(new GridLayout(2,1));
jp.add(scrollPane);
jp.add(jp1);
jf.add(jp,BorderLayout.CENTER);
jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class AddActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb){
int row= jt.getSelectedRow(); //这句选择要删除的行
if(row!=-1) //这句判断是否有选中的行
dtm.removeRow(row); //这句删除指定行
}
}
}
public static void main(String[] args) {
new G();
}
}
我给你个例子,你看了就明白了。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class G {
private Object[][] cells={{"some",0},{"any",1},{"even",2},
{"text",0},{"and",1},{"text",2}};
private String[] columnNames={"Column 1","Column 2"};
JFrame jf;
JButton jb;
JTable jt;
JPanel jp,jp1;
JScrollPane scrollPane;
DefaultTableModel dtm;
G(){
jf=new JFrame("JTable");
jb=new JButton("删除");
jb.addActionListener(new AddActionListener());
dtm = new DefaultTableModel(cells,columnNames);
jt = new JTable(dtm);
jt.setPreferredScrollableViewportSize(new Dimension(300,120));
scrollPane = new JScrollPane(jt);
jp=new JPanel();
jp1=new JPanel();
jp1.add(jb);
jp.setLayout(new GridLayout(2,1));
jp.add(scrollPane);
jp.add(jp1);
jf.add(jp,BorderLayout.CENTER);
jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class AddActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb){
int row= jt.getSelectedRow(); //这句选择要删除的行
if(row!=-1) //这句判断是否有选中的行
dtm.removeRow(row); //这句删除指定行
}
}
}
public static void main(String[] args) {
new G();
}
}
展开全部
/**
可运行例子。。。看看是不是你要的,有问题留言。
**/
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();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
通过getSelectedRow()得到选中的行,再根据id或者其他字段写sql语句就ok了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询