Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Click on JButton to show Image  RSS feed

 
Jay Okocha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! First off Thanks in advance for the help. I've recently decided to learn Java on my own using internet material such as forums, blogs and you tube. Ive never programed before but through some research I concluded that Java is going to be on the mid spectrum of difficulty as far as verbiage goes. I ma in no way implying that Java is easy but instead closer to spoken English that c++ if my research is correct. So here is my attempt at a basic program where I click a button and it will make an image appear. Not sure where Im falling short please advice. Thanks again!!


 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Okocha wrote: . . . Java is easy but instead closer to spoken English that c++ if my research is correct. . . .
I would be very interested to see where that comes from. I had never thought either language was close to spoken English. Java is easy. Really easy. That is the good news. Unfortunately the bad news is, programming is difficult. Really difficult. Java C++ Python C# Eiffel Smalltalk Simula67 etc etc are simply ways to implement object‑oriented programming.

I know precious little about images, but you know there is a tutorial available? That has sections about images in. Search it. This section might be what you want.

I think this is a GUI‑related topic, so shall moe it to our GUIs forum.

And welcome to the Ranch

 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . Java C++ Python C# Eiffel Smalltalk Simula67 etc etc are simply ways to implement object‑oriented programming. . . .
Some people would say I am bit overoptimistic about C++

You have some good stuff there, but there are conventions. Write all your class names with CapitalLettersLikeThis.
When you click the button, you are creating a new image object. But how are you adding it to the label? Has the JLabel(←link) class got setIcon methods?
 
Knute Snortum
Sheriff
Posts: 3550
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search for "java swing display image" you will probably find a better way to load an image.

But first, let's just try to get an image to display, regardless of the button. Here's the idiom:



Can you rewrite the program to just display?

Next, I would suggest that you use the JLabel.setVisible() method. Can you write the program so that you load the picture but don't display it?

Now you should be able to set the visibility of the JLabel in the actionListener method. You may have to call repaint().

That should get your program working, but see my next post.
 
Knute Snortum
Sheriff
Posts: 3550
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have three class variables in your program:



In general, you want to have the smallest scope of a variable that is possible, so you don't need ImageIcon and JLabel to be class members. Can you rewrite the program using local variables for the two mentioned variables?

As the program is written, you need label to be a a class variable because you need it in the actionListener method. But there's a way that you can make the label a local variable. It takes this idiom for creating an object that is passed an object of the type ActionListener():



What is happening is you are creating an anonymous object of type ActionListener. This interface requires an actionPerformed() method, so you create it write there. Try using this code to create your ActionListener.

Now why would we do this? Isn't it confusing? Yes, but you get something really nice when you do this. You get the enclosing method's environment. That means that label can now be a local variable. Try it!

You should now have a GUI program that looks a lot like other GUI programs in Java.

Note: in Java 8, you can use lambdas instead of anonymous objects, but I'll let someone else talk about that.
 
Jay Okocha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Jay Okocha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been pre occupied with other stuff and I thought I had this figured out; actually closer than original post. Also as always thanks in advance for the help. When I rewrote the code I was able to get my image to show once the click happens; weird thing is that the the window that pops up has to be resized for the image to show Very strange. Please in advance to Campbell and Knute my apologies if my code still appears sloppy. Ideas?
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it your intention to add an additional JLabel each time the button is clicked?
Or do you simply want to replace the existing image on the label?

In the first case: add 'label.revalidate()' to your actionListener and see if
you like the behaviour.

In the second case: in line 32, just do a 'label.setIcon(...)', and it would
be handy to load that second image in your initializer.

Greetz,
Piet
 
Jay Okocha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet,

Thanks for the reply. To answer your question, I want to be able to click the button an an image will appear where the label/image are. As stated above I know my code is sloppy but Im VERY green and Im trying to make sense of things by making these small programs. I will modify my code to make it work. Thanks again !
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jay,

okay. If you need any further help, just let us know.

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!