我想将数组列表中的对象添加到Jtable中。没有例外或错误。对象正在添加到数组列表中,并且还在控制台上打印但不添加JTable ....这里是代码....
public class Table extends javax.swing.JFrame {
ArrayList<Data>dataList = new ArrayList<Data>();
public Table() {
initComponents();
connection ();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jInternalFrame1 = new javax.swing.JInternalFrame();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jInternalFrame1.setVisible(true);
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(600, 400));
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, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null}
},
new String [] {
"Date", "Node ID", "Voltage", "Temperature", "CO", "CO2", "Avability"
}
) {
Class[] types = new Class [] {
java.lang.Long.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jTable1.setMaximumSize(new java.awt.Dimension(540, 380));
jScrollPane1.setViewportView(jTable1);
jButton1.setText("Fetch Records");
jButton2.setText("Specific Node");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(87, 87, 87))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public void connection (){
for(int i=0;i<5; i++){
Data data = new Data(i, 5.5, 32.0, 50, 60);
dataList.add(data);
System.out.println(dataList.get(i).getId());
System.out.println(dataList.get(i).getVoltage());
System.out.println(dataList.get(i).getTemperature());
System.out.println(dataList.get(i).getCO());
System.out.println(dataList.get(i).getCO2());
}
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
//model.setRowCount(dataList.size());
Object rowData[]=new Object[5];
int row = 0;
for(int i=0; i< 5; i++){
rowData[0]=dataList.get(i).getId();
rowData[1]=dataList.get(i).getVoltage();
rowData[2]=dataList.get(i).getTemperature();
rowData[3]=dataList.get(i).getCO();
rowData[4]=dataList.get(i).getCO2();
model.addRow(rowData);
}
table.setModel(model);
table.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Table().setVisible(true);
}
});
System.out.println();
}
答案 0 :(得分:1)
在你的连接方法中,你正在创建一个JTable并用数据填充它,但从不将它添加到GUI。我怀疑你想要做的是改变现有JTable中的数据,对吧?如果是这样的话,你就不会这样做了。您需要更新现有JTable表模型中的数据,或者将现有JTable的TableModel
设置为新的TableModel
public void connection (){
for(int i=0;i<5; i++){
Data data = new Data(i, 5.5, 32.0, 50, 60);
dataList.add(data);
System.out.println(dataList.get(i).getId());
System.out.println(dataList.get(i).getVoltage());
System.out.println(dataList.get(i).getTemperature());
System.out.println(dataList.get(i).getCO());
System.out.println(dataList.get(i).getCO2());
}
DefaultTableModel model = new DefaultTableModel();
/// ***** HERE IS WHERE YOU CREATE THE NEW JTABLE AND NEVER ADD IT TO ANYTHING ***** ///
JTable table = new JTable(model);
//model.setRowCount(dataList.size());
Object rowData[]=new Object[5];
int row = 0;
for(int i=0; i< 5; i++){
rowData[0]=dataList.get(i).getId();
rowData[1]=dataList.get(i).getVoltage();
rowData[2]=dataList.get(i).getTemperature();
rowData[3]=dataList.get(i).getCO();
rowData[4]=dataList.get(i).getCO2();
model.addRow(rowData);
}
table.setModel(model);
table.setVisible(true);
}