JList - how to select item

I want to select a particular item in list box. for this i have following

jList2.setSelectedIndex(index);//index of particular element

jList2.ensureIndexIsVisible(index);

But above code is not selecting the element nor scrolling and showing in screen. Element appears in list box but not get selected. Am i doing anythng wrong here?

[389 byte] By [ysrpa] at [2007-11-27 8:23:04]
# 1
works OK for me.post a sample program that displays the problem.just a frame with scrollpane/list and a button with actionListener thatcontains the setSelectedIndex and ensureIndexIsVisible code
Michael_Dunna at 2007-7-12 20:11:53 > top of Java-index,Desktop,Core GUI APIs...
# 2

Sorry for delayed reply. My internet connection was down so couldnt reply. Here is my code. I still have same problem. Unable to select element in listbox.

public class MyFrame extends javax.swing.JFrame implements ActionListener{

private javax.swing.JList jList1;

private javax.swing.JList jList2;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JScrollPane jScrollPane2;

private javax.swing.JButton saveButton;

private javax.swing.DefaultListModel matchAclModel, dontmatchAclModel;

private java.util.Vector dataElements = null;

public MyFrame() {

jScrollPane1 = new javax.swing.JScrollPane();

matchAclModel = new javax.swing.DefaultListModel();

jList1 = new javax.swing.JList(matchAclModel);

jScrollPane2 = new javax.swing.JScrollPane();

dontmatchAclModel = new javax.swing.DefaultListModel();

jList2 = new javax.swing.JList(dontmatchAclModel);

saveButton = new javax.swing.JButton();

jScrollPane1.setViewportView(jList1);

jScrollPane2.setViewportView(jList2);

saveButton.setText("Save");

//here code for adding them to frame

//....................

pack();

saveButton.addActionListener(this);

fillVector();

//following code fills the listbox by reading data from vector

for(int index=0; index<dataElements.size();index++) {

matchAclModel.addElement(dataElements.elementAt(index));

dontmatchAclModel.addElement(dataElements.elementAt(index));

}

}

public void fillVector() {

//code that reads a file and fills the vector

}

//in following method msgtext is instance variable that contains either always or never string

public void displaySelected(String data, int index) {

String array[] = data.split("\\s");

if(array == null) {}//todo: display error}

if(array[1] == null) {}//todo: display error or do nothing

System.out.println(msgtext + ":" + array[2].substring(1,array[2].length()));

if(msgtext.equalsIgnoreCase("always")) {

jList1.setSelectedIndex(index);

jList1.ensureIndexIsVisible(index);

}else if(msgtext.equalsIgnoreCase("never")) {

if(array[2].startsWith("!")) {

array[2] = array[2].substring(1, array[2].length());

}

jList2.setSelectedIndex(index);

jList2.ensureIndexIsVisible(index);

}

}

public void actionPerformed(ActionEvent evt) {

if(evt.getSource() == saveButton) {

displaySelected("always",5);

}

}

>

ysrpa at 2007-7-12 20:11:54 > top of Java-index,Desktop,Core GUI APIs...
# 3
"post a sample program that displays the problem."a program is something we can copy/paste/compile/run and see the behaviour,without having to do anything else.anyway, a quick readif(msgtext.equalsIgnoreCase("always")) {what is msgtext?
Michael_Dunna at 2007-7-12 20:11:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

> "post a sample program that displays the problem."

>

> a program is something we can copy/paste/compile/run

> and see the behaviour,

> without having to do anything else.

>

Whatever i posted in previous reply is my actual code. The missing code is gui design code which is auto generated by netbeans. My actual requirement is to fill the list box and read a file and a text and select this text read from file , matching element in listbox.

> anyway, a quick read

> if(msgtext.equalsIgnoreCase("always")) {

>

> what is msgtext?

msgtext is a string which contains either strings "always" or "never"

ysrpa at 2007-7-12 20:11:54 > top of Java-index,Desktop,Core GUI APIs...