Solved

adding more than one icon to a label in a panel

Posted on 2006-07-12
17
221 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
ID: 17090403
You can add another label with icon to the panel
0
 

Author Comment

by:nhay59
ID: 17090449
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
ID: 17090527
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 86

Expert Comment

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

Author Comment

by:nhay59
ID: 17090588
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
ID: 17090592
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
ID: 17090605
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
ID: 17090653
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
 

Author Comment

by:nhay59
ID: 17091344
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
ID: 17091659
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
ID: 17091777
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
ID: 17091810
> 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
ID: 17091883
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
ID: 17092228
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
ID: 17092626
?
0
 

Author Comment

by:nhay59
ID: 17092888
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
ID: 17097051
Thank you for accepting, glad I was of help :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 55
servlet example issue 6 39
servlet filter example 37 63
iterator/ListIterator approach 17 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

778 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