| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 811 人关注过本帖
标题:为什么新增表格内容使用 getValueAt 会有空的值出现 null ??
取消只看楼主 加入收藏
申请幸福
Rank: 1
等 级:新手上路
帖 子:29
专家分:5
注 册:2013-10-25
结帖率:90.91%
收藏
 问题点数:0 回复次数:1 
为什么新增表格内容使用 getValueAt 会有空的值出现 null ??
程序代码:
import java.import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import *;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import import import import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


 class BasicInfoFrame implements ActionListener{
    private JFrame basicFrame;
    private JButton addRowBtn ;    
    private JButton removeRowBtn; 
    private JButton save;
    private JTable table  ;
    private TableModel model;
    private String[] titles = {"宿舍号","院系","专业","总得分","备注"};
    private Object [][] userInfo ={{"223","信息学院","网络工程","89",""},
            };
    private JScrollPane scr;
    private JButton serch;
    private Component pan;
    private Container frame;
    
    public BasicInfoFrame(){
        setComponents();
    }
    public void setPanel(){
        basicFrame.setVisible(true) ;
    }
    private void setComponents(){
        
        basicFrame = new JFrame("宿舍评比信息");
        addRowBtn = new JButton("增加信息") ;
        removeRowBtn = new JButton("删除信息") ;
        serch = new JButton("查询信息") ; 
        save = new JButton("保存");
        if(userInfo!=null){ 
            model = new DefaultTableModel(userInfo, titles) {  
            
            private static final long serialVersionUID = 1L;

                        public Class getColumnClass(int column) {  
                            Class returnValue;  
                            if ((column >= 0) ) {  
                                returnValue = getValueAt(0, column).getClass();  
                            } else {  
                                returnValue = Object.class;  
                            }  
                            return returnValue;  
                        }  
                    };
        }else{
            model = new DefaultTableModel(userInfo, titles);
        }
         
         table = new JTable(model);  
         RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);  
         table.setRowSorter(sorter); 
         scr = new JScrollPane(table) ;
        JPanel toolBar = new JPanel();
        toolBar.add(this.addRowBtn) ;
        toolBar.add(this.removeRowBtn) ;
        toolBar.add(this.serch) ;
        toolBar.add(this.save) ;
        
        basicFrame.add(toolBar,BorderLayout.NORTH) ;    
        basicFrame.add(scr,BorderLayout.CENTER) ;    
        basicFrame.setSize(500,300) ;
        basicFrame.setLocation(400, 300);
        
        basicFrame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                basicFrame.dispose() ;
            }
        }) ;
        this.addRowBtn.addActionListener(this) ;
        this.removeRowBtn.addActionListener(this) ;
        this.serch.addActionListener(this) ;
        this.save.addActionListener(this);
    } 
    
    public void actionPerformed(ActionEvent evt){
        
        if(evt.getSource() == this.addRowBtn){
            ((DefaultTableModel) this.model).addRow(new Object[]{}) ;
        }
        
        if(evt.getSource()==this.removeRowBtn){
            int rowCount = this.model.getRowCount()-1 ;
            if(rowCount>=0){    
                ((DefaultTableModel) this.model).removeRow(rowCount) ;
                ((DefaultTableModel) this.model).setRowCount(rowCount) ;    
            }
        }
        if(evt.getSource()==this.save){
            int flag = JOptionPane.showConfirmDialog(null, "确定要保存吗?", "确定",
                    JOptionPane.YES_NO_OPTION);
            if (flag == JOptionPane.YES_NO_OPTION) {
                try {
                    this.Save();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        if(evt.getSource() == this.serch){
             String[] title = {"宿舍号","院系","专业","总得分","备注"};
            Object text = JOptionPane.showInputDialog("请输入宿舍号:");
             if(text==null||"".equals(text)){
                 JOptionPane.showMessageDialog(null, "输入信息不能为空!!!");
             }else{
                
                DefaultTableModel tableModel = null;  ;
                frame = new JFrame("排名查询") ;
                
                 
                 try {
                    tableModel = new DefaultTableModel(this.Se(text),title) ;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 table = new JTable(tableModel) ;
                 JScrollPane scr1 = new JScrollPane(table) ;
                    
                // frame.add(pan,BorderLayout.NORTH) ; 
                 frame.add(scr1,BorderLayout.CENTER);
                 frame.setSize(370,150) ;
                 frame.setLocation(200,300);
                 frame.setVisible(true) ;
                 
             }
        }
    }
    
    public Object[][] Save() throws Exception{
        ObjectOutputStream dos = null;
        File f = new File("E:"+File.separator+"text.txt");
        f.createNewFile();
        dos = new ObjectOutputStream(new FileOutputStream(f));
        Object o[][] = new Object[this.model.getRowCount()][this.model.getColumnCount()];
        for(int i=0;i<this.model.getRowCount();i++){
            for(int j=0;j<this.model.getColumnCount();j++){
                o[i][j] = this.model.getValueAt(i,j);    
                System.out.print(this.model.getValueAt(1,j)+" ");
                  dos.writeObject(o);
                  
            }
            dos.writeChar('\n');
            //System.out.println();
        }
        return o;
    }
    public Object[][] Se(Object na) throws Exception{
        int flag = 0,k=0,l=0;
        //Object s[][] = new Object[this.model.getRowCount()][this.model.getColumnCount()];
        for(int i=0;i<this.model.getRowCount();i++){
            //for(int j=0;j<this.model.getColumnCount();j++){
                if(na.equals(this.Save()[i][0])==true){
                    l=i;
                    
                }
            //}
            
        }
        Object s[][] = new Object[1][this.model.getColumnCount()];
        for(int i=0;i<1;i++){
            for(int j=0;j<this.model.getColumnCount();j++){    
                s[i][j] = this.Save()[l][j];
                System.out.print(this.Save()[l][j]+"  "+l);
            }
            
        }
        return s;
    }
}

public class Jf {
    
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //new Jf().getUserInfo();
        new BasicInfoFrame().setPanel();
    }
}

  

当我新增内容并保存后,查询信息会出现一栏没有信息
如图
搜索更多相关主题的帖子: color null 
2014-06-06 21:29
申请幸福
Rank: 1
等 级:新手上路
帖 子:29
专家分:5
注 册:2013-10-25
收藏
得分:0 
新增内容并保存后,查询时会有一栏空的、求解…………
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
2014-06-06 22:08
快速回复:为什么新增表格内容使用 getValueAt 会有空的值出现 null ??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042717 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved