Put firebase data in a JTable

Tags: , , , ,



I am struggling to put some firebase data into a jtable in java swing. As seen in photo I’ve been able to retrieve the data from firebase but for some reason the data won’t be displayed in the JTable field(table1) I looks like the getID(), getName()… methods didn’t get any value, since I tried to print row[i] to see if there is any value.

There is a screenshot of the actual program

Could any of you tell what’s the problem?

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);

Thanks in advance!

Answer

Have you defined the table columns with lines like these?

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

Also, the method setModel should be called after the for:

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

EDIT: Analyzing in more detail, I also verify that you have 7 fields but your array has only space for 6. In this line row [6] = casesList.getRisk (); throws an exception at runtime. Expand the size of your array to solve this. Object[] row = new Object[7];

To display the column names it is convenient to add your table to a scroll panel.

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

This works for me:

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);


Source: stackoverflow