gpt4 book ai didi

java - 如何将图像从MYSQL显示到JTable单元格行?

转载 作者:行者123 更新时间:2023-12-02 11:38:54 24 4
gpt4 key购买 nike

我想将来自MYSQL数据库的BLOB类型数据的图像显示到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 地址。

这是我最后的编程源码:

代码定制器 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;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

最佳答案

几个问题:

  1. 当您创建ImageIcon时与 String ,那么该文件必须是本地的。如果文件是外部的,那么您需要指定 URL作为 ImageIcon 的参数构造函数,而不是 String .

  2. JTable 的默认呈现器将仅显示添加到模型中的任何对象的 toString() 值。如果你想显示ImageIcon,那么你需要覆盖getColumnClass(...) TableModel 的方法返回 Icon.class对于该列,以便表可以使用正确的呈现器。

阅读 Swing 教程中关于 Renderers and Editors 的部分有关此概念的更多信息和示例。

关于java - 如何将图像从MYSQL显示到JTable单元格行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com