• Post Reply Bookmark Topic Watch Topic
  • New Topic

small doubt in Swing appln using JPanel  RSS feed

 
Jhanavi Shivgami
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • 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
 
Kather Basha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / AWT / SWT / JFace forum..
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!