如何在填充 table 的同时在 jtable 中设置图标?
How to set icon in a jtable while filling the table?
我试图根据条件在特定列中放置一个图标,但得到的只是图像的 link。
这就是 table 代码的启动方式:
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
case 1:
return Integer.class;
case 2:
return ImageIcon.class;
default:
return Object.class;
}
}
};
然后我根据条件(已完成-尚未开始-正在进行中)从我的数据库中填充 table:
model.addColumn("Phases");
model.addColumn("Date de début estimée(aaaa/mm/jj)");
model.addColumn("Date de début réelle(aaaa/mm/jj)");
model.addColumn("Date de fin estimée(aaaa/mm/jj)");
model.addColumn("Date de fin réelle(aaaa/mm/jj)");
model.addColumn("Status");
model.addColumn("Tendance");
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> last_name = new ArrayList<String>();
String status = null;
String tendance = null;
byte[] Image = null;
// ImageIcon image2 = new ImageIcon("C:\Users\badr\Documents\NetBeansProjects\UPLINE_GROUP\src\upline_group\Ok-48.png");
Date estimated_end_date = null;
Date real_end_date = null;
try {
String sql = "select * from task WHERE id_project='" + id_project1 + "' ORDER BY nom_task ASC";
PST = conn.prepareStatement(sql);
RS = PST.executeQuery();
while (RS.next()) {
try {
String sql2 = "select * from image ";
PST2 = conn2.prepareStatement(sql2);
RS2 = PST2.executeQuery();
while (RS2.next()) {
if (RS.getString("real_start_date") == null && RS.getString("real_end_date") == null) {
status = "0(Non Démarrée)";
//tendance = "0(Non Démarrée)";
Image = RS2.getBytes("non");
}
if (RS.getString("real_start_date") != null && RS.getString("real_end_date") == null) {
status = "1(en cours)";
// tendance = "1(en cours)";
Image = RS2.getBytes("cours");
}
if (RS.getString("real_end_date") != null) {
status = "2(Terminé)";
// tendance = "2(Terminé)";
Image = RS2.getBytes("fini");
}
}
} catch (Exception e) {
System.err.println(e);
}
estimated_end_date = RS.getDate("estimated_end_date");
real_end_date = RS.getDate("real_end_date");
model.addRow(new Object[]{RS.getString("nom_task"), RS.getDate("estimated_start_date"), RS.getDate("real_start_date"), estimated_end_date, real_end_date, status,Image});
}
} catch (Exception e) {
System.err.println(e);
}
jTable1.setModel(model);
but all get is the link of the image :
那么这意味着您正在该列中存储文本。
我看到你有几个问题:
您的代码正在尝试读取图像,但您需要在 TableModel
中存储一个 ImageIcon
。因此,您需要将 addRow(...)
语句更改为使用:
addRow(..., new ImageIcon(image));
因此,您可以按照希望它们在 table 中显示的顺序添加每个值。
修复上述语句后,图标似乎将成为 addRow(...)
语句的第 6 个参数。但是,根据您的 getColumnClass(...)
方法,图标位于第 2 列。你不觉得奇怪吗?
我试图根据条件在特定列中放置一个图标,但得到的只是图像的 link。
这就是 table 代码的启动方式:
DefaultTableModel model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
case 1:
return Integer.class;
case 2:
return ImageIcon.class;
default:
return Object.class;
}
}
};
然后我根据条件(已完成-尚未开始-正在进行中)从我的数据库中填充 table:
model.addColumn("Phases");
model.addColumn("Date de début estimée(aaaa/mm/jj)");
model.addColumn("Date de début réelle(aaaa/mm/jj)");
model.addColumn("Date de fin estimée(aaaa/mm/jj)");
model.addColumn("Date de fin réelle(aaaa/mm/jj)");
model.addColumn("Status");
model.addColumn("Tendance");
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> last_name = new ArrayList<String>();
String status = null;
String tendance = null;
byte[] Image = null;
// ImageIcon image2 = new ImageIcon("C:\Users\badr\Documents\NetBeansProjects\UPLINE_GROUP\src\upline_group\Ok-48.png");
Date estimated_end_date = null;
Date real_end_date = null;
try {
String sql = "select * from task WHERE id_project='" + id_project1 + "' ORDER BY nom_task ASC";
PST = conn.prepareStatement(sql);
RS = PST.executeQuery();
while (RS.next()) {
try {
String sql2 = "select * from image ";
PST2 = conn2.prepareStatement(sql2);
RS2 = PST2.executeQuery();
while (RS2.next()) {
if (RS.getString("real_start_date") == null && RS.getString("real_end_date") == null) {
status = "0(Non Démarrée)";
//tendance = "0(Non Démarrée)";
Image = RS2.getBytes("non");
}
if (RS.getString("real_start_date") != null && RS.getString("real_end_date") == null) {
status = "1(en cours)";
// tendance = "1(en cours)";
Image = RS2.getBytes("cours");
}
if (RS.getString("real_end_date") != null) {
status = "2(Terminé)";
// tendance = "2(Terminé)";
Image = RS2.getBytes("fini");
}
}
} catch (Exception e) {
System.err.println(e);
}
estimated_end_date = RS.getDate("estimated_end_date");
real_end_date = RS.getDate("real_end_date");
model.addRow(new Object[]{RS.getString("nom_task"), RS.getDate("estimated_start_date"), RS.getDate("real_start_date"), estimated_end_date, real_end_date, status,Image});
}
} catch (Exception e) {
System.err.println(e);
}
jTable1.setModel(model);
but all get is the link of the image :
那么这意味着您正在该列中存储文本。
我看到你有几个问题:
您的代码正在尝试读取图像,但您需要在 TableModel
中存储一个 ImageIcon
。因此,您需要将 addRow(...)
语句更改为使用:
addRow(..., new ImageIcon(image));
因此,您可以按照希望它们在 table 中显示的顺序添加每个值。
修复上述语句后,图标似乎将成为 addRow(...)
语句的第 6 个参数。但是,根据您的 getColumnClass(...)
方法,图标位于第 2 列。你不觉得奇怪吗?