954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Question about ItemListener

Hello,

The following program has three JPanels - one to hold the image, one for the JComboBox and the last one for holding the JLabel, JTextArea and JButton. Once a selection is made in the JComboBox, the user will enter text in the textarea and press the button. The button has an actionlistener which records all the comments in a string array. Once the user selects the choice of "User Comments" from the JComboBox, all the user comments for each image is displayed in the textarea. The issue I am having is that the user comments show up twice instead of once. My program is below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Scroll extends JApplet {

            private JTextArea area1, area2;            
            private JComboBox cbox;
            private JButton button;
            private JPanel panelA, panelB, panelC;
            private ImageIcon [] image = new ImageIcon[6];
            private int j = 0, cell = 0;
            private JLabel label;
            private String[] a = new String [6];
            private String answer = "";
            private int count = 0;
            
            
            public void init()
            {
              
               String options[]  = {"Choose", "Option1", "Option2", "Option3", "Option4", "User Comments"};
               area1 = new JTextArea(10, 60);
               label = new JLabel("Guestbook");
               
               for (int k = 0; k <6; k++)
                    {
                        a[k] = "";
                    }
                     
               image[0] =  new ImageIcon ("nature0.jpg");
               image[1] =  new ImageIcon ("nature.jpg");
               image[2] =  new ImageIcon ("nature1.jpg");
               image[3] =  new ImageIcon ("nature3.jpg");
               image[4] =  new ImageIcon ("nature4.jpg");
               image[5] =  new ImageIcon ("nature5.jpg");
              
               cbox = new JComboBox (options);  
               button = new JButton ("Enter Comments");
               
               panelA = new JPanel();
               panelB = new JPanel();
               panelC = new JPanel();
               
               panelB.setSize(800, 800);
               
               area1.setLineWrap(true);
               JScrollPane pane1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
       
               panelA.add(label);
               panelA.add(pane1);
               panelA.add(button);
               
               panelC.add(cbox);
                          
                
               
               cbox.addItemListener(
               
                   new ItemListener()
                   {
                       public void itemStateChanged (ItemEvent e)
                       {
                           j = cbox.getSelectedIndex();
                           if (j == 5)
                           {
                   
                               for (int i = 1; i <5 ; i++)
                               {
                      
                                   answer += "Image#" + i + "\n" + a[i];
                                                 
                               }
                        
                               area1.setText(answer);
                           }
                          
                           repaint();
                          
                       }
                  }
                  
               );                 
               
               button.addActionListener (
               
                new ActionListener()
                {
                    public void actionPerformed (ActionEvent e)
                    {
                       
                        if ( j > 0 && j <5)
                        {
                           a[j] += area1.getText() + "\n";
                           area1.setText("");
                        }
                                                 
                       
                         
                     }
              }
              );
               
               add(panelB, BorderLayout.CENTER);
               add(panelC, BorderLayout.EAST);
               add(panelA, BorderLayout.SOUTH);
             
            }
            
            public void paint(Graphics g)
            {
                                   
                   g.setColor(Color.GRAY);
                   g.fillRect(0, 0, 1000, 1500);
                   g.drawImage (image[j].getImage(), 0,0, image[j].getIconWidth(), image[j].getIconHeight(), panelB);
                   panelA.repaint();
                   panelC.repaint();
                 
              }
      
            
 }


