Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying JPG images in JLabel  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to working with files in Java and I'm stuck. I have a simple GUI interface that has a number of JLabels. My objective is to:

1. Allow the user to pick a directory that has .jpg files in it (I've got this code already)
2. Read through the directory the user choose and populate an Array with each of the .jpg files in the directory (there could be up to 100)
3. Take the first 6 .jpg images (from the array in the previous step) and populate them in the JLabels visible on the screen

I know it's simple but I've done Google searches and watched videos on YouTube but can't find exactly what I'm describing above. Any help would be appreciated

Sincerely,

Mr. Newbie
 
Rancher
Posts: 3164
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
Post your current code so we can see.  Be sure to wrap the code in code tags.  Use the Code button that is above the input area.
 
Chris Whitcomb
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have much code because I don't really even know where to begin:


 
Norm Radder
Rancher
Posts: 3164
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

where to begin:


Start by reading the API doc for the JLabel class and see how it can be used to display an image.
Then look at the code needed to read the image file and prepare it to be displayed in the JLabel.

Then write a small simple program that defines a JFrame and a JLabel.  Add the JLabel to the JFrame and read the image file and put it in the JLabel.
And that is all that should be done for this simple test program.

Post the code for the simple program if you need help getting it to work.
 
Rancher
Posts: 506
15
Java Notepad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the API javadocs, Oracle's Java tutorials is another place you may want to look into: https://docs.oracle.com/javase/tutorial/

I have often referred to these and found answers. At the above mentioned webpage you will find a link to something like "Creating a GUI with Swing", where one can lookup sample code for all the Swing GUI components (including JLabel and the usage of JPG images with JLabel).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!