Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Showing a picture  RSS feed

 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys/girls,

I'm trying to make a simple program that shows a picture in my GUI when i click a button. So far all it does is restart the window.


 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code doesn't give enough information to provide help.
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the purpose of wasserkopf class? You instantiate it at line 70.
You should follow Java Convention for naming your classes.
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its just a program to mess with a friend. the wasserkopf class is to put out name and weight. it shows up in 2 text fields on my GUI. you can ignore that.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, make sure you invoke revalidate() each time you add or remove component from your frame.
The problem you are having could probably be easily resolved by putting an empty label somewhere on your frame, and when user clicks button, just set image to already existing label. For checking, first add just some text to your label.
Also, I would avoid setting layout to null.
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
layout being null is auto generated from my plugin.

i have an empty label on my frame and it doesnt do anything

What does revalidate() do?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try getting image you want to display by using URL class, like this:

Make sure your image is in the same package as your class, otherwise you'll get that "error loading image" message.

Also remove those: pack() and setVisible(true) (your frame is already visible).

You can check the API for revalidate().
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i set that to a JLabel?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how your code is supposed to look now (posting just the relevant part):

Add the method I wrote you in the previous post to the same class, and it should work. When you make sure it works, set some better layout for your components.

Also, I would change this:

to:
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
soooo


Still doesn't work =/
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is "images.jpg" name of the image you want to display, and is it in the same directory as your class?
Are you getting that error message on output after clicking the button?

edit: Also, you omitted that revalidate() invocation.
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea sorry forgot to mention i changed the picture im useing. yea its in the same folder and i was getting an error untill i put it into that folder.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, do you still get error message, or it's working now?
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no im not getting the error message but the picture still wont show
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you invoke revalidate(); after getContentPane().add(label);?
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried revalidate() and repaint() both didnt work
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the line where you set layout to null.
 
chris rous
Greenhorn
Posts: 21
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iw worked! awesome thanks =D
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!