problem with JList

T

tolu45

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);
}
}
 
R

Rhino

tolu45 said:
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?

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:


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
 
Z

zero

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);
}
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top