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..!!

Recommended Answers

All 3 Replies

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?

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

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;     
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.