I am trying to display data on the jTable, but it doesn’t show up. I believe the main problem is in setObj() method (at the end of the code) that is called from a different JFrameForm (Database Extraction) with the appropriate input. I am able to print the correct number of rows but the table is not displaying the data.
This is the code that passes the data to the DriverList JFrameForm
//Database Extraction DriverList DList = new DriverList(); DList.setObj(driver.getName() , driver.getTeam() , driver.getNum() ,driver.getNation() , driver.getAI());
This the JFrameForm with the table that is supposed to display the data. In the setOBJ() method I tried to print the number of rows in the model and I was able to get the correct answer of 20. But the Table is still empty.
// DriverList import javax.swing.table.DefaultTableModel; public class DriverList extends javax.swing.JFrame { public DriverList() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); jTable1.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { jTable1FocusGained(evt); } }); jScrollPane1.setViewportView(jTable1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(94, 94, 94) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(142, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(54, 54, 54) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(117, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jTable1FocusGained(java.awt.event.FocusEvent evt) { } public static void main(String args[]) { //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(DriverList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DriverList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DriverList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DriverList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> java.awt.EventQueue.invokeLater(() -> { new DriverList().setVisible(true); }); } public void setObj(Object name, Object team, Object num, Object nation, Object ai) { String[] cnames = {"Name","Team","Number","Nation","AI"}; DefaultTableModel model = new DefaultTableModel(cnames,0); String[] NAME = (String[]) name; model.setRowCount(0); byte[] TID = (byte[]) team; byte[] NUM = (byte[]) num; byte[] NATION = (byte[]) nation; byte[] AI = (byte[]) ai; for(int i = 0; i<20 ; i++){ Object[] row = {NAME[i],TID[i],NUM[i],NATION[i],AI[i]}; model.insertRow(i, row); } jTable1.setModel(model); System.out.println("total rows"+model.getRowCount()); } // Variables declaration - do not modify private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration }
Anonymous Asked question May 14, 2021
Recent Comments