Vectors and Event handling
Hi,
I'm relatively new to Java and programming in general. Recently, I have
come across a problem regarding Vectors. Basically, in reference to the
code below, I find that when I press the delete button, the relevant elements
in the 4 vectors are deleted(which is what I want). But, what also happens
is that the next element (in the vector) is also automatically deleted.
Also the index number tends to automatically increment. Can somebody tell
me where the problem lies and how it can be resolved.
[javacode]
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboTest extends JFrame {
private JPanel labelPanel, fieldPanel, buttonPanel2;
public JTextField number, surname, firstname, email, course;
private JButton delRec;
private JComboBox j;
private String selectedCombNumber;
private int itemIndex;
//private Vector a, b, c, d;
public ComboTest()
{
final Vector a = new Vector();
final Vector b = new Vector();
final Vector c = new Vector();
final Vector d = new Vector();
a.addElement("Smith");
a.addElement("Mike");
a.addElement("John");
a.addElement("Alan");
a.addElement("Ian");
b.addElement("Khan");
b.addElement("Hus");
b.addElement("Ahmed");
b.addElement("Patel");
b.addElement("Singh");
c.addElement( "yewfyiw");
c.addElement( "hihihi");
c.addElement( "ejgtlkedajtglk");
c.addElement( "sdgscz");
c.addElement( "yaefrwwd");
d.addElement( "Comp");
d.addElement( "CompSci");
d.addElement( "Comb");
d.addElement( "SoftEng");
d.addElement( "Comp");
Container p = getContentPane();
setSize(500, 300);
final JButton delRec = new JButton();
final JComboBox j = new JComboBox();
j.addItem( "1" );
j.addItem( "2" );
j.addItem( "3" );
j.addItem( "4" );
j.addItem( "5" );
j.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e)
{
//System.out.print("itemStateChanged method called\n");
int g = j.getSelectedIndex(); // Get the index number of the selected
item in the combobox
currentItemIndex(g);
sendStudentNumber(StudentStringValInCombo);
//System.out.print("current item index (when itemstatechanged)=
" + g + "\n");
retrieveVectorElements(a,b,c,d, delRec);
}// end of itemstatechanged
} //end of itemlistener
); //end of additemlistener
labelPanel = new JPanel();
labelPanel.setLayout( new GridLayout(5,1));
labelPanel.add(new JLabel("Student Number"));
labelPanel.add(new JLabel("Surname"));
labelPanel.add(new JLabel("Firstname"));
labelPanel.add(new JLabel("Email Address"));
labelPanel.add(new JLabel("Course"));
//Textfield panel setup
fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(5,1));
number = new JTextField(20);
number.setEditable(false);
fieldPanel.add(number);
surname = new JTextField(20);
fieldPanel.add(surname);
firstname = new JTextField(20);
fieldPanel.add(firstname);
email = new JTextField(20);
fieldPanel.add(email);
course = new JTextField(20);
fieldPanel.add(course);
p.setLayout(new GridLayout(1,3));
p.add(labelPanel);
p.add(fieldPanel);
p.add(j);
p.add(delRec);
show();
}
public void retrieveVectorElements(Vector e, Vector f, Vector g, Vector h,
JButton dr)
{
System.out.print("retrieveVectorElements called\n");
JButton delRec = dr;
Vector a = e;
Vector b = f;
Vector c = g;
Vector d = h;
int currentIndex = getCurrentItemIndex();
Object v1 = a.get(currentIndex);
Object v2 = b.get(currentIndex);
Object v3 = c.get(currentIndex);
Object v4 = d.get(currentIndex);
//*************************
// Convert the objects to strings
String s1 = v1.toString();
String s2 = v2.toString();
String s3 = v3.toString();
String s4 = v4.toString();
surname.setText(s1);
firstname.setText(s2);
email.setText(s3);
course.setText(s4);
deleteRecord(delRec, currentIndex, a,b,c,d);
} // End of retrieveVectorElements
public void deleteRecord(JButton rd, int cI,Vector e, Vector f, Vector g,
Vector h)
{
System.out.print("deleteRecord Called\n");
final Vector a = e;
final Vector b = f;
final Vector c = g;
final Vector d = h;
final int cIndex = cI;
JButton delRec = rd;
delRec.setText("Delete Rec");
delRec.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.print("currentIndexNumber is " + cIndex + "\n");
clearElements(a,b,c,d,cIndex);
} // End of actionPerformed
} // End of ActionListener
); // End of addActionListener
} // End of deleteRecord
public void currentItemIndex(int item)
{
itemIndex = item;
}
public int getCurrentItemIndex() { return itemIndex; }
public void clearElements(Vector e,Vector f, Vector g, Vector h, int ind)
{
Vector a = e;
Vector b = f;
Vector c = g;
Vector d = h;
int cIndex = ind;
a.removeElementAt(cIndex);
b.removeElementAt(cIndex);
c.removeElementAt(cIndex);
d.removeElementAt(cIndex);
surname.setText("");
firstname.setText("");
email.setText("");
course.setText("");
}
public static void main(String args[])
{
ComboTest vec = new ComboTest();
}
}// End of Class
[/javacode]
Thanks
M_UK
Re: Vectors and Event handling
I haven't understood your program completely. But I did notice that you add
a listener to the delete button in a subroutine that is called repeatedly.
So every time you press the button, you add another listener, I think. And
then when you press the button, each of these listeners is called, so
pressing the button a second time deletes 2 entries and pressing it a third
time deletes 3 entries, and so on.
As I said, I didn't spend much time analyzing your program. But your design
mixes code that creates the user interface with code that handles input
from it. You should have one section of code, that is only executed once,
that sets up the user interface -- buttons, panels, adding listeners, and so
on. And you should have other code, including the code run by the listeners
and other supporting methods, that just reacts to events from the interface
and alters the data in your vectors as you wish.
PC2
Attiq <masalla_uk@yahoo.co.uk> wrote in message
news:3a056ecd$1@news.devx.com...
>
> Hi,
> I'm relatively new to Java and programming in general. Recently, I
have
> come across a problem regarding Vectors. Basically, in reference to the
> code below, I find that when I press the delete button, the relevant
elements
> in the 4 vectors are deleted(which is what I want). But, what also
happens
> is that the next element (in the vector) is also automatically deleted.
> Also the index number tends to automatically increment. Can somebody tell
> me where the problem lies and how it can be resolved.
>