Solved

"Lists"

Posted on 1998-09-08
11
184 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
Comment Utility
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
Comment Utility
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
Comment Utility
tell me if it works.
0
 

Author Comment

by:kans
Comment Utility

  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
Comment Utility
Instead of
if(s1 == "mickey")
use
if(s1.equals("mickey"))

Martin
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:shchuka
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

10 Experts available now in Live!

Get 1:1 Help Now