为什么没人理我啊!! 跪求一个答案: 新建了一个只有一行的JTable对象.想实现在每一行的第一个单元格单击时,table添加一行空行...问了几天了...没人理....惨啊....郁闷啊没分了..只能给40...... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package addbook;import java.awt.*;import java.awt.event.*;import java.sql.*;import javax.swing.*;import javax.swing.table.*;import javax.swing.event.*;import java.util.*;public class AddbookFrame extends JFrame{ JPanel content; JLabel addLab=new JLabel(" 添加新书目",new ImageIcon("bookbig.gif"),JLabel.LEFT); JButton addBtn=new JButton("添 加"); JButton cancelBtn=new JButton("取 消"); String[] title={"索书号","书名","作者","出版社","出版日期","价格","所属类别"}; JScrollPane scroll=null; JTable table; DefaultTableModel tm=new DefaultTableModel(title,1); Vector vector=new Vector(); Connection connection=null; Statement statement=null; String driverName="sun.jdbc.odbc.JdbcOdbcDriver"; String user="system"; String passWord="ericdb"; String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=ericdb.mdb"; String sqlStatement=new String(); JComboBox comboBox; public AddbookFrame() { super(); setTitle("添加新书"); this.setResizable(false); setIconImage(Toolkit.getDefaultToolkit().createImage("bookbig.gif")); Dimension screanSize=Toolkit.getDefaultToolkit().getScreenSize(); setSize(860,440); Dimension frameSize=this.getSize(); setLocation((screanSize.width-frameSize.width)/2, (screanSize.height-frameSize.height)/2); content=(JPanel)this.getContentPane(); content.setLayout(null); addLab.setBounds(30,10,600,50); sqlStatement="select * from KIND"; try { Class.forName(driverName); } catch(java.lang.ClassNotFoundException ex){ } try { connection=DriverManager.getConnection(url,user,passWord); statement=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet res=statement.executeQuery(sqlStatement); while(res.next()) vector.addElement(res.getString("KIND_NAME")); res.close(); } catch(SQLException ex) { System.out.println(ex.getErrorCode()); System.out.println(ex.getMessage()); System.out.println(ex.getSQLState()); } *********************************************************************** 在该table对象的每一行的第一个单元格单击时,table添加一行空行 table=new JTable(tm); TableColumn Column = table.getColumnModel().getColumn(6); comboBox=new JComboBox(vector); Column.setCellEditor(new DefaultCellEditor(comboBox)); ((DefaultCellEditor) table.getDefaultEditor(Object.class)).setClickCountToStart(1); *********************************************************************************** scroll=new JScrollPane(table); scroll.setBounds(20,80,820,240); addBtn.setBounds(50,340,160,30); addBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try{ for(int i=0;i<table.getRowCount();i++){ String updateSql="insert into BOOK values('"+ table.getValueAt(i,0)+"','"+table.getValueAt(i,1)+"','"+ table.getValueAt(i,2)+"','"+table.getValueAt(i,3)+"','"+ table.getValueAt(i,4)+"','"+table.getValueAt(i,5)+"','"+ table.getValueAt(i,6)+"')"; statement.executeUpdate(updateSql); } JOptionPane msg=new JOptionPane(); msg.showMessageDialog(AddbookFrame.this,"新书目添加成功","添加成功",1); } catch(SQLException ex) { System.out.println(ex.getErrorCode()); System.out.println(ex.getMessage()); System.out.println(ex.getSQLState()); } try{ if(statement!=null)statement.close(); } catch(SQLException ex) { } try{ if(statement!=null)connection.close(); } catch(SQLException ex) { } } }); cancelBtn.setBounds(240,340,160,30); content.add(cancelBtn); content.add(addLab); content.add(scroll); content.add(addBtn); } public static void main(String[] args) { AddbookFrame frame=new AddbookFrame(); frame.addWindowListener(new WindowAdapter(){ public void WindowClosing(WindowEvent e){ System.exit(0); } }); frame.show(); }} 实现接口MouseListener判断单击信息,然后再做你所想做的事! -_- 回复人: hifan(Hifan On Line) ( ) 信誉:100 2005-6-12 18:53:18 得分: 0 实现接口MouseListener判断单击信息,然后再做你所想做的事! -_- 尝试过了...但是不行...不知道为什么 swing问题 Java中关于异常处理的问题 java搜索引擎的问题,希望高手写详细点 这个语句是for循环吗??for(Employee e:staff) 怎么用String[]来存放打印输出的内容 如何判断JTextPane的内容已全选? JComboBox为何响应不了任何键盘事件? [求解答]java.lang.IllegalMonitorStateException 抽象类和接口的讨论 关于mina下实现UDP穿洞的问题。 求救!本人课程设计,想编写一个多媒体播放程序,请教各位一些问题。 [再次求助]关于跟据颜色或图片在屏幕上定位
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
public class AddbookFrame extends JFrame{
JPanel content;
JLabel addLab=new JLabel(" 添加新书目",new ImageIcon("bookbig.gif"),JLabel.LEFT);
JButton addBtn=new JButton("添 加");
JButton cancelBtn=new JButton("取 消");
String[] title={"索书号","书名","作者","出版社","出版日期","价格","所属类别"};
JScrollPane scroll=null;
JTable table;
DefaultTableModel tm=new DefaultTableModel(title,1);
Vector vector=new Vector();
Connection connection=null;
Statement statement=null;
String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
String user="system";
String passWord="ericdb";
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=ericdb.mdb";
String sqlStatement=new String();
JComboBox comboBox;
public AddbookFrame()
{
super();
setTitle("添加新书");
this.setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().createImage("bookbig.gif"));
Dimension screanSize=Toolkit.getDefaultToolkit().getScreenSize();
setSize(860,440);
Dimension frameSize=this.getSize();
setLocation((screanSize.width-frameSize.width)/2,
(screanSize.height-frameSize.height)/2);
content=(JPanel)this.getContentPane();
content.setLayout(null);
addLab.setBounds(30,10,600,50);
sqlStatement="select * from KIND";
try
{
Class.forName(driverName);
}
catch(java.lang.ClassNotFoundException ex){
}
try
{
connection=DriverManager.getConnection(url,user,passWord);
statement=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet res=statement.executeQuery(sqlStatement);
while(res.next())
vector.addElement(res.getString("KIND_NAME"));
res.close();
}
catch(SQLException ex)
{
System.out.println(ex.getErrorCode());
System.out.println(ex.getMessage());
System.out.println(ex.getSQLState());
}
***********************************************************************
在该table对象的每一行的第一个单元格单击时,table添加一行空行
table=new JTable(tm);
TableColumn Column = table.getColumnModel().getColumn(6);
comboBox=new JComboBox(vector);
Column.setCellEditor(new DefaultCellEditor(comboBox));
((DefaultCellEditor) table.getDefaultEditor(Object.class)).setClickCountToStart(1);
***********************************************************************************
scroll=new JScrollPane(table);
scroll.setBounds(20,80,820,240);
addBtn.setBounds(50,340,160,30);
addBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
for(int i=0;i<table.getRowCount();i++){
String updateSql="insert into BOOK values('"+
table.getValueAt(i,0)+"','"+table.getValueAt(i,1)+"','"+
table.getValueAt(i,2)+"','"+table.getValueAt(i,3)+"','"+
table.getValueAt(i,4)+"','"+table.getValueAt(i,5)+"','"+
table.getValueAt(i,6)+"')";
statement.executeUpdate(updateSql);
}
JOptionPane msg=new JOptionPane();
msg.showMessageDialog(AddbookFrame.this,"新书目添加成功","添加成功",1);
}
catch(SQLException ex)
{
System.out.println(ex.getErrorCode());
System.out.println(ex.getMessage());
System.out.println(ex.getSQLState());
}
try{
if(statement!=null)statement.close();
}
catch(SQLException ex)
{
}
try{
if(statement!=null)connection.close();
}
catch(SQLException ex)
{
}
}
});
cancelBtn.setBounds(240,340,160,30);
content.add(cancelBtn);
content.add(addLab);
content.add(scroll);
content.add(addBtn);
} public static void main(String[] args)
{
AddbookFrame frame=new AddbookFrame();
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
frame.show();
}}
实现接口MouseListener判断单击信息,然后再做你所想做的事! -_-
尝试过了...但是不行...不知道为什么