Problem in the setListData

Asked

Viewed 115 times

1

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

I’m having trouble on the line copyJList.setListData(colorJList.getSelectedValue());

Could someone 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 making this mistake:

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 cannot be converted to java.lang.Object[]) method javax.swing.Jlist.setListData(java.util.Vector) is not applicable (argument Mismatch; java.lang.Object cannot be converted to java.util.Vector)


Selecting items from the list to assign to another list, every time you click on the button generates the error

Selecionar os itens

  • 2

    What is the mistake that is happening?

1 answer

1

As can be seen in the documentation, the method setListData() expects to receive either an array or a type Vector, and with colorJList.getSelectedValue() you’re not going through either of the two types.

I highlight the documentation of the method getSelectedValue():

Returns the value for the smallest 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 selected index. Returns null if there is no selection.

You haven’t given any additional explanation, so there’s no way to suggest or fix anything, so it’s important to always provide context for the problem and not just the error and the code.

It is always important also to check the documentation of the method, almost always the reading avoids this type error.

  • I thought this would keep the array...: colorJList = new Jlist(colorNames);

  • @Welitonfigueiredo Have you ever read the documentation of the getselectedValue method? It is important to read the documentation of the methods before using it, it explains what this method returns.

  • I understood that returns a list of what is being selected within the list

  • 1

    @Welitonfigueiredo destaco da documentation: Retorna o valor para o menor índice de célula selecionado; o valor selecionado quando apenas um único item é selecionado na lista. Quando vários itens são selecionados, é simplesmente o valor do menor índice selecionado. Retorna nullse não houver seleção.. Do you see? Returns only a selected index, does not return list, I think Oce misunderstood the method

  • I understood that setListData needs to be an array, so I did it to work: Object[] Objects = colorJList.getSelectedValuesList(). toArray();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.