• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java JoptionPane

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Hey everyone.

I'm doing a project for my college and all I want is to display 3 cards as buttons and when you click you will get another window with 1 image.
I manage to do the buttons with the image and also when clicked will pop another image. Long way but I did it.(bare in mind I'm a beginner )
but i want to have a different image for the 3 cards. And i'm using the JOPTION. any idea of how to change it?

also i want to use the random but i will do it after as i need to understand this bit first.

thanks in advance,

M.

/////


 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget about the random thing for a sec, how are you doing it right now? In another words, given the 3 buttons how do you determine what icon for which button?

In the action listener, you only have the ActionEvent e, from it you should able to get which button is clicked right? (hint e.getSource())

Oh is your action listener a separate class or part of the GUI class (eg public class MyGUI extends JFrame implements ActionListener)?
 
M Dias
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Forget about the random thing for a sec, how are you doing it right now? In another words, given the 3 buttons how do you determine what icon for which button?

In the action listener, you only have the ActionEvent e, from it you should able to get which button is clicked right? (hint e.getSource())

Oh is your action listener a separate class or part of the GUI class (eg public class MyGUI extends JFrame implements ActionListener)?



Hey, thanks for the fast reply

yes, i do have all of that and running just fine with different images and all.
but in the code i posted i can only specify one. so it will show the same picture for the 3 buttons when clicked.

Does this make any sense?

Let me show you the code.



I created 3 buttons with a background image (all working fine), then when you click another window pops with another image which i invoked with this bit



and with



I call what i want to show. specially the last bit "icon02", but it cant be repeated so it shows the same to all 3 buttons
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok assuming the image file names are 01.jpg, 02.jpg and 03.jpg which I doubt.

Then you can use Random to generate a number 1,2 or 3 then map it to the corresponding image.

Another probably more flexible way is to put all those images in an array or ArrayList (recommended). Then use Random to generate the a number that represents the array/list index (remember 0 based)

I think you get the idea. Try it out and post back if you need help.
 
M Dias
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Ah ok assuming the image file names are 01.jpg, 02.jpg and 03.jpg which I doubt.

Then you can use Random to generate a number 1,2 or 3 then map it to the corresponding image.

Another probably more flexible way is to put all those images in an array or ArrayList (recommended). Then use Random to generate the a number that represents the array/list index (remember 0 based)

I think you get the idea. Try it out and post back if you need help.



yeah thats probably the right thing to do.
let me show you what i was trying to do.





any ideas how to improve it. again i am an absolute beginner at java programming
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good. Just need that random part lol

There are several ways to do the random number generator (1 liner to whole function/class) here
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic