0

I want to display an image from the MYSQL database of type BLOB data to the Jtable cell, but when displayed to the JTable cell line instead of image but the url address.

Example output, one of the number of rows:

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

note the example output above, in the table column dtl_profile_img = this should be shown images not url address.

This is the last my programming source code:

Code 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));

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

Method displays data to 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;
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dewot 9
  • 37
  • 9
  • This really has nothing to do with databases, tables or the IDE. It is about displaying the image at an URL instead of the URL. If you cannot figure that out in a simple `JLabel`, post a [mcve] of your attempt. One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 10 '18 at 15:43

1 Answers1

1

A couple of problems:

  1. When you create an ImageIcon with a String, then the file must be local. If the file is external then you need to specify a URL as the parameter to the ImageIcon constructor, not a String.

  2. The default renderer for a JTable will just display the toString() value of any object added to the model. If you want to display the ImageIcon, then you need to override the getColumnClass(...) method of the TableModel to return Icon.class for that column so the table can use the correct renderer.

Read the section from the Swing tutorial on Renderers and Editors for more information and examples on this concept.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Concerning (2): When looking at method [JTable.createDefaultRenderers()](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/JTable.java#5283) it seems that `ImageIcon.class` would work as fine as `Icon.class`. – Thomas Fritsch Feb 10 '18 at 16:02
  • @ThomasFritsch, correct, I just use the top level class so you aren't restricted to a specific type. – camickr Feb 10 '18 at 17:33