我想将图像从类型BLOB数据的MySQL数据库显示为JTable单元格,但是当显示到JTable单元线时,而不是图像,而不是图像,而是URL地址。
示例输出,行数之一:
current (error), one row: id_status = 123
dtl_profile_img = http://aaa.com/mario.png
dtl_username = mario
dtl_status = good
create_date = 2018-01-01 08:08:08.0
请注意上面的示例输出,在表列dtl_profile_img =应该显示图像不是URL地址。
这是我的编程源代码的最后一个:
代码customizer jtable
///////////////////////////////// Code Customizer JTable ////////////////////////////////////
jtbDetailEmployees = new javax.swing.JTable();
jtbDetailEmployees.setBackground(new java.awt.Color(216, 216, 216));
jtbDetailEmployees.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
jtbDetailEmployees.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
jtbDetailEmployees.setForeground(new java.awt.Color(255, 153, 0));
jtbDetailEmployees.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}
},
new String [] {
"ID Status", "dtl_profile_img", "dtl_username", "dtl_status", "create_date"
}
) {
Class[] type= new Class[]{
java.lang.String.class, javax.swing.ImageIcon.class, java.lang.String.class, java.lang.String.class,java.lang.String.class
};
boolean[] canEdit = new boolean [] {
true, true, true, true, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jtbDetailEmployees.setGridColor(new java.awt.Color(220, 220, 220));
jtbDetailEmployees.setOpaque(false);
jtbDetailEmployees.setRowHeight(35);
jtbDetailEmployees.setSelectionBackground(new java.awt.Color(204, 204, 204));
jtbDetailEmployees.setSelectionForeground(new java.awt.Color(255, 153, 0));
jtbDetailEmployees.setShowHorizontalLines(false);
jScrollPane1.setViewportView(jtbDetailEmployees);
if (jtbDetailEmployees.getColumnModel().getColumnCount() > 0) {
jtbDetailEmployees.getColumnModel().getColumn(0).setResizable(false);
jtbDetailEmployees.getColumnModel().getColumn(4).setResizable(false);
}
JTableHeader header =jtbDetailEmployees.getTableHeader();
header.setOpaque(false);
header.setBackground(new Color(12,44,54));
header.setForeground(Color.WHITE);
((DefaultTableCellRenderer)header.getDefaultRenderer())
.setHorizontalAlignment(JLabel.CENTER); // center header text
//jtbDetailEmployees.setShowGrid(false);
//jtbDetailEmployees.setShowHorizontalLines(false);
//jtbDetailEmployees.setShowVerticalLines(false);
jtbDetailEmployees.getTableHeader().setFont(new Font("SansSerif", Font.ITALIC, 12));
////////////////////////////////////////////////////////////////////////////////////////////////////
方法将数据显示为JTable
////////////////////////////////method displays data to JTable///////////////////////////////////////
public ArrayList qSELECT_tbl_dtlEmployees() throws SQLException {
ArrayList<dtlEmployees> alistTblEmployees = new ArrayList<>();
try {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
//ScheduledFuture<?> future = executor.schedule(task, 3, TimeUnit.SECONDS);
String qu = "SELECT id_status, dtl_profile_img, dtl_username, dtl_status, create_date"
+ " FROM tbl_dtl_employees "
+ " WHERE id_employees ='"+strIDEmployees+"'"
+ " LIMIT "+OffsetKe+",100";
DatabaseHandler handler = DatabaseHandler.getInstance();
ResultSet rs = handler.execQuery(qu);
System.out.println("query tbl_dtl_Employees :"+qu);
DefaultTableModel mdlListEmployees = (DefaultTableModel) jtbDetailEmployees.getModel();
Object objRecordEmployees[] = new Object[5];
try {
stopSelect_dtlEmployees_Tweets = false;
while (rs.next()) {
objRecordPengambilaEmployees[0] = rs.getString("id_status");
objRecordEmployees[1] = new ImageIcon(rs.getString("dtl_profile_img"));
objRecordEmployees[2] = rs.getString("dtl_username");
objRecordEmployees[3] = rs.getString("dtl_status");
objRecordEmployees[4] = rs.getString("create_date");
mdlListEmployees.addRow(objRecordEmployees);
}
} catch (SQLException ex) {
Logger.getLogger(alertHasil_PeriodTweets1.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (SQLException ex) {
Logger.getLogger(pnlPeriod_Tweets.class.getName()).log(Level.SEVERE, null, ex);
}
return alistTblEmployees;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
有几个问题:
-
使用
String
创建ImageIcon
时,该文件必须是本地的。如果文件是外部的,则需要将URL
指定为ImageIcon
构造函数的参数,而不是String
。 -
JTable的默认渲染器只会显示添加到模型中的任何对象的toString((值。如果要显示Imageicon,则需要覆盖TableModel的
getColumnClass(...)
方法以返回该列的Icon.class
,以便表可以使用正确的渲染器。
阅读有关渲染器和编辑器的Swing教程的部分,以获取有关此概念的更多信息和示例。