Home arrow static arrow Java Programming [Archive] - Vector class, insertElementAt() method and JComboBox
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Vector class, insertElementAt() method and JComboBox
This topic has 7 replies on 1 page.

Posts:22
Registered: 7/28/04
Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 4:26 AM



 

Hi guys,

I am using a vector called vectorSquare. I have an (Square)object in each component. When I run the program, depending on how many squares the user chooses, the combobox size will be vectorSquare.size(). Everything is all right. But if the user reenter data for one of the square, I think the vectorSquare.size() increases 1. Using vSquares.insertElementAt(temp, comboBox.getSelectedIndex()); when the user enters the data works OK, but if the user wants to change and reenter the data of a square, the new solution will be through the getSelectedIndex()) giving by the JComboBox. Instead, the past result will keep displaying, in getSelectedIndex() + 1 index. I don't know why. I thought insertElementAt() inserts new data but delete the past one, but it doesn't seem at all. Thats why I have decided to delete the past entering. But doing that, it compiles but now doesn't run, and gives an error to me here:

--> if (vSquares.elementAt(comboBox.getSelectedIndex()) != null) {

At the beginning I used:
vSquares.insertElementAt(temp, comboBox.getSelectedIndex());


But it overwrites, so I now use this:

if (vSquares.elementAt(comboBox.getSelectedIndex()) != null) {
vSquares.removeElementAt(comboBox.getSelectedIndex());
vSquares.insertElementAt(temp, comboBox.getSelectedIndex());
}

But it doesnt run as I said above.

Anybody knows what I could do to work it out?

Thank you.

 

Posts:441
Registered: 2/25/04
Re: Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 5:02 AM (reply 1 of 7)



 
[url http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#insertElementAt(java.lang.Object,%20int)]insertElementAt(java.lang.Object, int)[/url]inserts an element at the position.
If you want to replace, use [url http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#set(int,%20java.lang.Object)]set(int, java.lang.Object)[/url].
The reason your second attempt fails is that you have removed the selected element, therefore it is no longer selected, and comboBox.getSelectedIndex() will return -1. Store the selected index in a local variable to avoid that behaviour.

Pete

 

Posts:2,909
Registered: 13.8.2003
Re: Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 5:02 AM (reply 2 of 7)



 
setElementAt()?
 

Posts:22
Registered: 7/28/04
Re: Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 5:50 AM (reply 3 of 7)



 

Hi, its me again. I am checking and it still doesn't work. I have tried these different ways and I dont know whats going on??????

vSquares.insertElementAt(temp,comboBox.getSelectedIndex());
vSquares.removeElementAt(comboBox.getSelectedIndex()+1);

++++++++++++++++++++++

vSquares.setElementAt(temp,comboBox.getSelectedIndex());


/++++++++++++++++++++

int p = comboBox.getSelectedIndex();

vSquares.removeElementAt(p);
vSquares.insertElementAt(temp,p);


++++++++++++++++++++++++
int p = comboBox.getSelectedIndex();
vSquares.setElementAt(temp,p);

 

Posts:19,725
Registered: 9/26/01
Re: Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 5:57 AM (reply 4 of 7)



 
...and it still doesn't work.
And yet you're supposedly using the API correctly, so that it should. On the unlikelihood that there's actually a bug in the API, the obviously you are still doing something wrong. But all you're posting is little snippets of supposedly correct API usage and claiming "it doesn't work". What kind of help do you expect here for that?
[url=www.catb.org/~esr/faqs/smart-questions.html]How to ask questions the smart way[/url]
 

Posts:19,725
Registered: 9/26/01
Re: Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 6:01 AM (reply 5 of 7)



 
Rats. The above URL should read: http://www.catb.org/_esr/faqs/smart-questions.html
 

Posts:22
Registered: 7/28/04
Re: Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 6:03 AM (reply 6 of 7)



 

Sorry, but you can imagine how I feel after 2 hours and half trying to work it out. I think it is a silly error, thats why I am nervious.

But you are right. I am sorry Warnerja, I didnt mean to disturb you.

Peter
 

Posts:22
Registered: 7/28/04
Re: Vector class, insertElementAt() method and JComboBox  
Aug 4, 2004 7:01 AM (reply 7 of 7)



 
Hi!, it is now working. This is what I have written in the end

Note: temp is a Square object

if (vSquares.isEmpty()) {
vSquares.insertElementAt(temp, comboBox.getSelectedIndex());

} else {

if (vSquares.size() > comboBox.getSelectedIndex()) {
vSquares.setElementAt(temp, comboBox.getSelectedIndex());

} else {
vSquares.insertElementAt(temp, comboBox.getSelectedIndex());
}
}

 
This topic has 7 replies on 1 page.