• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display a specific text for each random image

 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, This class produces three random images.
I need to set a string for each random image to the right of it to display with the picture but keep getting stuck.
This is my first day working with the swing.





 
Knute Snortum
Sheriff
Posts: 3257
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see if the Swing forum gets you better results.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect idea!
 
Campbell Ritchie
Marshal
Posts: 52558
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a button? Why re you using a 1×1 grid layout?

You should get the picture file and the text from a non‑GUI program. You should not be loading images from inside a panel; you should have another class which finds the photo and the text. Create a class which will find the photo and display he text, then give it some methods to add them to the panel. Why not put the text on a label? I would use a different layout.That way you need not extend any display classes.
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Select a layout that will allow you to have a 2x3 grid for your objects.
In the left side, put a JPanel and populate it with the image you want.
In the right side put a JLabel and put the text in it you need.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to put a text on the button with b2.setText("whatever"); after I add the button the problem is that I wanted to put the right text with the right image
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross,

If I understand the problem you are illustrating, then you have a problem knowing what text to associate with what image as you display them, so you are looking for a way to reliably associate the text label with the image so that when you generate a random number you get the right image and text together. Is that right?

Java has several ways of approaching this, but I think I would do it in this manner:

I would make a class to hold the image and the text so that I would have one place to look, basically pre-associate them together,

then add them to a Java structure that would allow me to access randomly--something like an array or ArrayList.


Les

Note: none of the previous code has been checked.

Ross Gerard wrote:I know how to put a text on the button with b2.setText("whatever"); after I add the button the problem is that I wanted to put the right text with the right image
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!