The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

ArrayList Iterator and incorrect results

Posted by wonder on November 22, 2000 at 10:50 AM

If a user enters a letter for first initial, selects an element from the combo box and presses Save button, the input is save in an arraylist. These steps can be repeated. When the Send button is clicked, the data from the arraylist is displayed to a screen. The problem is when multiple entries are saved to the array list, only the last entry is displayed. For example: first element of array list is A 1, second element of array list is B 2 and the third element of array list is C 3, the result are shown as C 3 C 3 C 3. It should be A 1 B 2 C 3. Does anybody know why?
Thanks

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.Integer;
import javax.swing.BorderFactory.*;
import javax.swing.border.Border;

import java.util.ArrayList;
import java.util.Iterator;

public class TestList extends JApplet
{
private JPanel firstPanel, svcPanel, group3, buttonGroup, mainPanel;
private JTextField firstInitTextF;
private JLabel firstInitLabel, svcLabel;
private JComboBox svcCombo;
private JButton saveButton, sendButton;

private String[] svcStr = {"", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "M", "N"};

Alist aircrewObj = new Alist();

InnerTextField t1 = new InnerTextField();
InnerComboBox c1 = new InnerComboBox();
InnerButton b1 = new InnerButton();

ArrayList mylist;

public void init()
{
firstInitLabel = new JLabel("FIRST INIT");
firstInitTextF = new JTextField(2);
svcLabel = new JLabel("SVC");
svcCombo = new JComboBox();
firstPanel = new JPanel(new BorderLayout());
svcPanel = new JPanel(new BorderLayout());
group3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttonGroup = new JPanel(new FlowLayout());
mainPanel = new JPanel(new BorderLayout());
Border etched = BorderFactory.createEtchedBorder();
saveButton = new JButton("Save");
saveButton.setMnemonic(KeyEvent.VK_A);
sendButton = new JButton("Send");
sendButton.setMnemonic(KeyEvent.VK_E);

int i = 0;

for(i = 0; i < svcStr.length; i++)
svcCombo.addItem(svcStr[i]);

Container cp = getContentPane();
cp.setLayout(new FlowLayout());

firstPanel.add(firstInitLabel, BorderLayout.NORTH);
firstPanel.add(firstInitTextF, BorderLayout.SOUTH);
svcPanel.add(svcLabel, BorderLayout.NORTH);
svcPanel.add(svcCombo, BorderLayout.SOUTH);
group3.add(firstPanel);
group3.add(svcPanel);
group3.setBorder(etched);
buttonGroup.add(saveButton);
buttonGroup.add(sendButton);
mainPanel.add(group3, BorderLayout.NORTH);
mainPanel.add(buttonGroup, BorderLayout.SOUTH);
cp.add(mainPanel);

firstInitTextF.addFocusListener(t1);
svcCombo.addActionListener(c1);

saveButton.addActionListener(b1);
sendButton.addActionListener(b1);

mylist = new ArrayList();
}

class InnerButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == saveButton)
{
mylist.add(aircrewObj);
System.out.println("DEBUGGING PURPOSE:" + aircrewObj.getFirstInit() + ";" + aircrewObj.getSvc());
}

else if(e.getSource() == sendButton)
{
Iterator iterList = mylist.iterator();
Alist t2 = new Alist();

while(iterList.hasNext())
{
t2 = (Alist)iterList.next();
System.out.println("" + t2.getFirstInit() + ";" + t2.getSvc());
//iterList.remove();
}
}
}
}

class InnerComboBox implements ActionListener
{
private String str = "";

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == svcCombo)
{
str = (String)svcCombo.getSelectedItem();
aircrewObj.setSvc(str);
}
}
}

class InnerTextField implements FocusListener
{
private String str = "";

public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
if(e.getSource() == firstInitTextF)
{
str = firstInitTextF.getText();

//Invalid if the length is not equal to 1
if(str.length() != 1)
{
firstInitTextF.setText("");
firstInitTextF.requestFocus();
}

//Ensure the input is a letter then convert to upper case
else if(Character.isLetter(str.charAt(0)) == true)
{
str = str.toUpperCase();

firstInitTextF.setText(str);
aircrewObj.setFirstInit(str);
}
else
{
firstInitTextF.setText("");
firstInitTextF.requestFocus();
}
}
}
}
}

class Alist
{
private String firstInit, svc;

public String toString()
{
return "" + firstInit + "," + svc;
}

public String getFirstInit()
{
return firstInit;
}

public void setFirstInit(String f)
{
firstInit = f;
}

public String getSvc()
{
return svc;
}

public void setSvc(String s)
{
svc = s;
}
}



Replies:
  • TEST asasassas July 23, 2001 at 8:39 AM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us