Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

adding more than one icon to a label in a panel

Hi,

I've put together an application, swing and sql, which uses a MySQL database to store images as BLOBS and a swing gui to load and view them. Currently, the blobs in the sql database are loaded and added to an icon, which is then added to a label using,

signLabel.setIcon(iconThumbnail);

The user queries the database using a textfield and a button, which then returns the required image as a label within a panel. All of this works fine, but each time a user enters a query into the textfield, eg: House, the button returns only the icon for that label. It effectively removes the previous label. Only one label can be shown on the panel at any time.

Therefore, what would be the best way to preserve previous labels on the panel, and add more than one label/query to the panel. Basically, I want to be able to show more than one image from the database within the panel, instead of currently only the most recent search query.

Would some sort of array or vector etc work with this problem?

Any ideas or advice would be really appreciated.

Thanks
0
nhay59
Asked:
nhay59
  • 8
  • 5
  • 4
1 Solution
 
CEHJCommented:
You can add another label with icon to the panel
0
 
nhay59Author Commented:
Hi,

Thanks for the reply. How would I get it to assign the second query to the second label etc?

Thanks for the help
0
 
nhay59Author Commented:
HI,

I've jsut thought. If I use extra labels for the panel, effectively how could I tell it to add the first query to label1, the second query to label2 etc?

Is this even possible?

Thanks for the help
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
CEHJCommented:
If you use a FlowLayout, the labels will be automatically added one after the other
0
 
nhay59Author Commented:
Hi,

Adding the labels to the panel is not a problem. I'm using a FlowLayout with left positioning for the panel. The problem is how does the program know that the first query result, eg: image1, goes to label1, the second query result, image2, to label2 and so on.

I can't seem to see how to add separate query results, image1 etc, to separate labels on the panel.

Any help or advice appreciated.

Thanks

0
 
girionisCommented:
Add a label to the panel dynamically, i.e. first run the query, then get the icon and then add the label.
0
 
girionisCommented:
nhay59,
> I can't seem to see how to add separate query results, image1 etc, to
> separate labels on the panel.

how are you keeping tracks of the queries? The same should be done for the labels.
0
 
CEHJCommented:
If you mean you don't want to lose references to the labels, do


JLabel[] queryLabels = new JLabel[NUM_QUERIES];

and assign them appropriately
0
 
nhay59Author Commented:
Hi,

Thanks for the replies. The button uses the following actionPerformed method,

if (source == convertButton)
           
            try {
                sqlLoader.rs.beforeFirst();
                while (sqlLoader.rs.next()) {
                    if (sqlLoader.rs.getString("sign_name").equals(
                            translateText.getText()))
                        break;
                }
                if (!sqlLoader.rs.isAfterLast()) {
                   
                    Blob blob = sqlLoader.rs.getBlob("sign_full");
                   
                    int b;
                    InputStream bis = sqlLoader.rs.getBinaryStream("sign_full");
                   
                    bis.close();
                   
                    icon = new ImageIcon(blob.getBytes(1L, (int) blob
                            .length()));
                   
                    loadImage();
                    signLabel.setIcon(iconThumbnail);
                                       
                   
                }
            } catch (Exception selectException) {
                displaySQLErrors(selectException);
            }

where the query is obtained from the textfield 'translateText', and then the answer is stored as a ResultSet object 'rs'. Then the appropriate image is loaded as an icon from the blob stored in the database, and added to the label on the panel.

In effect, I'm trying to add more than one query to more than one label, and then add them as required to the panel.

Thanks for the help.
0
 
girionisCommented:
You could do

f (!sqlLoader.rs.isAfterLast()) {
                   
                    Blob blob = sqlLoader.rs.getBlob("sign_full");
                   
                    int b;
                    InputStream bis = sqlLoader.rs.getBinaryStream("sign_full");
                   
                    bis.close();
                   
                    icon = new ImageIcon(blob.getBytes(1L, (int) blob
                            .length()));
                   
                    loadImage();
                    Label label = new Label();   // You create a new label every time
                    label.setIcon(iconThumbnail);   // You set the icon to the new label
                    panel.add(label);             // You add the label to the panel
                   
                }

I assume you have a variable "panel" defined somewhere in your code which represents the panel you add the labels to.
0
 
nhay59Author Commented:
Hi,

That doesn't seem to work. Anyway, I think that would just give me the same solution as before.

I've been trying to use a vetor, ie: by adding each created iconThumbnail to a vector. I thought I could then just call each iconThumbnail at the appropriate positon in the vector index and then assign it to each label as required.
How would I get the iconThumbnail out of the vector and then add it to each label?

So far, I've created the vector,

Vector signs = new Vector();

and then added each created iconThumbnail to the vector,

signs.add(iconThumbnail);

What I need to be able to do is then remove each iconThumbnail from a set index position in the vector and add to the setIcon() method as before for each label.

Any ideas or help appreciated.

Thanks
0
 
girionisCommented:
> That doesn't seem to work.

Why not? I guess you don't see the labels? Try to revalidate the GUI as to reload any change in it.
0
 
nhay59Author Commented:
Hi,

Thanks for all the replies. I've managed to figure out how to use a vector with the labels and imageIcon object. I've used the following code to get each icon at the set index of the vector, and then assigned to each required label.

                    signs.add(iconThumbnail);
                   
                    signs.get(0);
                    ImageIcon thumbNail = (ImageIcon)signs.get(0);
                    signLabel.setIcon(thumbNail);
                   
                    signs.get(1);
                    ImageIcon thumbNail2 = (ImageIcon)signs.get(1);
                    signLabel2.setIcon(thumbNail2);

There's only a finite number of labels I can add to this panel, so this should be sufficient. If anyone sees a way to improve this let me know. But at least it works for now.

Thanks to all for their help and advice.
0
 
nhay59Author Commented:
girionis,

Thanks for the help. Yes, you were right. I can achieve the same result with the following code,

JLabel label = new JLabel();   // You create a new label every time
                    label.setIcon(iconThumbnail);   // You set the icon to the new label
                    panel2.add(label);  
                    panel2.revalidate();

The biggest, and better,difference is that with this solution you can add as many labels as you want to the panel. Exactly the result I wanted.

Thanks for the help and solution.
0
 
CEHJCommented:
?
0
 
nhay59Author Commented:
Hi CEHJ,

Thanks for the reply. I accepted the answer from 'girionis' because it provided me with a solution to the problem given. Granted, I now have other issues resulting from adding a scrollpane to the panel in question, but I'm not certain whether the above solution is a cause of the new problem, or it is merely a coincidence.

Thanks for your help though.
0
 
girionisCommented:
Thank you for accepting, glad I was of help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now