Results 1 to 11 of 11
  1. #1
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Displaying image in jLabel

    hi everyone,
    I have a project which am yet to complete because i am struggling to get some code to work. I have a custom tableModel that retrieves data from mysql database and when selected, the data is also displayed in textfield. my problem is that, i have a column with images that am trying to display on jlabel when it is selected. I need help with code sample if possible.

    thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Displaying image in jLabel

    The code samples should begin with your code, since we can help you, but we're not here to do your homework or field work for you. So show us what you've got so far. For better help, also please ask more specific questions: what have you tried, and where exactly have you tried and how is it now working?
    Last edited by Fubarable; 05-16-2012 at 10:50 PM.

  3. #3
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: Displaying image in jLabel

    thanks for your quick response. this is work i have done so far in attempt to display the image on jlabel:

    Java Code:
    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {                                     
            // TODO add your handling code here:
            // display jtable values on textfield when it is selected
            int row = jTable2.getSelectedRow();
            
            
            for (int i = 0; i <= row; i++)
            {
                
                String empId = (String) jTable2.getValueAt(i, 0);
                txtEmployeeId.setText(empId);
                
                String name = (String) jTable2.getValueAt(i, 1);
                txtName.setText(name);
                
                Object phone = jTable2.getValueAt(i, 2);
                txtPhone.setText(phone.toString());
                
                String email = (String) jTable2.getValueAt(i, 3);
                txtEmail.setText(email);
                
                String address = (String) jTable2.getValueAt(i, 4);
                txaAddress.setText(address);
                
                String refereeName = (String) jTable2.getValueAt(i, 5);
                txtRefereeName.setText(refereeName);
                
                Object refereePhone = jTable2.getValueAt(i, 6);
                txtRefereePhone.setText(refereePhone.toString());
                
                String refereeAddress = (String) jTable2.getValueAt(i, 7);
                txaRefereeAddress.setText(refereeAddress);
                
                String category = (String) jTable2.getValueAt(i, 8);
                txtCategory.setText(category);
                
                String remarks = (String) jTable2.getValueAt(i, 9);
                txaRemarks.setText(remarks);
                
                String status = (String) jTable2.getValueAt(i, 10);
                txtStatus.setText(status);
                
                String pic = jTable2.getValueAt(i, 11).toString();
                byte [] photo = pic.getBytes();
                Image img = lblPhoto.getToolkit().createImage(photo);
                lblPhoto.setIcon(new ImageIcon(img));
                   
            }
            
        }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Displaying image in jLabel

    Does the JTable already show an image in the column? If so, what type of object is held in that column? Is it an ImageIcon (which would likely work best for you)?

    And it seems like what you're trying to do -- create an image from the bytes an object's toString representation -- is doomed to failure. toString() is supposed to return readable text, and there's no way that this will translate into the bytes of an actual image. Have you printed out exactly what that call to toString at the bottom of your post above will return? Take a look at it and let us know please.

  5. #5
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: Displaying image in jLabel

    the column doesn't show image. it is holding a byte data in that column coz the images were converted to byte during insertion. below is the code that inserts the image into the database:

    Java Code:
    
    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here: 
            // adding data to database
            // create connection and prepare statement
              
            try{ 
                
            
                connection = DriverManager.getConnection(DATABASE_URL);
                insertNewData = connection.prepareStatement("INSERT INTO Employee" +
                   "(Employees_Id, Name, Phone,Email, Address, Referee_Name,Referee_Phone, Referee_Address, Category, Remarks,Status, Image)" +
                    "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");   
             
                // declare variables to store textfield data
                String t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;
                t1 = txtEmployeeId.getText();
                t2 = txtName.getText();
                t3 = txtPhone.getText();
                t4 = txtEmail.getText();
                t5 = txaAddress.getText();
                t6 = txtRefereeName.getText();
                t7 = txtRefereePhone.getText();
                t8 = txaRefereeAddress.getText();
                t9 = txtCategory.getText();
                t10 = txaRemarks.getText();
                t11 = txtStatus.getText();
                
                
                        
                
                // initializing photo to save in database
                try{
                ByteArrayOutputStream photo = new ByteArrayOutputStream();
                ImageIO.write( image, "jpg", photo );
                photo.flush();
                imageInByte = photo.toByteArray();
                photo.close();
                }
                catch(FileNotFoundException Ffe)
                {
                    Ffe.printStackTrace();
                JOptionPane.showMessageDialog(null, Ffe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    
                }
                catch(IOException ioException)
                {
                   ioException.printStackTrace();
                JOptionPane.showMessageDialog(null, ioException.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    
                }
                
                
            
             //add entries
            int result = 0;
            
           // set parameters, then execute insertNewData
            
            insertNewData.setString(1,t1);
            insertNewData.setString(2, t2);
            insertNewData.setString(3, t3);
            insertNewData.setString(4, t4);
            insertNewData.setString(5, t5);
            insertNewData.setString(6, t6);
            insertNewData.setString(7, t7);
            insertNewData.setString(8, t8);
            insertNewData.setString(9, t9);
            insertNewData.setString(10, t10);
            insertNewData.setString(11, t11);
            insertNewData.setBytes(12, imageInByte);
            
            
            
            
            // insert the new data
            result = insertNewData.executeUpdate();
            
                
            Update = true;
            
            lblUpdate.setText("Record Added");
            
             // reset text fields
                txtEmployeeId.setText("");
                txtName.setText("");
                txtPhone.setText("");
                txtEmail.setText("");
                txaAddress.setText("");
                txtRefereeName.setText("");
                txtRefereePhone.setText("");
                txaRefereeAddress.setText("");
                txtCategory.setText("");
                txaRemarks.setText("");
                txtStatus.setText("");
                lblPhoto.setIcon(null);
                
                // call method refresh
                Refresh();
          
            
            }
            catch (SQLException sqlException)
            {
                sqlException.printStackTrace();
                JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                try{
                connection.close();
                }
                catch (SQLException sqlException2)
                {
                   sqlException2.printStackTrace();
                    JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
                
                
               
                
            }
               
        }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Displaying image in jLabel

    OK, I'm guessing here, as I've never done this before, and since I don't have your code to test, I can only guess, but it looks like your table holds a byte array in that last column, and it makes perfect sense that calling toString() on this won't work since it will return nonsense as far as your concerned -- [B followed by the hashCode.

    So instead, why not cast the object returned as a byte array, (byte[]), initiate a ByteInputStream with this byte array and then call ImageIO.read(...) passing in the ByteInputStream. This could possibly get you a BufferedImage, and then with this you can create your ImageIcon.


    Edit:
    oops, that's ByteArrayInputStream.

    Something like:

    Java Code:
    byte[] byteArray = (byte[]) jTable2.getValueAt(i, 11);
    ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
    BufferedImage bImg = ImageIO.read(bais);
    ImageIcon icon = new ImageIcon(bImg);
    lblPhoto.setIcon(icon);
    bais.close();
    Last edited by Fubarable; 05-16-2012 at 11:14 PM.

  7. #7
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: Displaying image in jLabel

    i will try your suggestion. thank you for your help. really appreciated

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Displaying image in jLabel

    Quote Originally Posted by victaino View Post
    i will try your suggestion. thank you for your help. really appreciated
    You're welcome. Please let us know how it turns out.

  9. #9
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: Displaying image in jLabel

    it worked. thank you so much. now i need to scale the image to fit the jlabel cox it come up very large to disorganise my UI arrangement. How do i go about that? Thank you so much. You are a life saver.

  10. #10
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: Displaying image in jLabel

    ok. got it sorted. was able to scale the image. Thanks

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Using Image in JLabel? Please Help! :-)
    By Alerhau in forum New To Java
    Replies: 9
    Last Post: 09-07-2011, 06:48 PM
  2. Replies: 1
    Last Post: 03-19-2011, 11:06 PM
  3. Image On a JLabel
    By corbokhan in forum New To Java
    Replies: 11
    Last Post: 11-06-2010, 12:18 AM
  4. Image in JLabel
    By ishere11 in forum AWT / Swing
    Replies: 2
    Last Post: 04-29-2010, 08:02 AM
  5. Replies: 2
    Last Post: 02-15-2010, 06:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •