Solved

"Lists"

Posted on 1998-09-08
11
194 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 83
firstChar challenge 13 106
wordappend challenge 8 142
Requested array size exceeds VM limit 3 82
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

939 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now