?
Solved

"Lists"

Posted on 1998-09-08
11
Medium Priority
?
232 Views
Last Modified: 2010-03-30

  I have created a list without multiple select along with a
  textfield that displays the name of the item selected.An
  image corresponding to the name in the textfield has to be
  displayed on the applet.This is not happening.This is part
  of my code...
   
  /* Inside ActionPerformed*/
  t1.setText((List)e.getSource()).getSelectedItem());
 
  public void paint( Graphics g)
{
  Image im1,im2;
  s1 = t1.getText().trim(); /* s1 is a string */
   
   if(s1 == "mickey")
  {
    im1=getImage(getCodeBase(),"mickey.gif") /* mickey.gif is in the same directory */

   g.drawImage(im1,50,50 this); /* this image is not showing
   on the applet */
   g.drawString(msg,60,60); /* msg is declared as a string
   and is not getting displayed on the applet */
}
0
Comment
Question by:kans
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 4

Expert Comment

by:evijay
ID: 1223824
can you elaborate what exactly is happening when you select the item in the list.

can you give more source code

0
 
LVL 8

Expert Comment

by:diakov
ID: 1223825
After the selection event occurs, you have to catch it and call update() or repaint() of the applet. The source of the paint stays the same.

0
 
LVL 8

Expert Comment

by:diakov
ID: 1223826
tell me if it works.
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.

 

Author Comment

by:kans
ID: 1223827

  Yeah, Vijay... I have created a list with 2 items(mickey and
  donald) along with a textfield that displays mickey when mickey is selected and donald when donald is selected.This part of the program works. But, the image of mickey or donald is not getting displayed.I've given that part of the source code  which does not work in my question.

 

 
0
 
LVL 4

Expert Comment

by:martinag
ID: 1223828
Instead of
if(s1 == "mickey")
use
if(s1.equals("mickey"))

Martin
0
 
LVL 2

Expert Comment

by:shchuka
ID: 1223829
martinag is correct.  You cannot use == sign to compare strings - this way Java compares the pointers to the strings.  If you want to compare the contents of the strings, you have to use methods equals() or equalsIgnoreCase(), e.t.

if(s1.equals("mickey"))
or
if(s1.equalsIgnoreCase("mickey"))
0
 
LVL 2

Accepted Solution

by:
aziz061097 earned 280 total points
ID: 1223830
Just to make clear == also works on String . The following program will do what you want.

//ImageUpdate.java
public class ImageUpdate extends Applet
{
      TextField tx = new TextField();
      List lst = new List();
      Image img;

public void init()
{
      lst.addItem("Mickey");
      lst.addItem("Donald");
      add(lst);
      tx.setText("Mickey");
      add(tx);
      lst.addItemListener(this.new lstItemListener());
      img = getImage(getDocumentBase(),"Mickey.jpg");
}
public void paint(Graphics g)
{
      g.drawImage(img,0, 80, 615, 430, this);
}

class lstItemListener implements ItemListener{
      public void itemStateChanged(ItemEvent e){
      if (lst.getSelectedItem()=="Mickey")
      {
      System.out.println("List Selected : Mickey");
      img = getImage(getDocumentBase(),"Mickey.jpg");
      tx.setText("Mickey");
      }
      else
      {
      System.out.println("List Selected : Donald");
      img = getImage(getDocumentBase(),"Donald.jpg");
      tx.setText("Donald");
      }
      repaint();
    }
}
}
 
0
 
LVL 4

Expert Comment

by:martinag
ID: 1223831
As a newbie I once spent many hours trying to find out what was wrong with my small applet and then it turned out that I had used == instead of equals().

I am not saying that aziz is completely wrong. It's possible that his version of JDK treats the == different than mine.

Martin
0
 

Author Comment

by:kans
ID: 1223832
 Hi aziz!
   yeah, i agree with your answer...his solution works but there's one more problem....the system.println does n't seem to print a comment.Otherwise,its
  fine.I have done the same program using actionlistener instead of itemlistener
  ..here again the program works but the comment added using println does n't
  seem to work.Can you tell me why?
0
 
LVL 2

Expert Comment

by:aziz061097
ID: 1223833
If you use actionListener and on actionPerformed you want to do something you will have to note that it will be triggered only on a double click.

And if you think that your program is correct and still the println statement does not print , I observed such a thing happening when I used the Adapter and misspeled the method name , that method was never triggered at all, check the spelling if it is correct.
0
 

Author Comment

by:kans
ID: 1223834
Hi Aziz!
  I'll tell you what i've done...sorry i've not used println ..
  here's that part of my program
     
     /* Inside actionperformed */
     public void actionPerformed(ActionEvent e )
{
    txt.setText((List)e.getSource().getSelecterItem());
}
   
      String  s1  = txt.getText().trim();  
     
      public void paint(Graphics g )
  {
      if(s1.equals() " Mickey")
      {
      img1 = getImage(getCodeBase(), "Mickey.gif");
       System.out.println(" Ppl look at mickey mouse!");
      g.drawImage(img1,20,30,600,450,this);
      }
   

      if(s1.equals() "donald")
      {
        img2 = getImage(getCodeBase(),"donald");
        System.out.println(" LOOK AT DONALD DUCK");
        g.drawImage(img2,20,50,500,600,this);
      }

      repaint();


 }
  /* this program is showing the list and on selecting mickey
     or donald the textfield txt has either mickey or donald
     and the image is showing up but the problem with the image
     is that it flickers and the pritnln statement does n't work
    ...can you tell me why */
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 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