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:hr.fer.zemris.vhdllab.applets.view.compilation.CompilationErrorsView.java

@Override
protected JComponent createControl() {
    model = new DefaultListModel();
    final JList listContent = new JList(model);
    listContent.setFixedCellHeight(15);//from   w  ww .  ja  va2 s.  com
    listContent.addMouseListener(new MouseClickAdapter() {
        @Override
        protected void onDoubleClick(MouseEvent e) {
            String selectedValue = (String) listContent.getSelectedValue();
            highlightError(selectedValue);
        }
    });

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

From source file:Main.java

public ListRenderingFrame() {
    setTitle("ListRendering");
    setSize(400, 300);/*  w w w .  j a v  a2 s  .  co m*/
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    Vector fonts = new Vector();
    fonts.add(new Font("Serif", Font.PLAIN, 8));
    fonts.add(new Font("SansSerif", Font.BOLD, 12));
    fonts.add(new Font("Monospaced", Font.PLAIN, 16));
    fonts.add(new Font("Dialog", Font.ITALIC, 12));
    fonts.add(new Font("DialogInput", Font.PLAIN, 12));
    JList fontList = new JList(fonts);
    fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    fontList.setCellRenderer(new FontCellRenderer());
    JScrollPane scrollPane = new JScrollPane(fontList);

    JPanel p = new JPanel();
    p.add(scrollPane);
    fontList.addListSelectionListener(this);

    getContentPane().add(p, "Center");

    getContentPane().add(label, "South");
}

From source file:hr.fer.zemris.vhdllab.platform.ui.wizard.support.FileSelectionComponent.java

private void createComponent(Predicate projectFilter) {
    projectsCombobox = new JComboBox(projectList(projectFilter));
    new ComboBoxAutoCompletion(projectsCombobox);
    model = new DefaultListModel();
    list = new JList(model);
    list.setPreferredSize(new Dimension(150, 150));
    projectsCombobox.addItemListener(new ItemListener() {
        @Override//  w w w  .j a v  a 2  s  .  co m
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                updateList(e.getItem());
            }
        }
    });

    setLayout(new BorderLayout());
    add(projectsCombobox, BorderLayout.NORTH);
    add(new JScrollPane(list), BorderLayout.CENTER);
}

From source file:stockit.Trader.java

/**
 * Creates new form Trader//  w ww.  ja  va  2s  . c o m
 */
