Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

adding more than one icon to a label in a panel

Posted on 2006-07-12
17
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

604 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