繁体   English   中英

如何显示从MYSQL到JTable单元格行的图像?

[英]how to display image from MYSQL to JTable cell rows?

我想将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地址。

这是我最后的编程源代码:

代码定制器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. 使用String创建ImageIcon时,该文件必须是本地文件。 如果文件是外部文件,则需要指定URL作为ImageIcon构造函数的参数,而不是String

  2. JTable的默认渲染器将仅显示添加到模型中的任何对象的toString()值。 如果要显示ImageIcon,则需要重写TableModel的getColumnClass(...)方法以返回该列的Icon.class ,以便表可以使用正确的渲染器。

阅读Swing教程中有关渲染器和编辑器的部分,以获取有关此概念的更多信息和示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM