Example usage for javax.swing JList JList

List of usage examples for javax.swing JList JList

Introduction

In this page you can find the example usage for javax.swing JList JList.

Prototype

public JList(final Vector<? extends E> listData) 

Source Link

Document

Constructs a JList that displays the elements in the specified Vector.

Usage

From source file:de.dakror.virtualhub.client.dialog.ChooseCatalogDialog.java

public static void show(ClientFrame frame, final JSONArray data) {
    final JDialog dialog = new JDialog(frame, "Katalog whlen", true);
    dialog.setSize(400, 300);/*from w w  w  . j  av  a2s .  c  om*/
    dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    dialog.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            Client.currentClient.disconnect();
            System.exit(0);
        }
    });

    JPanel contentPane = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
    dialog.setContentPane(contentPane);
    DefaultListModel dlm = new DefaultListModel();
    for (int i = 0; i < data.length(); i++) {
        try {
            dlm.addElement(data.getJSONObject(i).getString("name"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    final JList catalogs = new JList(dlm);
    catalogs.setDragEnabled(false);
    catalogs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JScrollPane jsp = new JScrollPane(catalogs, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jsp.setPreferredSize(new Dimension(396, 200));
    contentPane.add(jsp);

    JPanel mods = new JPanel(new GridLayout(1, 2));
    mods.setPreferredSize(new Dimension(50, 22));
    mods.add(new JButton(new AbstractAction("+") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = JOptionPane.showInputDialog(dialog,
                    "Bitte geben Sie den Namen des neuen Katalogs ein.", "Katalog hinzufgen",
                    JOptionPane.PLAIN_MESSAGE);
            if (name != null && name.length() > 0) {
                DefaultListModel dlm = (DefaultListModel) catalogs.getModel();
                for (int i = 0; i < dlm.getSize(); i++) {
                    if (dlm.get(i).toString().equals(name)) {
                        JOptionPane.showMessageDialog(dialog,
                                "Es existert bereits ein Katalog mit diesem Namen!",
                                "Katalog bereits vorhanden!", JOptionPane.ERROR_MESSAGE);
                        actionPerformed(e);
                        return;
                    }
                }

                try {
                    dlm.addElement(name);
                    JSONObject o = new JSONObject();
                    o.put("name", name);
                    o.put("sources", new JSONArray());
                    o.put("tags", new JSONArray());
                    data.put(o);
                    Client.currentClient.sendPacket(new Packet0Catalogs(data));
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }));
    mods.add(new JButton(new AbstractAction("-") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            if (catalogs.getSelectedIndex() != -1) {
                if (JOptionPane.showConfirmDialog(dialog,
                        "Sind Sie sicher, dass Sie diesen\r\nKatalog unwiderruflich lschen wollen?",
                        "Katalog lschen", JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                    DefaultListModel dlm = (DefaultListModel) catalogs.getModel();
                    data.remove(catalogs.getSelectedIndex());
                    dlm.remove(catalogs.getSelectedIndex());
                    try {
                        Client.currentClient.sendPacket(new Packet0Catalogs(data));
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }));

    contentPane.add(mods);

    JLabel l = new JLabel("");
    l.setPreferredSize(new Dimension(396, 14));
    contentPane.add(l);

    JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
    sep.setPreferredSize(new Dimension(396, 10));
    contentPane.add(sep);

    JPanel buttons = new JPanel(new GridLayout(1, 2));
    buttons.setPreferredSize(new Dimension(396, 22));
    buttons.add(new JButton(new AbstractAction("Abbrechen") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            Client.currentClient.disconnect();
            System.exit(0);
        }
    }));
    buttons.add(new JButton(new AbstractAction("Katalog whlen") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            if (catalogs.getSelectedIndex() != -1) {
                try {
                    Client.currentClient
                            .setCatalog(new Catalog(data.getJSONObject(catalogs.getSelectedIndex())));
                    Client.currentClient.frame.setTitle("- " + Client.currentClient.getCatalog().getName());
                    dialog.dispose();
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }));

    dialog.add(buttons);

    dialog.setLocationRelativeTo(frame);
    dialog.setResizable(false);
    dialog.setVisible(true);
}

From source file:SplitPaneTest.java

public SplitPaneFrame() {
    setTitle("SplitPaneTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // set up components for planet names, images, descriptions

    final JList planetList = new JList(planets);
    final JLabel planetImage = new JLabel();
    final JTextArea planetDescription = new JTextArea();

    planetList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent event) {
            Planet value = (Planet) planetList.getSelectedValue();

            // update image and description

            planetImage.setIcon(value.getImage());
            planetDescription.setText(value.getDescription());
        }//from   www.j a va 2  s .c  om
    });

    // set up split panes

    JSplitPane innerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, planetList, planetImage);

    innerPane.setContinuousLayout(true);
    innerPane.setOneTouchExpandable(true);

    JSplitPane outerPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, innerPane, planetDescription);

    add(outerPane, BorderLayout.CENTER);
}

From source file:LongListTest.java

public LongListFrame() {
    setTitle("LongListTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    wordList = new JList(new WordListModel(3));
    wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    wordList.setPrototypeCellValue("www");
    JScrollPane scrollPane = new JScrollPane(wordList);

    JPanel p = new JPanel();
    p.add(scrollPane);/* w  w  w.  j a va  2 s  . c  o m*/
    wordList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            StringBuilder word = (StringBuilder) wordList.getSelectedValue();
            setSubject(word.toString());
        }

    });

    Container contentPane = getContentPane();
    contentPane.add(p, BorderLayout.NORTH);
    label = new JLabel(prefix + suffix);
    contentPane.add(label, BorderLayout.CENTER);
    setSubject("fox");
}

From source file:hr.fer.zemris.vhdllab.applets.view.simulation.SimulationErrorsView.java

@Override
protected JComponent createControl() {
    model = new DefaultListModel();
    JList listContent = new JList(model);
    listContent.setFixedCellHeight(15);//from ww  w  . j  av a 2 s . c  o  m

    simulationManager.addListener(this);
    return new JScrollPane(listContent);
}

From source file:gui.SpamPanel.java

public void generate() {
    Message[] arrMsg = GmailAPI.Spam.toArray(new Message[GmailAPI.Spam.size()]);
    SpamList = new JList(arrMsg);
    SpamList.setCellRenderer(new DefaultListCellRenderer() { // Setting the DefaultListCellRenderer
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            Message message = (Message) value; // Using value we are getting the object in JList
            Map<String, String> map = null;
            try {
                map = GmailAPI.getMessageDetails(message.getId());
            } catch (MessagingException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            }//from   ww  w.j a  v a  2  s . c o m
            String sub = map.get("subject");
            if (map.get("subject").length() > 22) {
                sub = map.get("subject").substring(0, 20) + "...";
            }
            setText(sub); // Setting the text
            //setIcon( shape.getImage() ); // Setting the Image Icon
            return this;
        }
    });
    SpamList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    SpamList.setLayoutOrientation(JList.VERTICAL);
    SpamList.setVisibleRowCount(-1);
    jScrollPane1.setViewportView(SpamList);

    SpamList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            try {
                JList list = (JList) evt.getSource();
                int index = list.locationToIndex(evt.getPoint());
                String id = arrMsg[index].getId();
                Map<String, String> map = GmailAPI.getMessageDetails(id);
                jTextField1.setText(map.get("from"));
                jTextField2.setText(map.get("subject"));
                dateTextField.setText(map.get("senddate"));
                BodyTextPane.setText(map.get("body"));
                BodyTextPane.setContentType("text/html");
                //BodyTextArea.setCo
            } catch (IOException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MessagingException ex) {
                Logger.getLogger(SpamPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

From source file:ListIt.java

public ListIt() {
    JFrame f = new JFrame();
    final PartsListModel pcm = new PartsListModel();
    ListCellRenderer lcr = new MyLabelRenderer();
    JList jl = new JList(pcm);
    jl.setCellRenderer(lcr);/*from   w w  w . j  a  va  2  s.  com*/
    ListSelectionModel lsm = jl.getSelectionModel();
    lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jl.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                String element[] = (String[]) pcm.getElementAt(e.getFirstIndex());
                System.out.println(element[0] + " : " + element[1] + " : " + element[2]);
            }
        }
    });
    JScrollPane jsp = new JScrollPane(jl);
    JComboBox jc = new JComboBox(pcm);
    jc.setRenderer(lcr);
    JButton jb = new JButton("Add Merchandise");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pcm.addElement(partsList[(int) (Math.random() * partsList.length)]);
        }
    });
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.NORTH);
    c.add(jc, BorderLayout.CENTER);
    c.add(jb, BorderLayout.SOUTH);
    f.setSize(250, 250);
    f.show();
}

