1

I am trying to write a application using LWUIT where I want a image to be displayed on click of a button. I have the following code. But I get an exception if the button is clicked twice. Please help me display the image without giving any exception.

        final Form f = new Form("Static TAF");

        Button TrackMe = new Button("TrackMe");

        Image TrackMeicon = null;
        TrackMeicon = Image.createImage("/hello/follow.jpeg");
        final Label TrackMeLabel = new Label(TrackMeicon);    

        TrackMe.addActionListener(new ActionListener()
        {

        public void actionPerformed(ActionEvent ae) 
        {
                 System.out.println("Removing the previous Images");
                 f.addComponent(TrackMeLabel); 
        }
        });

Please Help

Kalai Selvan Ravi
  • 2,846
  • 2
  • 16
  • 28
swatijoshi
  • 51
  • 2

3 Answers3

1

When you are clicking the button for the first time, image is added to the form. When you are clicking for the second time, that image already exists in the form. So, it will throw "Component already exists" exception.

Your action listener should be

TrackMe.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae) {
              System.out.println("Removing the previous Images");
              f.removeComponent(TrackMeLabel); 
              f.addComponent(TrackMeLabel); 
      }
});
Kalai Selvan Ravi
  • 2,846
  • 2
  • 16
  • 28
  • Sorry to tell but this code is not working f.removeComponent(TrackMeLabel); this line permanently removes the component and there by never adds it in the next line of code. Apart from displaying the image with label... is'nt there any other logic? – swatijoshi Aug 28 '12 at 07:15
  • In my experience it is better to add a component to LWUIT Form only once, or use removeAll() and then re add them. – Ajibola Aug 30 '12 at 16:02
  • Thanku Ajibola and Kalai for your valuable answers. Kindly look into other questions i posted under lwuit-commands tag. I need help regarding that. – swatijoshi Sep 06 '12 at 05:19
0
TrackMe.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae) {
          System.out.println("Removing the previous Images");
          final Label TrackMeLabel = new Label(TrackMeicon); 
          f.removeAll();
          f.addComponent(TrackMeLabel); 
  }

});

Ajibola
  • 1,218
  • 16
  • 28
  • f.removeAll() will work only if TrackMeLabel is the only component in the form. If already some components in the form f means that also will be removed. – Kalai Selvan Ravi Sep 06 '12 at 12:03
0

If you want yo add only one image you can use this:

....

TrackMe.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent ae) {
          if(!f.containes(TrackMeLabel))
             f.addComponent(TrackMeLabel); 
  }

if you want some imges you need something like that:

....

 TrackMe.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae) {
               Image TrackMeicon = null;
               TrackMeicon = Image.createImage("/hello/follow.jpeg");
               Label TrackMeLabel = new Label(TrackMeicon);   
               f.addComponent(TrackMeLabel); 
      }
neb1
  • 209
  • 1
  • 12