• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Import image into JPanel using JFileCHOOSER

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My java code uses a button to open up JFileChooser. What I want to do is use file chooser to select a image and then place it into the Panel. Right now in the Panel only 1 objects is declared which is the button. So right now a place to display a image is not defined anywhere in this code. I don't know If I should do that in action event or in JFrame.



 
Saloon Keeper
Posts: 6921
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want help you are more likely to get it if your code is clean and neat (even if it may have issues). If you are editing with an IDE (e.g. Eclipse), pretty much every IDE has an automatic formatter built in. If not, my signature below has a lilnk to an Indenter that does an ok job. Here's what your code should have looked like:
 
Carey Brown
Saloon Keeper
Posts: 6921
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are allowing the user to select FILES or DIRECTORIES with the chooser. Why directories?

You open the chooser results with a Scanner. An image file is not a text file it is a binary file so a Scanner is the wrong tool for the job.

You don't have a JPanel to display an image in.
 
Ching Ming
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You are allowing the user to select FILES or DIRECTORIES with the chooser. Why directories?

You open the chooser results with a Scanner. An image file is not a text file it is a binary file so a Scanner is the wrong tool for the job.

You don't have a JPanel to display an image in.




I guess I first need a jpanel to display the image in.
 
Marshal
Posts: 25143
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ching Ming wrote:I guess I first need a jpanel to display the image in.



Yes, of course. You should create it at the same time as you create the other components -- otherwise the user of the GUI is going to be confused. "Okay, I click that button to select and display an image file, but I don't see where it's going to be displayed."

And after your JFileChooser has chosen a file to display, you won't write any code to read from that file. Here's an example from CodeRanch's Wiki which shows how to display an image as the background of a JPanel:

Background Image On JPanel

 
Ching Ming
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Ching Ming wrote:I guess I first need a jpanel to display the image in.



Yes, of course. You should create it at the same time as you create the other components -- otherwise the user of the GUI is going to be confused. "Okay, I click that button to select and display an image file, but I don't see where it's going to be displayed."

And after your JFileChooser has chosen a file to display, you won't write any code to read from that file. Here's an example from CodeRanch's Wiki which shows how to display an image as the background of a JPanel:

Background Image On JPanel



Can You show me how to declare a blank label which can hold the image. I dont want a background image covering the entire panel. The panel can hold 2 labels. One I want to be a blank label which can hold a image.
 
Paul Clapham
Marshal
Posts: 25143
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ching Ming wrote:Can You show me how to declare a blank label which can hold the image. I dont want a background image covering the entire panel. The panel can hold 2 labels. One I want to be a blank label which can hold a image.





Have you considered looking at a Swing tutorial? Here's the Oracle tutorial which has been around for years: Trail: Creating a GUI With JFC/Swing.
 
Bartender
Posts: 3747
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is advisable to make the JFileChooser object an instance field. That way it remembers the last directory visited, and will open in that directory next time. That can save a lot of mouseclicks.

Since you're interested in images, you can make the JFileChooser to only show image files. For this you can set a FileFilter to your jfc. In the API of the class FileNameExtensionFilter you can see an example of how to use it. There is one disadvantage, though, and that is that the standard FileFilter, present in a jfc, shows all filestypes, so the user can select that FileFilter and next select some non-image.

So I use something like this in my code:

In Careys code a Scanner is used to read all lines, but that doesn't work for image files (for as far as I know).
 
Carey Brown
Saloon Keeper
Posts: 6921
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:In Careys code a Scanner is used to read all lines, but that doesn't work for image files (for as far as I know).


That is Ching's code. I just ran it through a formatter. And, no, Scanner won't work.
 
Ching Ming
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:It is advisable to make the JFileChooser object an instance field. That way it remembers the last directory visited, and will open in that directory next time. That can save a lot of mouseclicks.

Since you're interested in images, you can make the JFileChooser to only show image files. For this you can set a FileFilter to your jfc. In the API of the class FileNameExtensionFilter you can see an example of how to use it. There is one disadvantage, though, and that is that the standard FileFilter, present in a jfc, shows all filestypes, so the user can select that FileFilter and next select some non-image.

I tried adding your 4 lines of code. I did not know where to add your 4 lines of code. This is what I tried and the code is not compiling.




 
Paul Clapham
Marshal
Posts: 25143
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed the quote and code tags so that the post was easier to understand.

So you the code doesn't compile. If that's the case then you ought to have some error messages describing the error. It looks like I have to remind you that only you can see those error messages.
 
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I fixed the quote and code tags . . . .

Unfortunately that makes the incorrect indentation easier to see. Remember that incorrectly indented code can hide compiler errors from everybody, including oneself. Please clean up the indentation. If your IDE won't indent code automatically, use a decent text editor (my favourite for Windows® is called NotePad++), and set up the following options:-
  • 1: Automatic indentation
  • 2: Automatic conversion of tab→four spaces
  • 3: Syntax highlighting, assuming you can read coloured text
  • 4: Bracket matching
  • 5: Right margin marker somewhere between 80 and 120. Don't use more than 80 for anything you post on the Ranch because some people will read your code on narrow screens.
  • All those features help you format your code correctly and enable you to find the errors. IDEs and a few editors (e.g. Kate) help by pairing brackets automatically, but otherwise you will have to do what I call writing backwards←link.
     
    Piet Souris
    Bartender
    Posts: 3747
    154
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @ Ching
    there is some misunderstanding, showing in your code.

    First, you declare and define the JFileChooser within the constructor, so it is only available in the constructor, instead of making this field an instance member.
    Second, it is meant to use this instance field in your actionPerformed method, instead of creating a new JFileChooser every time this method is invoked.

    So, your code should look like this:
     
    Saloon Keeper
    Posts: 11441
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Piet Souris wrote:So, your code should look like this:


    Not quite. Don't let forms extend windows or implement listeners. The JFrame should be a member and the listener should be anonymous.
     
    Piet Souris
    Bartender
    Posts: 3747
    154
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was talking about JFileChoosers.

    Besides: when I was learning Swing, I used to start my JFrames from main(), without a problem, and many a class extended some JComponent. It all worked, and I was happy about it. Later, with increasing knowledge and experience, I started wondering why my classes had to implement and extend things, and my coding changed over time. At this site, it sometimes looks like we do not allow a beginner to learn from experience.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11441
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Piet Souris wrote:At this site, it sometimes looks like we do not allow a beginner to learn from experience.


    Most beginners never evolve past "it all worked, and they are happy about it". A lot of experience comes from being told what the right way is, and then asking why it's the right way.
     
    Paul Clapham
    Marshal
    Posts: 25143
    64
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Piet Souris wrote:Later, with increasing knowledge and experience, I started wondering why my classes had to implement and extend things, and my coding changed over time.



    My style evolved in that way too. Although I have to say, it was mostly from this forum that I learned that my class which extended JLabel so that I could have a red JLabel, well, there was a better way to do that. Even so my mind rebelled against that idea for a while, but finally it sank in.
     
    Campbell Ritchie
    Marshal
    Posts: 67939
    258
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Many of us have learnt lots on this forum. Thank you to those who have helped us.
     
    Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!