From source file:ListTest.java

public ListFrame() {
    setTitle("ListTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    String[] words = { "quick", "brown", "hungry", "wild", "silent", "huge", "private", "abstract", "static",
            "final" };

    wordList = new JList(words);
    wordList.setVisibleRowCount(4);/*from  w w  w. j ava  2  s  .c  om*/
    JScrollPane scrollPane = new JScrollPane(wordList);

    listPanel = new JPanel();
    listPanel.add(scrollPane);
    wordList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent event) {
            Object[] values = wordList.getSelectedValues();

            StringBuilder text = new StringBuilder(prefix);
            for (int i = 0; i < values.length; i++) {
                String word = (String) values[i];
                text.append(word);
                text.append(" ");
            }
            text.append(suffix);

            label.setText(text.toString());
        }
    });

    buttonPanel = new JPanel();
    group = new ButtonGroup();
    makeButton("Vertical", JList.VERTICAL);
    makeButton("Vertical Wrap", JList.VERTICAL_WRAP);
    makeButton("Horizontal Wrap", JList.HORIZONTAL_WRAP);

    add(listPanel, BorderLayout.NORTH);
    label = new JLabel(prefix + suffix);
    add(label, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);
}

From source file:Demo.HistGraph.java

public HistGraph(List<String> paraType_list) {
    this.paraType_list = paraType_list;

    // widgets: charts(JFreeChart), parameter selector (JList)
    chartPane = new ChartPanel(null);
    charts = new JFreeChart[paraType_list.size()];

    DefaultListModel modelOfList = new DefaultListModel();
    paraSelector = new JList(modelOfList);

    for (int i = 0; i < paraType_list.size(); i++) {
        modelOfList.addElement(paraType_list.get(i));
        charts[i] = null;/*from   ww  w. j  av a  2  s . co  m*/
    }

    paraSelector.setSelectedIndex(0);

    charts[0] = CreateChart(paraType_list.get(0));
    chartPane.setChart(charts[0]);

    JScrollPane selectorPanel = new JScrollPane();
    selectorPanel.setViewportView(paraSelector);

    paraSelector.addMouseListener(this);

    // layout
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.WEST;

    gbc.insets = new Insets(5, 20, 5, 5);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 20;
    add(new JLabel("Distribution:"), gbc);

    gbc.insets = new Insets(5, 5, 5, 20);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weightx = 1;
    add(new JLabel("Parameters:"), gbc);

    gbc.weighty = 14;

    gbc.insets = new Insets(5, 20, 5, 5);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weightx = 20;
    add(chartPane, gbc);

    gbc.insets = new Insets(5, 5, 15, 20);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.weightx = 1;
    add(selectorPanel, gbc);
}

