简体   繁体   中英

Swing JList and DefaultListModel.

How I can get a selected item?

I have a simple requirement in my code: I need to get a selected item to realize a ActionListener that is enable a JButton .

    public class Tela extends JFrame{
    private static final long serialVersionUID = 1L;
    private DefaultListModel<String> model;
    private JList<String> list;

    public Tela(){
        setTitle("Maquina de Refrigerante");
         String labels[] = { "Coca-Cola", "Fanta Laranja", "Fanta-Uva", 
                 "Sprite"};

        //Cria os Paineis no Frame
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        JPanel downPanel = new JPanel();
        leftPanel.setLayout(new BorderLayout());

        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
        list = new JList<String>(labels);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

        JScrollPane pane = new JScrollPane();
        pane.getViewport().add(list);  
        leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        leftPanel.add(pane);

        final JButton comprar = new JButton("Comprar");
        comprar.setEnabled(false);       
        rightPanel.add(comprar);
        rightPanel.add(Box.createRigidArea(new Dimension(0,4)));
        rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

        panel.add(leftPanel);
        panel.add(rightPanel);
        panel.add(downPanel);
        add(panel);

        setSize(450, 350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        comprar.addActionListener(new Paga());
    }


}

I want to implement an ActionListener that get the item selected and so enable a jbutton("Comprar") , but I don't have any idea at this moment to do it.

getSelectedIndex() and getSelectedIndices() will give the index of the selected item

In addition to MadProgrammer 's answer, It is preferable to use ListSelectionListener of listening for selection state changes event. JList takes care of listening for selection state changes in the selection model, and notifies the given listener of each change. Use it as follows:

jList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent evt) {
               selectedValues = jList1.getSelectedValuesList(); 
                        // it will return a list of values
                        //evt.getFirstIndex() - the index of last selected item
                       //evt.getLastIndex() - the index of current selected item
            }
        });  

You can use JList#getSelectedValue to return the item which is currently selected.

Take a look at How to use buttons and How to write an Action Listener for more details about registering an action listener to your button and How to use lists for details about using JList s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM