image - Java: If statement for the 'value' of an ImageIcon -


i have small program textfield, label (for image) , button. when press button, shows standard image.

but, want change picture if input of textfield equals string, "car" example.

when use this, error defined 'foto' variable:

if ("car".equals(input)) {     imageicon foto =  imageicon("c:......png"); 

this important part of code:

private void buttonactionperformed(java.awt.event.actionevent evt) {                                            string input = textfield.gettext();     imageicon foto = new imageicon("c:.......png");     label.seticon(foto); 

and full code this:

main class:

package test4;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield;   public class test4 extends jframe {             public static void main(string[] args) {         test5 frame = new test5();         frame.setvisible(true);     }  } 

"swing" class:

/*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */ package test4;  import javax.swing.imageicon;  /**  *  * @author  */ public class test5 extends javax.swing.jframe {      /**      * creates new form test5      */     public test5() {         initcomponents();     }      /**      * method called within constructor initialize form.      * warning: not modify code. content of method      * regenerated form editor.      */     @suppresswarnings("unchecked")     // <editor-fold defaultstate="collapsed" desc="generated code">                               private void initcomponents() {          textfield = new javax.swing.jtextfield();         label = new javax.swing.jlabel();         button = new javax.swing.jbutton();          setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);          button.settext("show");         button.addactionlistener(new java.awt.event.actionlistener() {             public void actionperformed(java.awt.event.actionevent evt) {                 buttonactionperformed(evt);             }         });          javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane());         getcontentpane().setlayout(layout);         layout.sethorizontalgroup(             layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)             .addgroup(layout.createsequentialgroup()                 .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                     .addgroup(layout.createsequentialgroup()                         .addgap(121, 121, 121)                         .addcomponent(textfield, javax.swing.grouplayout.preferred_size, 138, javax.swing.grouplayout.preferred_size))                     .addgroup(layout.createsequentialgroup()                         .addgap(100, 100, 100)                         .addcomponent(label, javax.swing.grouplayout.preferred_size, 176, javax.swing.grouplayout.preferred_size))                     .addgroup(layout.createsequentialgroup()                         .addgap(147, 147, 147)                         .addcomponent(button)))                 .addcontainergap(124, short.max_value))         );         layout.setverticalgroup(             layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)             .addgroup(layout.createsequentialgroup()                 .addgap(25, 25, 25)                 .addcomponent(textfield, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)                 .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)                 .addcomponent(label, javax.swing.grouplayout.preferred_size, 134, javax.swing.grouplayout.preferred_size)                 .addgap(29, 29, 29)                 .addcomponent(button)                 .addcontainergap(63, short.max_value))         );          pack();     }// </editor-fold>                              private void buttonactionperformed(java.awt.event.actionevent evt) {                                                string input = textfield.gettext();         imageicon foto = new imageicon("c:\\users\\jordii\\pictures\\music.png");         label.seticon(foto);      }                                            /**      * @param args command line arguments      */     public static void main(string args[]) {         /* set nimbus , feel */         //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) ">         /* if nimbus (introduced in java se 6) not available, stay default , feel.          * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html           */         try {             (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) {                 if ("nimbus".equals(info.getname())) {                     javax.swing.uimanager.setlookandfeel(info.getclassname());                     break;                 }             }         } catch (classnotfoundexception ex) {             java.util.logging.logger.getlogger(test5.class.getname()).log(java.util.logging.level.severe, null, ex);         } catch (instantiationexception ex) {             java.util.logging.logger.getlogger(test5.class.getname()).log(java.util.logging.level.severe, null, ex);         } catch (illegalaccessexception ex) {             java.util.logging.logger.getlogger(test5.class.getname()).log(java.util.logging.level.severe, null, ex);         } catch (javax.swing.unsupportedlookandfeelexception ex) {             java.util.logging.logger.getlogger(test5.class.getname()).log(java.util.logging.level.severe, null, ex);         }         //</editor-fold>          /* create , display form */         java.awt.eventqueue.invokelater(new runnable() {             public void run() {                 new test5().setvisible(true);             }         });     }      // variables declaration - not modify                          private javax.swing.jbutton button;     private javax.swing.jlabel label;     private javax.swing.jtextfield textfield;     // end of variables declaration                    } 

so question is: there way display picture depending on input of textfield?

for example: if input 'car', display picture of car. if input 'house', display picture of house. else, display picture of bike.

look, code not pretty. maneuver in code need get does.

all aside, answer question:

if error saying ... defined, ... defined. it's simple. example show how works:

package test.project.main;  public class main {      public static void main(string[] args) {         int = 10;         string = "10";     } } 

if try run above code, compiler warn , defined variable name a. if still proceed run code, runtime exception thrown:

exception in thread "main" java.lang.error: unresolved compilation problem: duplicate local variable at test.project.main.main.main(main.java:7)

now foto example.

private void buttonactionperformed(java.awt.event.actionevent evt) {                                            string input = textfield.gettext();      imageicon foto = new imageicon("c:.....png");     if ("car".equals(input))         imageicon foto = new imageicon("c:......png");      label.seticon(foto); } 

here can see foto defined, compiler warn this. if run code , if if statement true (because input equal 'car') runtime exception, mind you: if have compiler error, unresolved compilation error message @ runtime.

you can fix avoiding duplicate names in same method e.a. foto , foto1 etc. better approach make 1 variable foto, , set @ runtime. example below explain how works:

private void buttonactionperformed(java.awt.event.actionevent evt) {                                            string input = textfield.gettext();      imageicon foto;     if ("car".equals(input))         foto = new imageicon("c:......png");     else if ("plane".equals(input))         foto = new imageicon("c:......png");     else if ("bike".equals(input))         foto = new imageicon("c:......png");     else         foto = new imageicon("c:......png");      label.seticon(foto); } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -