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!