程序运行时jtable不会更新

时间:2016-10-08 10:46:19

标签: java swing oop user-interface jtable

好吧我已经发布了这个,但是我删除它因为没有人已经24小时回答

我真的不知道该怎么做了。我的系统中有这么多表,它们都有相同的代码,但这是唯一不起作用的。我的问题是每次我将候选/数据添加到jtable时,它都不会出现。它只会在我终止程序然后再次运行时出现。这是我的代码:

这是包含存储到文件和jtable方法的类

    public class DatabaseForCandidates {
        String pres; 
        int resPres;
        static Vector rowsPres;
        static String [] columnPres={"PRESIDENTIAL CANDIDATES", "CURRENT NUMBER OF VOTES", ""};
        static File filePres;
        static FileWriter fileWrite;
        static FileReader fileRead;
        static Scanner read;

        public DatabaseForCandidates() {
            filePres=new File("President.txt");
            fileWrite=null;
            fileRead=null;
            read=null;
        }
        public void setColumns(){
            PresidentTable.tblModel=new DefaultTableModel();
            PresidentTable.tblModel.setColumnIdentifiers(columnPres);
            PresidentTable.tblNatPresident=new JTable(PresidentTable.tblModel);
        }

    public void storePresidentRecords(){
            try {
                fileWrite=new FileWriter(filePres,true);
                getDataPres();  
                storeToTable();

                //storing to a file
                fileWrite.write("**"+pres+"**"); fileWrite.write(resPres+"**");
            fileWrite.write("\r\n");

            fileWrite.close();
            JOptionPane.showMessageDialog(null, "Candidate is now nominated for President");
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Error: "+e.getMessage());
        }
    }

    public void getDataPres(){
        String linePres="";
        try {
            fileRead=new FileReader(Database.fileNew);
            read=new Scanner(fileRead);

            while(read.hasNext()){
                linePres+=read.nextLine()+"\n";
            }
            read.close();

            String [] infoPres=linePres.split("/");

            pres=infoPres[4]+" "+infoPres[3];
            resPres=0;
        }catch(Exception e){}
    }

    public void storeToTable(){
        rowsPres=new Vector();
        rowsPres.add(pres); rowsPres.add(resPres);
        PresidentTable.tblModel.addRow(rowsPres);
    }

    public void retrievePresidentRecords(){
        String holdStr="";
        try {
            fileRead=new FileReader(filePres);
            read=new Scanner(fileRead);

            while(read.hasNext()){
                holdStr+=read.nextLine()+"\n";
            }
            read.close();

            StringTokenizer strToken=new StringTokenizer(holdStr, "**");

            while(strToken.hasMoreElements()){
                rowsPres=new Vector();
                for(int i=0; i<columnPres.length; i++){
                    rowsPres.add(strToken.nextElement());
                }
                PresidentTable.tblModel.addRow(rowsPres);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

这是jtable的类

public class PresidentTable extends JPanel{
    static JTable tblNatPresident;
    static DefaultTableModel tblModel;
    DatabaseForCandidates data;

    PresidentTable(){
        setName("panelNatPres");

        data=new DatabaseForCandidates();

        data.setColumns();
        data.retrievePresidentRecords();

        PresidentTable.tblNatPresident=new JTable(PresidentTable.tblModel);
        add(new JScrollPane(PresidentTable.tblNatPresident));
    }
}

如果你要添加候选人

,这是按钮的监听器
if(e.getSource().equals(PresidentPanel.btnPresAdd)){
            DatabaseForCandidates data=new DatabaseForCandidates();
            boolean found=false;
            try {
                String add=PresidentPanel.txtVNum.getText();
                String vnum=null;
                for (int row=0; (row<ViewTablePanel.tblModel.getRowCount()) && (!found);row++) {
                    vnum=ViewTablePanel.tblModel.getValueAt(row, 1).toString();
                    if(vnum.equals(add)){
                        found=true;
                        data.storePresidentRecords();
                    }
                }
                if(!found){
                    JOptionPane.showMessageDialog(null, add+" is not found.");
                }
            } catch (Exception e1) {}
        }

请问我是否不理解我的代码以及是否有任何澄清。请非常感谢你:))

1 个答案:

答案 0 :(得分:0)

如果没有发布整个代码,很难调试。然而,有些事情可能会出错:

  1. 您未通过致电通知JTable数据已更改: .fireTableDataChanged()
  2. 您不能通过以下方式更新相框: .repaint(); .revalidate();
  3. 您更新了文件,但未阅读文件更新或以其他方式更新JTable的更改。