Re: Add multiple data items inside JList
On Sun, 15 Jul 2007 12:31:57 -0700, christopher_board@yahoo.co.uk
wrote, quoted or indirectly quoted someone who said :
Hi all. I am currently developing a java application that will add
multiple data items into a JList. I have it adding data into a loop
using JList.setDataList. However it outputs everything correctly into
the loop but it will only add the last thing that the loop did into
the JList. Below is the code that I am using:
I would do it like this.
1. extract the ListModel with getModel.
2. Build a vector of the existing options
3. add to that Vector.
4. sort the Vector
5. Replace the datamodel with your ENTIRE Vector of choices. Don't
keep replaceing the ListModel. Wait till you have it complete.
// THIS IS NOT THE SOLUTION, just your code tidied a bit.
// To understand your code, I first converted it to a conventional for
loop.
// Then I indented it.
// Then I invented a local for the List.
// Then I did some renaming to clarify singlular and plural.
// you wanted to ADD to the list, not replace it.
public void lstComputerNamesInitiate()
{
remoteshutdown.mainScreen.lstComputerNames.setVisibleRowCount(-1);
int computerNumbers = 1;
String adminMachine = "RM49_AD";
String[] admin = { adminMachine};
System.out.println("Computer Name is: " + adminMachine);
JList jlist = remoteshutdown.mainScreen.lstComputerNames;
list.setListData(admin);
for ( int cn = 1; i< 31; i++ )
{
if ( cn < 10 )
{
String computerName =
jlist.getSelectedItem()+ cn;
String computerNames[] = {computerName};
jlist.setListData( ComputerNames);
System.out.println("Computer Name is: " +
computerName);
}
else
{
String computerName = "RM49_" + cn + "";
String[] computerNames = {computerName};
jsist.setListData(computerNames);
System.out.println("Computer Name is: " +
computerNames);
}
}
}
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com