Java programming tutorial with Official Hord - Retrieving Image from database into Label

in #utopian-io6 years ago (edited)

What Will I Learn?

  • You will learn to add an Image column to your database
  • You will learn to collect image from database on table click
  • You will learn to set image as icon for jLabel

Requirements

  • Netbeans IDE
  • XAMPP Server
  • Basic Knowledge of Java Methods

Difficulty

  • Intermediate

Tutorial Contents

Hi guys, from our last tutorial, I explained how to retrieve data from the database and display in text field. This time I'll be explaining how to display a picture that has been uploaded in the database using Image Icon on a jLabel.


Database

To begin with, we'll be adding the Image column to the database first, follow these steps careful.

  1. Add Column, after connecting your Xampp Server and Opening your Table. Set Column name to desired name and Type to LongBlob. Long blob allows you to save Attachments of up to 4,294,967,295 bytes in length. Click save
    image.png
  2. Click on Insert and CLick Choose File to select the Image you want to Upload, then fill other rows with any data you like. image.png

Program

First we add a label to our jFrame Form, this label will hold the image from the database, so rename it to your choice, I have called mine photo. After renaming, right click on the label and go to properties, where you can now set horizontal and vertical size to 150 as seen in the screenshot.
image.png
Now we import ImageIcon Class from Java Swing Library. Using import javax.swing.ImageIcon;, then declare a global variable called format, which would be of type ImageIcon ImageIcon format;. After all these has been done without any errors, we can now add the code to receive the uploaded Image and resize to the same size as the label we have placed in the program for it.

 byte[] image = rs.getBytes("photo"); //This creates an array of bytes to collect Image from database, usually when a file is uploaded in the database,it is saved as an array of bytes which can be compiled back to its normal form when downloaded.
                format = new ImageIcon(image); //this creates an ImageIcon from the bytes array collected from the image in the database.

                Image img = format.getImage(); //this creates an Image, this would be used to carry out the resizing, as ImageIcon cannot be resized directly with this method.
                Image newimage = img.getScaledInstance(photo.getWidth(), photo.getHeight(), Image.SCALE_SMOOTH); // This resizes the Image using size from the label which we have placed in the program earlier.
                ImageIcon images = new ImageIcon(newimage); //this coverts the resized image to an iconImage

image.png
Now we set the icon of our label photo to the ImageIcon created using photo.setIcon(images);
image.png

Run the code and click on the row where the image has been added, you will see the image displayed and scaled to fit in the label added to the interface, as seen in the output screenshot below.
image.png

I hope you understood it all, If not you can ask questions in comments.

Related Videos



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Congratulations!,@official-hord Your post has been upvoted by @reachout via the nigeria tag


Our goal is to support Minnows on Steemit. Join our discord group https://discord.gg/NWAkKfn
Proudly sponsored via SP donation from @eturnerx , @rufans & @solomon158
Upvotes Benefactor : @bleepcoin & the rest of us
###### Join Our Trail here: https://steemauto.com/dash.php?i=15&id=1&user=reachout
Curator On Duty: Richie, the Manual Bot (BETA)

## Also,We'd like invite you to the @eoscafe discord community https://discord.gg/wdUnCdE , be part of something great

Thank you for the contribution It has been approved.


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

Hey @official-hord I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Utopian Witness!

Participate on Discord. Lets GROW TOGETHER!

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 57483.44
ETH 2574.21
USDT 1.00
SBD 2.48