Jtable击一行记录,成功删除后,但是删除第二条记录时为什么不可以?主要是第二次getselectedrow一直返-1
importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swin...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.table.*;
public class Delnewstype extends JFrame implements ActionListener{
private JTable table;
private JButton jb1;
private Vector content;
private Vector columnnames;
private DefaultTableModel dtm;
private JScrollPane jscroll;
private JPanel jp1;
String sql;
public Delnewstype(){
super("删除信息窗体");
columnnames=new Vector(1,1);
content=new Vector(1,1);
jb1=new JButton("删除");
jb1.addActionListener(this);
columnnames.add("报刊号");
columnnames.add("报刊名称");
columnnames.add("报刊类型");
columnnames.add("出版社");
columnnames.add("单价");
jp1=new JPanel();
jp1.add(jb1);
this.add(jp1,BorderLayout.SOUTH);
}
public void creat(){
content.removeAllElements();
sql="select * from newsinfo";
JavaBean bean=new JavaBean();
ResultSet set=bean.execquery(sql);
try {
while(set.next()){
Vector item=new Vector(1,1);
item.add(set.getString("news_id"));
item.add(set.getString("news_name"));
item.add(set.getString("news_type"));
item.add(set.getString("news_publisher"));
item.add(set.getFloat("news_price"));
content.add(item);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
bean.closestmt();
bean.closeconn();
}
dtm=new DefaultTableModel(content,columnnames){
public boolean isCellEditable(int row, int column) {
return false;
}
};
table=new JTable(dtm);
jscroll=new JScrollPane(table);
this.add(jscroll,BorderLayout.NORTH);
}
public void showframe(){
this.setSize(500,500);
this.setResizable(false);
int w = (Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2;
int h = (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2;
this.setLocation(w, h);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if("删除".equals(str)){
int i=table.getSelectedRow();
if(i>=0){
String newsid=(String)table.getValueAt(i,0);
sql="delete from newsinfo where news_id='"+newsid+"'";
JavaBean bean=new JavaBean();
bean.execupdate(sql);
bean.closeconn();
bean.closestmt();
creat();
this.setVisible(true);
}
else{
JOptionPane.showMessageDialog(this, "你还未选择要删除的记录");
}
}
}
public static void main(String[] args) {
Delnewstype test=new Delnewstype();
test.creat();
test.showframe();
}
}
第二次点击删除时,一直弹出你还未选择要删除的记录,那个selectedrow()返回值一直是-1。请教各位大大,是什么原因?
另外那个javabean其实就是封装的对数据库的操作。因为很多地方要用到,所以写了这个类。各位帮帮忙啊,这个是课程设计,挺急的。 展开
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.table.*;
public class Delnewstype extends JFrame implements ActionListener{
private JTable table;
private JButton jb1;
private Vector content;
private Vector columnnames;
private DefaultTableModel dtm;
private JScrollPane jscroll;
private JPanel jp1;
String sql;
public Delnewstype(){
super("删除信息窗体");
columnnames=new Vector(1,1);
content=new Vector(1,1);
jb1=new JButton("删除");
jb1.addActionListener(this);
columnnames.add("报刊号");
columnnames.add("报刊名称");
columnnames.add("报刊类型");
columnnames.add("出版社");
columnnames.add("单价");
jp1=new JPanel();
jp1.add(jb1);
this.add(jp1,BorderLayout.SOUTH);
}
public void creat(){
content.removeAllElements();
sql="select * from newsinfo";
JavaBean bean=new JavaBean();
ResultSet set=bean.execquery(sql);
try {
while(set.next()){
Vector item=new Vector(1,1);
item.add(set.getString("news_id"));
item.add(set.getString("news_name"));
item.add(set.getString("news_type"));
item.add(set.getString("news_publisher"));
item.add(set.getFloat("news_price"));
content.add(item);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
bean.closestmt();
bean.closeconn();
}
dtm=new DefaultTableModel(content,columnnames){
public boolean isCellEditable(int row, int column) {
return false;
}
};
table=new JTable(dtm);
jscroll=new JScrollPane(table);
this.add(jscroll,BorderLayout.NORTH);
}
public void showframe(){
this.setSize(500,500);
this.setResizable(false);
int w = (Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2;
int h = (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2;
this.setLocation(w, h);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if("删除".equals(str)){
int i=table.getSelectedRow();
if(i>=0){
String newsid=(String)table.getValueAt(i,0);
sql="delete from newsinfo where news_id='"+newsid+"'";
JavaBean bean=new JavaBean();
bean.execupdate(sql);
bean.closeconn();
bean.closestmt();
creat();
this.setVisible(true);
}
else{
JOptionPane.showMessageDialog(this, "你还未选择要删除的记录");
}
}
}
public static void main(String[] args) {
Delnewstype test=new Delnewstype();
test.creat();
test.showframe();
}
}
第二次点击删除时,一直弹出你还未选择要删除的记录,那个selectedrow()返回值一直是-1。请教各位大大,是什么原因?
另外那个javabean其实就是封装的对数据库的操作。因为很多地方要用到,所以写了这个类。各位帮帮忙啊,这个是课程设计,挺急的。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询