From source file:components.SplitPaneDemo.java

public SplitPaneDemo() {

    //Create the list of images and put it in a scroll pane.

    list = new JList(imageNames);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);//from  w w w .  ja  v  a 2s.c  o m
    list.addListSelectionListener(this);

    JScrollPane listScrollPane = new JScrollPane(list);
    picture = new JLabel();
    picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
    picture.setHorizontalAlignment(JLabel.CENTER);

    JScrollPane pictureScrollPane = new JScrollPane(picture);

    //Create a split pane with the two scroll panes in it.
    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(150);

    //Provide minimum sizes for the two components in the split pane.
    Dimension minimumSize = new Dimension(100, 50);
    listScrollPane.setMinimumSize(minimumSize);
    pictureScrollPane.setMinimumSize(minimumSize);

    //Provide a preferred size for the split pane.
    splitPane.setPreferredSize(new Dimension(400, 200));
    updateLabel(imageNames[list.getSelectedIndex()]);
}

From source file:org.pf.midea.MainUI.java

private void updatePlanList() {
    scrollPane1.removeAll();//from  w  w  w  .  ja v  a2 s. c om
    panelPlan.removeAll();
    listPlan = null;
    scrollPane1 = null;
    listPlan = new JList(planCells.toArray(new PlanCell[planCells.size()]));
    listPlan.setCellRenderer(new PlanCellRenderer());
    scrollPane1 = new JScrollPane(listPlan);
    panelPlan.add(scrollPane1, new CellConstraints());
    panelPlan.updateUI();
}