目前我有一个DerbyDB和一个带有JTable的UI。现在我也有一堆课程,但我需要帮助的课程是SensorDBConnection
和myUI
。 myUI
是我的JTable的地方,我想要做的是从查询中更新我的JTable,该查询位于我的SensorDBConnection
以下是我SensorDBConnection
的代码段,因为它正在执行System.outs
DefaultTableModel model;//Confused
model = new DefaultTableModel(); //Confused
jTable1 = new JTable(model);//Confused
model.addColumn("TIME");
model.addColumn("SENSORID");
model.addColumn("TEMP");
model.addColumn("HUM");
model.addColumn("LIGHT");
model.addColumn("VOLTAGE");
while(results.next())
{
time = results.getInt(1);
id = results.getInt(2);
temp = results.getInt(3);
hum = results.getInt(4);
light = results.getInt(5);
voltage = results.getInt(6);
model.addRow(new Object[]{time,id,temp,hum,light,voltage});
Sensor sens = new Sensor(id, temp, hum, light, voltage);
sensors.add(sens);
//System.out.println(sens);
}
results.close();
stmt.close();
基本上我现在正在做的是查询我的数据,然后尝试将它推送到位于我其他类的JTabel上,我不知道如何在它们之间创建链接..
答案 0 :(得分:2)
首先将UI与数据管理分开。不要让SensorDBConnection
返回TableModel
,而是让它返回构建数据模型所需的数据......
例如......
public class SensorDBConnection extends ... {
public List<Sensor> loadData() {
List<Sensor> sensors = new ArrayList<>(25);
try {
//...
while(results.next())
{
time = results.getInt(1);
id = results.getInt(2);
temp = results.getInt(3);
hum = results.getInt(4);
light = results.getInt(5);
voltage = results.getInt(6);
model.addRow(new Object[]{time,id,temp,hum,light,voltage});
Sensor sens = new Sensor(id, temp, hum, light, voltage);
sensors.add(sens);
}
} finally {
try {
results.close();
} catch (Exception exp) {
}
try {
stmt.close();
} catch (Exception exp) {
}
}
return sensors;
}
}
然后,在MyUI
中,您只需请求传感器数据并构建您的TableModel
。为了让您的生活更简单,请创建一个知道如何处理TableModel
的自定义Sensor
...
public class SensorTableModel extends AbstractTableModel {
protected static final String[] COLUMN_NAMES = {
"TIME",
"SENSORID",
"TEMP",
"HUM",
"LIGHT",
"VOLTAGE"
};
private List<Sensor> sensors;
public SensorTableModel(List<Sensor> sensors) {
this.sensors = sensors;
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
public int getRowCount() {
return sensors.size();
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Sensor sensor = sensors.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = sensor.getTime();
break;
case 1:
value = sensor.getID();
break;
case 2:
value = sensor.getTemp();
break;
case 3:
value = sensor.getHum();
break;
case 4:
value = sensor.getLight();
break;
case 5:
value = sensor.getVoltage();
break;
}
return value;
}
}
然后只需在loadData
类的SensorDBConnection
中调用MyUI
方法,只需创建一个已经拥有的TableModel and apply it to the
JTable`实例的实例...
public class MyUI extends ... {
private JTable table;
private SensorDBConnection sensor;
//...
protected void loadTableContents() {
TableModel model = null;
try {
model = new SensorTableModel(sensor.loadData());
} catch (SQLException exp) {
// Handle exception
}
table.setModel(model);
}
}
例如......
答案 1 :(得分:0)
如果我正确理解了您的问题,您应该能够在myUI中创建SensorDBConnection的实例。这样你就可以在myUI中调用SensorDBConnection的方法,查询等等。
答案 2 :(得分:0)
我会创建自己的TableModel子类,该子类特定于返回的数据。然后,一旦将TableModel中的数据设置为新数据,请调用fireTableDataChanged()。