public Trader() {
    initComponents();
    try {
        DefaultListModel demoList = new DefaultListModel();
        DBConnection dbcon = new DBConnection();
        dbcon.establishConnection();
        Statement stmt = dbcon.con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT C.Name"
                + "                       FROM client as C, trader as t, trader_account as tc, account as a"
                + "                       WHERE C.Client_SSN = a.Client_SSN AND "
                + "                       tc.Trader_SSN = t.Trader_SSN "
                + "                       AND a.Trader_SSN = t.Trader_SSN "
                + "                       AND tc.username = \"" + username + "\"");
        while (rs.next()) {
            demoList.addElement(rs.getString("Name"));

        }
        dbcon.con.close();
        listOfClients = new JList(demoList);
        jScrollPane3.setViewportView(listOfClients);
        //setUpTable();
    } catch (Exception ex) {
        Logger.getLogger(clientLogin.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:fr.duminy.jbackup.swing.SourceListTypeMapper.java

@Nonnull
@Override//from ww  w .  j a va  2s .c  o m
public ListPanel<Source, JList<Source>> createEditorComponent() {
    JList<Source> list = new JList(new DefaultMutableListModel<Source>());
    list.setName("sources");
    list.setCellRenderer(SourceRenderer.INSTANCE);

    DefaultFormBuilder sourceFormBuilder = new DefaultFormBuilder<Source>(Source.class) {
        @Override
        protected void configureBuilder(FormBuilder<Source> builder) {
            super.configureBuilder(builder);
            builder.useForProperty("path", new StringPathTypeMapper(SHOW_HIDDEN_FILES_BUILDER));
        }
    };
    SimpleItemManager<Source> sourceProvider = new SimpleItemManager<>(Source.class, sourceFormBuilder, parent,
            "Source", DIALOG);
    return new SourceListPanel(list, sourceProvider);
}

From source file:ListSelectionDemo.java

public ListSelectionDemo() {
    super(new BorderLayout());

    String[] listData = { "one", "two", "three", "four", "five", "six", "seven" };
    String[] columnNames = { "French", "Spanish", "Italian" };
    list = new JList(listData);

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane listPane = new JScrollPane(list);

    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);//from w  w w.java  2  s. c  o m
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    // Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    // Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    listContainer.setBorder(BorderFactory.createTitledBorder("List"));
    listContainer.add(listPane);

    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    // topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(100, 50));
    topHalf.setPreferredSize(new Dimension(100, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    // XXX: next line needed if bottomHalf is a scroll pane:
    // bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 135));
    splitPane.add(bottomHalf);
}

From source file:net.sf.jabref.exporter.ExportToClipboardAction.java

@Override
public void run() {
    BasePanel panel = frame.getCurrentBasePanel();
    if (panel == null) {
        return;/*w w  w  .  j av  a2  s .c  o  m*/
    }
    if (panel.getSelectedEntries().isEmpty()) {
        message = Localization.lang("This operation requires one or more entries to be selected.");
        getCallBack().update();
        return;
    }

    List<IExportFormat> exportFormats = new LinkedList<>(ExportFormats.getExportFormats().values());
    Collections.sort(exportFormats, (e1, e2) -> e1.getDisplayName().compareTo(e2.getDisplayName()));
    String[] exportFormatDisplayNames = new String[exportFormats.size()];
    for (int i = 0; i < exportFormats.size(); i++) {
        IExportFormat exportFormat = exportFormats.get(i);
        exportFormatDisplayNames[i] = exportFormat.getDisplayName();
    }

    JList<String> list = new JList<>(exportFormatDisplayNames);
    list.setBorder(BorderFactory.createEtchedBorder());
    list.setSelectionInterval(0, 0);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    int answer = JOptionPane.showOptionDialog(frame, list, Localization.lang("Select export format"),
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {
                    Localization.lang("Export with selected format"), Localization.lang("Return to JabRef") },
            Localization.lang("Export with selected format"));
    if (answer == JOptionPane.NO_OPTION) {
        return;
    }

    IExportFormat format = exportFormats.get(list.getSelectedIndex());

    // Set the global variable for this database's file directory before exporting,
    // so formatters can resolve linked files correctly.
    // (This is an ugly hack!)
    Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getBibDatabaseContext().getFileDirectory();

    File tmp = null;
    try {
        // To simplify the exporter API we simply do a normal export to a temporary
        // file, and read the contents afterwards:
        tmp = File.createTempFile("jabrefCb", ".tmp");
        tmp.deleteOnExit();
        List<BibEntry> entries = panel.getSelectedEntries();

        // Write to file:
        format.performExport(panel.getBibDatabaseContext(), tmp.getPath(), panel.getEncoding(), entries);
        // Read the file and put the contents on the clipboard:
        StringBuilder sb = new StringBuilder();
        try (Reader reader = new InputStreamReader(new FileInputStream(tmp), panel.getEncoding())) {
            int s;
            while ((s = reader.read()) != -1) {
                sb.append((char) s);
            }
        }
        ClipboardOwner owner = (clipboard, content) -> {
            // Do nothing
        };
        RtfSelection rs = new RtfSelection(sb.toString());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(rs, owner);
        message = Localization.lang("Entries exported to clipboard") + ": " + entries.size();

    } catch (Exception e) {
        LOGGER.error("Error exporting to clipboard", e); //To change body of catch statement use File | Settings | File Templates.
        message = Localization.lang("Error exporting to clipboard");
    } finally {
        // Clean up:
        if ((tmp != null) && !tmp.delete()) {
            LOGGER.info("Cannot delete temporary clipboard file");
        }
    }
}

From source file:com.sshtools.common.ui.HostsTab.java

/**
 * Creates a new HostsTab object./*  ww  w .  j  a v  a 2 s .c om*/
 *
 * @param hostKeyVerifier
 */
public HostsTab(AbstractKnownHostsKeyVerification hostKeyVerifier) {
    super();
    this.hostKeyVerifier = hostKeyVerifier;
    hosts = new JList(model = new HostsListModel());
    hosts.setVisibleRowCount(10);
    hosts.setCellRenderer(new HostRenderer());
    hosts.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            setAvailableActions();
        }
    });
    remove = new JButton("Remove", new ResourceIcon(REMOVE_ICON));
    remove.addActionListener(this);

    //deny = new JButton("Deny", new ResourceIcon(DENY_ICON));
    //deny.addActionListener(this);
    JPanel b = new JPanel(new GridBagLayout());
    b.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 0));

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(0, 0, 4, 0);
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weightx = 1.0;
    UIUtil.jGridBagAdd(b, remove, gbc, GridBagConstraints.REMAINDER);
    gbc.weighty = 1.0;

    //UIUtil.jGridBagAdd(b, deny, gbc, GridBagConstraints.REMAINDER);
    JPanel s = new JPanel(new BorderLayout());
    s.add(new JScrollPane(hosts), BorderLayout.CENTER);
    s.add(b, BorderLayout.EAST);

    IconWrapperPanel w = new IconWrapperPanel(new ResourceIcon(GLOBAL_ICON), s);

    //  This tab
    setLayout(new BorderLayout());
    add(w, BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    reset();
}

