• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

small doubt in Swing appln using JPanel

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm creating a small swing appln. in that i need to display my photo(jpg file) in the frame's WEST region. for that i created MyPanel class(inner class) to display it in the frame. i need the code snippet inside the paintComponent method to get my picture from a particular path.

Regards

Jhanavi.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you set up a layout for your Frame and then add the jpg to the west of that layout?
 
Jhanavi Shivgami
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i already planned to use Panel for my WEST region(as i mentioned), i need the actual coding part to be used inside the paintComponent method to display the jpeg.

Originally posted by Vijayendra V Rao:
Why don't you set up a layout for your Frame and then add the jpg to the west of that layout?

 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should be abble to create an JLabel with an ImageIcon out of a JPG file, then just add this label wherever you want to. no need to write your custom paintComponentn method.

pascal
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks pascal betz.

Originally posted by pascal betz:
you should be abble to create an JLabel with an ImageIcon out of a JPG file, then just add this label wherever you want to. no need to write your custom paintComponentn method.

pascal

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing / AWT / SWT / JFace forum..
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About loading an image from inside the paintComponent method: this is not recommended. Java calls this method often according to its own needs. To demonstrate this you can include a System.out.println statement inside the method and see what happens as you disturb the parent JFrames environment (minimize, restore, cover/uncover with other apps).

You need to load the image only once. The place to load it is in the class constructor or a method called from the constructor. Save a reference to it in class scope (member variable) and it's ready whenever you want to use it.

Here's a demo that shows how to display an image in two ways:

[ September 02, 2004: Message edited by: Craig Wood ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic