Problem in setListData

1

I made the example of the book "Java How to program".

I'm having trouble with the copyJList.setListData (colorJList.getSelectedValue ()) line;

Someone could help.

package LvProg8.exercicio.capitulo14.exemp8;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MultipleSelectionFrame extends JFrame {
    private JList colorJList; //lista para exibir cores
    private JList copyJList;
    private JButton copyJButton;
    private static final String[] colorNames = {
            "Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta"
            , "Orange", "Pink", "Red", "White", "Yellow"
    };

    //Contrutor
    public MultipleSelectionFrame()
    {
        super("Multiplas seleções");
        setLayout(new FlowLayout());

        colorJList = new JList(colorNames);
        colorJList.setVisibleRowCount(5); //
        colorJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        System.out.println(colorJList.getSelectedValue());

        //adiciona JScrollPane que contem JList ao frame
        add(new JScrollPane(colorJList));

        copyJButton = new JButton("Copy >>>");
        copyJButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        copyJList.setListData(colorJList.getSelectedValue());
                    }
                }
        );
        add(copyJButton);

        copyJList = new JList();
        copyJList.setVisibleRowCount(5);
        copyJList.setFixedCellWidth(100);
        copyJList.setFixedCellHeight(15);
        copyJList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        add(new JScrollPane(copyJList));
    }
}

You're giving this error:

  

Error: (35, 34) java: no suitable method found for   setListData (java.lang.Object)       method javax.swing.JList.setListData (java.lang.Object []) is not applicable         (argument mismatch; java.lang.Object can not be converted to java.lang.Object [])       method javax.swing.JList.setListData (java.util.Vector) is not applicable         (argument mismatch; java.lang.Object can not be converted to java.util.Vector)

Select the list items to assign to another list, every time you click the button it generates the error

    
asked by anonymous 30.04.2018 / 14:02

1 answer

2

As can be seen in the documentation, the setListData() expects to receive either an array or a type Vector , and with colorJList.getSelectedValue() you are not passing either type.

Method documentation highlighting getSelectedValue() :

  

Returns the value for the lowest selected cell index; the value selected when only a single item is selected in the list. When multiple items are selected, it is simply the value of the lowest index selected. Returns null if there is no selection.

You have not given any further explanation, so you have no way to suggest any fixes, so it's important to always provide a context for the problem, not just the error and the code.

It is always important to check the method documentation as well, almost always reading prevents this kind of type error.

    
30.04.2018 / 14:48