The program works fine if I print the user comments in the paint() method:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Scroll extends JApplet {

            private JTextArea area1, area2;            
            private JComboBox cbox;
            private JButton button;
            private JPanel panelA, panelB, panelC;
            private ImageIcon [] image = new ImageIcon[6];
            private int j = 0, cell = 0;
            private JLabel label;
            private String[] a = new String [6];
            private String answer = "";
            private int count = 0;
            
            
            public void init()
            {
              
               String options[]  = {"Choose", "Option1", "Option2", "Option3", "Option4", "User Comments"};
               area1 = new JTextArea(10, 60);
               label = new JLabel("Guestbook");
               
               for (int k = 0; k <6; k++)
                    {
                        a[k] = "";
                    }
                     
               image[0] =  new ImageIcon ("nature0.jpg");
               image[1] =  new ImageIcon ("nature.jpg");
               image[2] =  new ImageIcon ("nature1.jpg");
               image[3] =  new ImageIcon ("nature3.jpg");
               image[4] =  new ImageIcon ("nature4.jpg");
               image[5] =  new ImageIcon ("nature5.jpg");
              
               cbox = new JComboBox (options);  
               button = new JButton ("Enter Comments");
               
               panelA = new JPanel();
               panelB = new JPanel();
               panelC = new JPanel();
               
               panelB.setSize(800, 800);
               
               area1.setLineWrap(true);
               JScrollPane pane1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
       
               panelA.add(label);
               panelA.add(pane1);
               panelA.add(button);
               
               panelC.add(cbox);
                          
                
               
               cbox.addItemListener(
               
                   new ItemListener()
                   {
                       public void itemStateChanged (ItemEvent e)
                       {
                           j = cbox.getSelectedIndex();
                           
                          
                           repaint();
                          
                       }
                  }
                  
               );                 
               
               button.addActionListener (
               
                new ActionListener()
                {
                    public void actionPerformed (ActionEvent e)
                    {
                       
                        if ( j > 0 && j <5)
                        {
                           a[j] += area1.getText() + "\n";
                           area1.setText("");
                        }
                                                 
                       
                         
                     }
              }
              );
               
               add(panelB, BorderLayout.CENTER);
               add(panelC, BorderLayout.EAST);
               add(panelA, BorderLayout.SOUTH);
             
            }
            
            public void paint(Graphics g)
            {
                   if (j == 5)
                   {
           
                       for (int i = 1; i <5 ; i++)
                       {
              
                           answer += "Image#" + i + "\n" + a[i];
                                         
                       }
                
                       area1.setText(answer);
                   }                
                   g.setColor(Color.GRAY);
                   g.fillRect(0, 0, 1000, 1500);
                   g.drawImage (image[j].getImage(), 0,0, image[j].getIconWidth(), image[j].getIconHeight(), panelB);
                   panelA.repaint();
                   panelC.repaint();
                 
              }
      
            
 }

Question:
Why does the program display the comments twice when the printing of the user comments happens in the ItemListener?

Thank you!

javaprog200
Newbie Poster
21 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Is the itemListener being called two times?

Add a println to show when it is called and what the value of the args are.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I think Norm's onto something there. This is from the JComboBox API doc:
public void addItemListener(ItemListener aListener)
Adds an ItemListener.
aListener will receive one or two ItemEvents when the selected item changes.
A quick print inside the itemStateChanged method will confirm or refute this.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Hello Guys,

Thank you for the replies!

I did precisely that and it duplicates the event only when j = 5. For all other selections, it processes only one event. Just wondering how having an if statement in the ItemListener always creates two events instead of one?

Thank you!

javaprog200
Newbie Poster
21 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Please post the print out that shows what you are talking about. My version prints out two lines for every item I select.

Also show where you added the println.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Hello Norm,

Sorry! I was wrong. I meant to say the output duplicates only when the if statement is inside the itemstatechanged() method and not only when j = 5. So, yes, going by what you and James are saying, it makes sense now.

Thank you again!

javaprog200
Newbie Poster
21 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Do you how to change your code now so it will do what you want?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Presumably there's an ItemEvent.SELECTED and an ItemEvent.DESELECTED when the selection changes. You can query which it is via the event's getStateChange() method, then only update your text on an ItemEvent.SELECTED

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Hello James and Norm,

The last post by James fixed everything. The program works well now. Following is the change I made to the JCombox event handler:

cbox.addItemListener(
               
                   new ItemListener()
                   {
                       public void itemStateChanged (ItemEvent e)
                       {
                           
                           if (e.getStateChange() == e.SELECTED)
                               {
                                   j = cbox.getSelectedIndex();
                                   
                                   if (j == 5)
                                   {
                           
                                       for (int i = 1; i <5 ; i++)
                                       {
                              
                                           answer += "Image#" + i + "\n" + a[i];
                                                         
                                       }
                                
                                       area1.setText(answer);
                                   } 
                               }
                          
                           repaint();
                          
                       }
                  }
                  
               );


Thanks a lot!!

javaprog200
Newbie Poster
21 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You