Solved

adding more than one icon to a label in a panel

Posted on 2006-07-12
17
219 Views
Last Modified: 2010-03-31
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
Comment
Question by:nhay59
  • 8
  • 5
  • 4
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can add another label with icon to the panel
0
 

Author Comment

by:nhay59
Comment Utility
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
 

Author Comment

by:nhay59
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If you use a FlowLayout, the labels will be automatically added one after the other
0
 

Author Comment

by:nhay59
Comment Utility
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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Add a label to the panel dynamically, i.e. first run the query, then get the icon and then add the label.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:nhay59
Comment Utility
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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
 

Author Comment

by:nhay59
Comment Utility
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
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
Comment Utility
> 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
 

Author Comment

by:nhay59
Comment Utility
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
 

Author Comment

by:nhay59
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
?
0
 

Author Comment

by:nhay59
Comment Utility
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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Thank you for accepting, glad I was of help :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now