?
Solved

"Lists"

Posted on 1998-09-08
11
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

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.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.
Suggested Courses

649 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