I have a problem with my code. I am trying to select some elements in a
Jlist to populate another JList. The code does not work as i desire. It
refreshs what is in the second Jlist with the new item selected intead
of increasing the number of items by +1.Could you have a look at the
code and suggest what could be the problem and how i can solve it?
The code is as follows:
jList3.addListSelectionListener(new
javax.swing.event.ListSelectionListener()
{
public void valueChanged(javax.swing.event.ListSelectionEvent e)
{
DefaultListModel listModel = new DefaultListModel();
String listElem = "";
if (e.getValueIsAdjusting() == true) {
if (jList3.getSelectedIndex() > -1) {
//No selection, disable fire button.
listModel.addElement(jList3.getSelectedValue().toString());
jList4.setModel(listModel);
}
}
Rhino - 24 Nov 2005 16:08 GMT
>I have a problem with my code. I am trying to select some elements in a
>
[quoted text clipped - 3 lines]
> of increasing the number of items by +1.Could you have a look at the
> code and suggest what could be the problem and how i can solve it?
Your question is confusing. How many elements do you want to copy from the
first JList to the second JList: just one or several? You said 'elements'
but your code is only getting a single value. Are you aware that there are
different techniques to use, depending on whether you are dealing with a
single value, a range of values, and a discrete set of values?
It sounds like you are saying that the code _is_ successfully copying a
single selected element to the second JList. The copy should also be
incrementing the number of items in the second JList. Are you saying that
the element is being copied but the size of the second JList is not being
incremented?
If you can clarify what you are trying to accomplish and what is actually
happening, it will be a lot easier for someone to help you.
> The code is as follows:
>
[quoted text clipped - 15 lines]
> }
> }
Rhino
zero - 24 Nov 2005 18:33 GMT
"tolu45" <tolu45@gmail.com> wrote in news:1132844358.545847.129390
@o13g2000cwo.googlegroups.com:
> I have a problem with my code. I am trying to select some elements in a
>
[quoted text clipped - 22 lines]
> }
> }
Looking at the code, it seems what you want is to copy one element from
jList3 to jList4, as soon as the element is selected in jList3. But, you
want to add this element to the existing elements in jList4, without
overwriting them (as your code is doing now). Is that correct?
If so, your problem is with the DefaultListModel listModel = new
DefaultListModel(); line. You are creating a new, empty listModel - so
you're not using whatever is already in jList4. Instead, you should use
ListModel listModel = jList4.getModel();
However, like Rhino suggested, if you want to add more than one item,
there are better ways. Have a look at the JList:setSelectionMode(int)
method.