Solved

"Lists"

Posted on 1998-09-08
11
203 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 19
Checkbox and ListView in Android Layout 4 65
javap bin 2 34
Android development question 2 36
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

776 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