将firebase数据放入jtable

vu8f3i0k  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(366)

我正在努力将一些firebase数据放入javaswing中的jtable中。如图所示,我已经能够从firebase中检索数据,但是由于某些原因,数据不会显示在jtable字段(表1)中,我看起来像getid(),getname()。。。方法没有得到任何值,因为我试图打印行[i]以查看是否有任何值。
有一个实际程序的截图
你们谁能告诉我有什么问题吗?

addNewCaseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                database = FirebaseDatabase.getInstance();
                ref = database.getReference("NewCases");
                ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        System.out.println(dataSnapshot.getValue());

                        ArrayList<Object[]> myList = new ArrayList<>();
                        DefaultTableModel tableModel = (DefaultTableModel) table1.getModel();
                        tableModel.addColumn("ID");
                        tableModel.addColumn("Name");
                        tableModel.addColumn("Age");
                        tableModel.addColumn("City");
                        tableModel.addColumn("TimeStamp");
                        tableModel.addColumn("Disease");
                        tableModel.addColumn("Risk");
                        Object[] row = new Object[6];
                        tableModel.setNumRows(0);
                        myList.clear();

                        CasesList casesList = new CasesList();

              for (DataSnapshot ds : dataSnapshot.getChildren()){

               casesList.setID((Integer)ds.child("ID").getValue());

               casesList.setName(ds.child("Name").getValue().toString());

               casesList.setAge((Integer)ds.child("Age").getValue());

               casesList.setCity(ds.child("City").getValue().toString());

         casesList.setTimeStamp(ds.child("TimeStamp").getValue().toString());

         casesList.setDisease(ds.child("Disease").getValue().toString());

         casesList.setRisk(ds.child("Risk").getValue().toString());
//       myList.add(ds.getKey());

                            row[0] = casesList.getID();
                            row[1] = casesList.getName();
                            row[2] = casesList.getAge();
                            row[3] = casesList.getCity();
                            row[4] = casesList.getTimeStamp();
                            row[5] = casesList.getDisease();
                            row[6] = casesList.getRisk();
                            myList.add(row);

                            System.out.println(myList);
                            tableModel.addRow(row);
                        }
                        table1.setModel(tableModel);

提前谢谢!

fkaflof6

fkaflof61#

您用这样的行定义了表列吗?

DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("ID");
tableModel.addColumn("Name");
tableModel.addColumn("Age");

另外,方法 setModel 应在以下时间后调用:

for... {
      //add row to tableModel 
  }
  table1.setModel(tableModel);

编辑:更详细地分析,我还验证了您有7个字段,但您的数组只有6个字段的空间。在这条线上 row [6] = casesList.getRisk (); 在运行时引发异常。扩展数组的大小以解决此问题。 Object[] row = new Object[7]; 要显示列名,可以方便地将表添加到滚动面板中。

JScrollPane scrollPane = new JScrollPane(table1);
frame.getContentPane().add(scrollPane);

这对我很有用:

ArrayList<Object[]> myList = new ArrayList<>();
DefaultTableModel tableModel = (DefaultTableModel) table1.getModel();
tableModel.addColumn("ID");
tableModel.addColumn("Name");
tableModel.addColumn("Age");
tableModel.addColumn("City");
tableModel.addColumn("TimeStamp");
tableModel.addColumn("Disease");
tableModel.addColumn("Risk");
Object[] row = new Object[7];
tableModel.setNumRows(0);
myList.clear();

for (DataSnapshot ds : dataSnapshot.getChildren()) {

    CasesList casesList = new CasesList();
    casesList.setID((Integer) ds.child("ID").getValue());
    casesList.setName(ds.child("Name").getValue().toString());
    casesList.setAge((Integer) ds.child("Age").getValue());
    casesList.setCity(ds.child("City").getValue().toString());
    casesList.setTimeStamp(ds.child("TimeStamp").getValue().toString());
    casesList.setDisease(ds.child("Disease").getValue().toString());
    casesList.setRisk(ds.child("Risk").getValue().toString());

    row[0] = casesList.getID();
    row[1] = casesList.getName();
    row[2] = casesList.getAge();
    row[3] = casesList.getCity();
    row[4] = casesList.getTimeStamp();
    row[5] = casesList.getDisease();
    row[6] = casesList.getRisk();

    myList.add(row);

    System.out.println(myList);
    tableModel.addRow(row);

}
table1.setModel(tableModel);

相关问题