From source file:ChooseDropActionDemo.java

public ChooseDropActionDemo() {
    super("ChooseDropActionDemo");

    for (int i = 15; i >= 0; i--) {
        from.add(0, "Source item " + i);
    }// ww w. j a  v a  2  s  .co m

    for (int i = 2; i >= 0; i--) {
        copy.add(0, "Target item " + i);
        move.add(0, "Target item " + i);
    }

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    dragFrom = new JList(from);
    dragFrom.setTransferHandler(new FromTransferHandler());
    dragFrom.setPrototypeCellValue("List Item WWWWWW");
    dragFrom.setDragEnabled(true);
    dragFrom.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JLabel label = new JLabel("Drag from here:");
    label.setAlignmentX(0f);
    p.add(label);
    JScrollPane sp = new JScrollPane(dragFrom);
    sp.setAlignmentX(0f);
    p.add(sp);
    add(p, BorderLayout.WEST);

    JList moveTo = new JList(move);
    moveTo.setTransferHandler(new ToTransferHandler(TransferHandler.COPY));
    moveTo.setDropMode(DropMode.INSERT);
    JList copyTo = new JList(copy);
    copyTo.setTransferHandler(new ToTransferHandler(TransferHandler.MOVE));
    copyTo.setDropMode(DropMode.INSERT);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    label = new JLabel("Drop to COPY to here:");
    ;
    label.setAlignmentX(0f);
    p.add(label);
    sp = new JScrollPane(moveTo);
    sp.setAlignmentX(0f);
    p.add(sp);
    label = new JLabel("Drop to MOVE to here:");
    label.setAlignmentX(0f);
    p.add(label);
    sp = new JScrollPane(copyTo);
    sp.setAlignmentX(0f);
    p.add(sp);
    p.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
    add(p, BorderLayout.CENTER);

    ((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));

    getContentPane().setPreferredSize(new Dimension(320, 315));
}

From source file:kenh.xscript.elements.Debug.java

private void initial(Container c) {
    c.setLayout(new BorderLayout());

    // Add variable list

    DefaultListModel<String> model = new DefaultListModel();

    if (this.getEnvironment() != null) {
        java.util.Set<String> keys = this.getEnvironment().getVariables().keySet();
        for (String key : keys) {
            model.addElement(key);/*from www  .j a  v a2  s  .  c om*/
        }
    } else {
        for (int i = 1; i < 10; i++) {
            model.addElement("Variable " + i);
        }
        model.addElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }

    JList list = new JList(model);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane listPane = new JScrollPane();
    listPane.setViewportView(list);
    listPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    c.add(listPane, BorderLayout.EAST);

    list.setPreferredSize(new Dimension(150, list.getPreferredSize().height));

    // 

    JTextField quote = new JTextField();
    quote.requestFocus();

    //JButton button = new JButton(">>");

    JPanel quotePanel = new JPanel();
    quotePanel.setLayout(new BorderLayout());
    quotePanel.add(quote, BorderLayout.CENTER);
    //quotePanel.add(button, BorderLayout.EAST);

    JTextArea result = new JTextArea();
    result.setEditable(false);

    JScrollPane resultPane = new JScrollPane();
    resultPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    resultPane.setViewportView(result);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(quotePanel, BorderLayout.NORTH);
    panel.add(resultPane, BorderLayout.CENTER);

    c.add(panel, BorderLayout.CENTER);

    list.addListSelectionListener(this);
    //button.addActionListener(this);
    quote.addKeyListener(this);

    this.result = result;
}