RaylessTiKi
RaylessTiKi

Reputation: 63

Get the selected Object from the List

It's my first time using MouseListener and I don't know how to implement it.

Here's the code:

 DefaultListModel<Object> listModel = new DefaultListModel<Object>();
 try {
listModel = readLines(file);
    //this function basically converts the file in a defaultlistmodel
 } catch (Exception e) {
e.printStackTrace();
 }

  JList<Object> list = new JList<Object>();
  list.setModel(listModel);
  JScrollPane scrollPane = new JScrollPane(list);
  list.setBackground(Color.WHITE);
  list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  list.setLayoutOrientation(JList.VERTICAL);
  scrollPane.setBounds(10, 21, 130, 267);
  westPanel.add(scrollPane, BorderLayout.CENTER);

What I want is to create a mouse listener, that when I click an Object from the list (scroll pane), save it (getElementAt(index)) and implement it elsewhere, like in a different text field.

Upvotes: 1

Views: 191

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Don't use a MouseListener on a JList. Instead use a ListSelectionListener built for the task.

Here's a short example I put together before realising you'd solved the problem just based on that tip. So I'm posting it anyway. 😉

enter image description here

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

public class FontSelector {

    FontSelector() {
        JPanel fontSelectorPanel = new JPanel(new BorderLayout(4, 4));
        GraphicsEnvironment ge = GraphicsEnvironment.
                getLocalGraphicsEnvironment();
        String[] fonts = ge.getAvailableFontFamilyNames();
        final JList fontList = new JList(fonts);
        fontSelectorPanel.add(new JScrollPane(fontList));
        fontList.setCellRenderer(new FontCellRenderer());
        fontList.setVisibleRowCount(10);

        final JTextArea textArea = new JTextArea(
                "The quick brown fox jumps over the lazy dog.", 3, 20);
        fontSelectorPanel.add(new JScrollPane(
                textArea), BorderLayout.PAGE_END);
        textArea.setEditable(false);
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);

        ListSelectionListener fontListener = (ListSelectionEvent e) -> {
            String fontName = fontList.getSelectedValue().toString();
            textArea.setFont(new Font(fontName, Font.PLAIN, 50));
        };
        fontList.addListSelectionListener(fontListener);
        fontList.setSelectedIndex(0);
        JOptionPane.showMessageDialog(null, fontSelectorPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new FontSelector();
        });
    }
}

class FontCellRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        JLabel label = (JLabel) super.getListCellRendererComponent(
                list, value, index, isSelected, cellHasFocus);
        Font font = new Font((String) value, Font.PLAIN, 20);
        label.setFont(font);
        return label;
    }
}

Upvotes: 2

Related Questions