java 表格单元格数据获取

qq7878478 2009-12-18 08:12:48
Java里的JTabel的单元格数据怎么获取?用getVauleAt()的方法不知道怎么才能用
...全文
给本帖投票
810 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
public class addSell extends JPanel{
JLabel sidLabel=new JLabel("当前单号:");
JLabel sid=new JLabel();
JLabel vipidLabel=new JLabel("会员卡号:");
JLabel vipid=new JLabel();
JLabel sellLabel=new JLabel("应收金额");
JLabel sell=new JLabel();
JLabel buyLabel=new JLabel("收款金额");
JTextField buy=new JTextField();
JLabel findLabel=new JLabel("找回金额");
JLabel find=new JLabel();
JLabel goodidLabel=new JLabel("商品编号:");
JTextField goodid=new JTextField();
JLabel vipLabel=new JLabel("会员卡号");
JTextField vip=new JTextField();
JButton vipok=new JButton("输入会员");
JButton sure=new JButton("确定");
JButton contin=new JButton("新建销售");
JButton buyok=new JButton("确认付款");
JPanel panel=new JPanel();
DefaultTableModel model=new DefaultTableModel();
JTable table=new JTable(model);
JScrollPane js=new JScrollPane(table);
DbConn db=new DbConn();
public addSell() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();}
}
private void jbInit() throws Exception {
setLayout(null);//设置布局管理器为无布局

//设置组件位置
sidLabel.setBounds(new Rectangle(5,5,70,20));
sid.setBounds(new Rectangle(80,5,150,20));
vipidLabel.setBounds(new Rectangle(240,5,70,20));
vipid.setBounds(new Rectangle(315,5,100,20));
buyLabel.setBounds(new Rectangle(300,660,70,20));
buy.setBounds(new Rectangle(375,660,150,20));
sellLabel.setBounds(new Rectangle(660,5,70,20));
sell.setBounds(new Rectangle(735,5,150,20));
goodidLabel.setBounds(new Rectangle(10,650,70,20));
goodid.setBounds(new Rectangle(85,650,100,20));
sure.setBounds(new Rectangle(200,650,65,25));
contin.setBounds(new Rectangle(900,650,90,25));
vipLabel.setBounds(new Rectangle(10,675,70,20));
vip.setBounds(new Rectangle(85,675,100,20));
vipok.setBounds(new Rectangle(200,675,80,25));
buyok.setBounds(new Rectangle(530,650,80,25));
js.setBounds(new Rectangle(5,30,1100,600));

//将组件加入容器
this.add(sidLabel);this.add(sid);this.add(vipidLabel);this.add(vipid);
this.add(buyLabel);this.add(buy);this.add(buyok);
this.add(sellLabel);this.add(sell);
this.add(js);this.add(panel);this.add(goodidLabel);
this.add(goodid);this.add(sure);this.add(contin);this.add(vipLabel);this.add(vip);this.add(vipok);
//初始表格
model.addColumn("编号");
model.addColumn("名称");
model.addColumn("规格");
model.addColumn("单位");
model.addColumn("金额");
model.addColumn("数量");
table.getTableHeader().setReorderingAllowed(false);
//设置表格列宽
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(0).setMaxWidth(100);
table.getColumnModel().getColumn(0).setMinWidth(100);
table.getColumnModel().getColumn(2).setPreferredWidth(50);
table.getColumnModel().getColumn(2).setMaxWidth(50);
table.getColumnModel().getColumn(2).setMinWidth(100);

table.setDragEnabled(true);
sure.addActionListener(new add_good());
contin.addActionListener(new add_good());
vipok.addActionListener(new add_good());
buyok.addActionListener(new add_good());
}
class add_good extends Thread implements ActionListener{

public void actionPerformed(ActionEvent e){
int g=model.getRowCount();
if(e.getSource()==sure){
contin.setEnabled(true);
String gid=goodid.getText();
String Sql="select * from Goods where GoodID='"+gid+"'";
ResultSet rs=db.Query(Sql);
if(gid.equals("")){ JOptionPane.showMessageDialog(null,"请输入商品编号再行查询");}
else{
try{ if(rs.next())
{
String[] o={rs.getString(1),rs.getString(2),rs.getString(4),rs.getString(5),rs.getString(7),"1"};
model.addRow(o);
} else{ JOptionPane.showMessageDialog(null,"暂无您所要的商品信息");}
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.toString());
}
}}
else if(e.getSource()==contin){
if(g>0){for(int l=0;l<g;l++)model.removeRow(0);}
contin.setEnabled(false);
}
else if(e.getSource()==vipok){
String vid=vip.getText();
String Vsql="select VipId from Vip where VipId='"+vid+"'";
ResultSet vrs=db.Query(Vsql);
try{
if(vrs.next()){
vipid.setText(vrs.getString(1));

}
}catch(Exception ve){ve.toString();}

}
else if(e.getSource()==buyok){
if(g>0){for(int z=0;z<g;z++) String s=model.}//就是在这里

}

}

}
}
365810247 2009-12-18
  • 打赏
  • 举报
回复
你把完整的代码贴出来看一下.才知道到底哪里有问题?
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
我是一行一行加表格信息的,这有关系吗
365810247 2009-12-18
  • 打赏
  • 举报
回复
上面的应该是没有问题的.不知道你哪里为什么不行.哎.你看一下你的其它地方的代码吧.
table.getModel().getValueAt(1,1);这样子写的话.你的表格中如果有多于二行二列的数据的话
应该是可以取得到数据的.
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
设置一个按钮动作,点击按取获取单元格里的数据
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
DefaultTableModel model=new DefaultTableModel();
JTable table=new JTable(model);//表格
JScrollPane js=new JScrollPane(table);
365810247 2009-12-18
  • 打赏
  • 举报
回复
你的JTable是如何构造的.你把你的完整代码贴出来看下.
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
不成功,再加个DefaultTableModel对象呢
365810247 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 qq7878478 的回复:]
抱歉,是JTable类的
[/Quote]

你用你的对象.JTabel table=new JTable();
table.getModel().getValueAt(1,1);//这里的table是对象,不是awt中的Table.
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
抱歉,是JTable类的
365810247 2009-12-18
  • 打赏
  • 举报
回复

table.getModel().getValueAt(1,1);
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 swandragon 的回复:]
引用 2 楼 qq7878478 的回复:
String s=tale.getValueAt(1,);这样呢


错误,编译通不过
学习java多长时间啊?
方法调用参数不匹配,返回类型也不对
String s = (String) tale.getValueAt(1,1);
[/Quote]


这样也不行!提示:no a statement
crazylaa 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 swandragon 的回复:]
Object getValueAt(int row, int column) 返回 row 和 column 位置的单元格值。

Object obj = table.getValueAt(1,1);

[/Quote]

up
swandragon 2009-12-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qq7878478 的回复:]
String s=tale.getValueAt(1,);这样呢
[/Quote]

错误,编译通不过
学习java多长时间啊?
方法调用参数不匹配,返回类型也不对
String s = (String) tale.getValueAt(1,1);
qq7878478 2009-12-18
  • 打赏
  • 举报
回复
String s=tale.getValueAt(1,);这样呢
swandragon 2009-12-18
  • 打赏
  • 举报
回复
Object getValueAt(int row, int column) 返回 row 和 column 位置的单元格值。

Object obj = table.getValueAt(1,1);

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部