0

hello to all...,

i am working on a project and there i am facing a problem of adding pictures to a label.In my project its like that.. i have a textfield ,a button and a label. When i write the file name in the textfield and then after clicking on the button, the image with the name specified if exists should load on the label. otherwise display a message that file do not exist. I am not getting how to make that particular logic. Can any one please help me out....any sort of help will be highly appritiated...
thankx..!!

3
Contributors
3
Replies
23
Views
2 Years
Discussion Span
Last Post by preetchannay
1

I would suggest you on the entering image file name. Instead of let user enter a file name, you should rather have a "browse" button or similar to open up a file selector. This way, user would be able to select a file from anywhere and the file should exist (but may not be the type you expect which is a different story).

And what do you mean by 'label'? How do you display the 'label' you are talking about?

1

This is how you can locate the image in your class folder
private Icon icon = new ImageIcon( getClass().getResource( "ImageName.png" ));

Then you can use the image to create a new Jlabel of it such as
private JLabel jl = new JLabel(icon);

Afterwards you can use the Jlabel to attach it to a Panel or anything you want to do with it

Edited by Slavi

0

warm regards for replying and putting your efforts in my problem.It will be cool if i just write my code lil bit what i was trying..like..-->

Taywin you didn't get my prob..,i am trying to show a picture on a jLabel in my jPanel (netbeans) on the basis of the name of the particular picture.
As we get the name of a picture from any of the resources whether static or userdefined ,putting that name into a condition check and then after displaying it on the jLabel Swing Control Component. Thanks buddie..,but i found another way to taste the HONEY without bees.. thanx ...!!

Slavi i also tried the code you put up in your reply..,but there was no big difference. As we have to pass the value for the icon in your code and i directly put it as using setIcon method, both gave me the same result. then i tried the switch case the basic one hahaha i wish to tell you it really works..,wooohuuu thanks a lot for all my friends for giving me useful advices THANX guys C U soon :)

String rs=jTextField1.getText();
   switch(rs){
            case "sukhi": jLabel1.setIcon(new ImageIcon("C:/Users/M@NEY/Documents/NetBeansProjects/appextra/src/sukhi.jpg"));
            break;
            case "smiley": jLabel1.setIcon(new ImageIcon("C:/Users/M@NEY/Documents/NetBeansProjects/appextra/src/smiley.jpg"));
            break;
            default :
                jLabel1.setIcon(new ImageIcon(""));
                jLabel1.setText("Sorry");
            break;     
        }

Edited by preetchannay

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.