Example usage for javax.swing DefaultListModel DefaultListModel

List of usage examples for javax.swing DefaultListModel DefaultListModel

Introduction

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

Prototype

DefaultListModel

Source Link

Usage

From source file:util.ui.UiUtilities.java

/**
 * Moves Selected Items from one List to another
 *
 * @param fromList/*  ww w .j a  v  a 2s .c o  m*/
 *          Move from this List
 * @param toList
 *          Move into this List
 * @return Moved Elements
 */
public static Object[] moveSelectedItems(JList fromList, JList toList) {
    DefaultListModel fromModel = (DefaultListModel) fromList.getModel();
    DefaultListModel toModel = (DefaultListModel) toList.getModel();

    // get the selection
    int[] selection = fromList.getSelectedIndices();

    if (selection.length == 0) {
        return new Object[] {};
    }

    Object[] objects = new Object[selection.length];
    for (int i = 0; i < selection.length; i++) {
        objects[i] = fromModel.getElementAt(selection[i]);
    }

    // get the target insertion position
    int targetPos = toList.getMaxSelectionIndex();
    if (targetPos == -1) {
        targetPos = toModel.getSize();
    } else {
        targetPos++;
    }

    // suppress updates on both lists
    if (selection.length >= 5) {
        fromList.setModel(new DefaultListModel());
        toList.setModel(new DefaultListModel());
    }

    // move the elements
    for (int i = selection.length - 1; i >= 0; i--) {
        Object value = fromModel.remove(selection[i]);
        toModel.add(targetPos, value);
    }

    if (selection.length >= 5) {
        fromList.setModel(fromModel);
        toList.setModel(toModel);
    }

    // change selection of the fromList
    if (fromModel.getSize() > 0) {
        int newSelection = selection[0];
        if (newSelection >= fromModel.getSize()) {
            newSelection = fromModel.getSize() - 1;
        }
        fromList.setSelectedIndex(newSelection);
    }

    if (selection.length >= 5) {
        fromList.repaint();
        fromList.revalidate();
        toList.repaint();
        toList.revalidate();
    }

    // change selection of the toList
    toList.setSelectionInterval(targetPos, targetPos + selection.length - 1);

    // ensure the selection is visible
    toList.ensureIndexIsVisible(toList.getMaxSelectionIndex());
    toList.ensureIndexIsVisible(toList.getMinSelectionIndex());

    return objects;
}

From source file:zsk.JFCMainClient.java

/**
 * @param pane/*from ww w  .j  av  a 2 s .  co  m*/
 * @param downloadDir 
 */
public void addComponentsToPane(final Container pane, String downloadDir) {
    this.panel = new JPanel();

    this.panel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.WEST;

    JFCMainClient.dlm = new DefaultListModel<String>();
    this.urllist = new JList<String>(JFCMainClient.dlm);
    // TODO maybe we add a button to remove added URLs from list?
    //      this.userlist.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
    this.urllist.setFocusable(false);
    this.textarea = new JTextArea(2, 2);
    this.textarea.setEditable(true);
    this.textarea.setFocusable(false);

    JScrollPane leftscrollpane = new JScrollPane(this.urllist);
    JScrollPane rightscrollpane = new JScrollPane(this.textarea);
    this.middlepane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftscrollpane, rightscrollpane);
    this.middlepane.setOneTouchExpandable(true);
    this.middlepane.setDividerLocation(150);

    Dimension minimumSize = new Dimension(25, 25);
    leftscrollpane.setMinimumSize(minimumSize);
    rightscrollpane.setMinimumSize(minimumSize);

    this.directorybutton = new JButton("", createImageIcon("images/open.png", ""));
    gbc.gridx = 0;
    gbc.gridy = 0;
    this.directorybutton.addActionListener(this);
    this.panel.add(this.directorybutton, gbc);

    this.saveconfigcheckbox = new JCheckBox(isgerman() ? "Konfig. speichern" : "Save config");
    this.saveconfigcheckbox.setSelected(false);

    this.saveconfigcheckbox.addItemListener(this);
    this.panel.add(this.saveconfigcheckbox);

    this.saveconfigcheckbox.setEnabled(false);

    // TODO check if initial download directory exists
    // assume that at least the users homedir exists
    //if (System.getProperty("user.home").equals("/home/knoedel")) shomedir = "/home/knoedel/YouTube Downloads/";
    debugoutput("user.home: ".concat(System.getProperty("user.home")).concat("  shomedir: ".concat(shomedir)));
    debugoutput("os.name: ".concat(System.getProperty("os.name")));
    debugoutput("os.arch: ".concat(System.getProperty("os.arch")));
    debugoutput("os.version: ".concat(System.getProperty("os.version")));
    debugoutput("Locale.getDefault: ".concat(Locale.getDefault().toString()));

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.directorytextfield = new JTextField(downloadDir, 20 + (JFCMainClient.getbDEBUG() ? 48 : 0));
    //this.directorytextfield = new JTextField( shomedir, 20+(JFCMainClient.getbDEBUG()?48:0) );
    this.directorytextfield.setEnabled(false);
    this.directorytextfield.setFocusable(true);
    this.directorytextfield.addActionListener(this);
    this.panel.add(this.directorytextfield, gbc);

    JLabel dirhint = new JLabel(isgerman() ? "Speichern im Ordner:" : "Download to folder:");

    gbc.gridx = 0;
    gbc.gridy = 1;
    this.panel.add(dirhint, gbc);

    debugoutput(String.format("heigth x width: %d x %d", Toolkit.getDefaultToolkit().getScreenSize().width,
            Toolkit.getDefaultToolkit().getScreenSize().height));

    this.middlepane.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width / 3,
            Toolkit.getDefaultToolkit().getScreenSize().height / 4 + (JFCMainClient.getbDEBUG() ? 200 : 0)));

    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weighty = 2;
    gbc.weightx = 2;
    gbc.gridwidth = 2;
    this.panel.add(this.middlepane, gbc);

    // radio buttons for resolution to download
    JFCMainClient.frame.hdbutton = new JRadioButton("HD");
    JFCMainClient.frame.hdbutton.setActionCommand("hd");
    JFCMainClient.frame.hdbutton.addActionListener(this);
    JFCMainClient.frame.hdbutton.setToolTipText("1080p/720p");
    JFCMainClient.frame.stdbutton = new JRadioButton("Std");
    JFCMainClient.frame.stdbutton.setActionCommand("std");
    JFCMainClient.frame.stdbutton.addActionListener(this);
    JFCMainClient.frame.stdbutton.setToolTipText("480p/360p");
    JFCMainClient.frame.ldbutton = new JRadioButton("LD");
    JFCMainClient.frame.ldbutton.setActionCommand("ld");
    JFCMainClient.frame.ldbutton.addActionListener(this);
    JFCMainClient.frame.ldbutton.setToolTipText("< 360p");

    JFCMainClient.frame.stdbutton.setSelected(true);
    JFCMainClient.frame.hdbutton.setEnabled(true);
    JFCMainClient.frame.ldbutton.setEnabled(true);

    ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(JFCMainClient.frame.hdbutton);
    bgroup.add(JFCMainClient.frame.stdbutton);
    bgroup.add(JFCMainClient.frame.ldbutton);

    JPanel radiopanel = new JPanel(new GridLayout(1, 0));
    radiopanel.add(JFCMainClient.frame.hdbutton);
    radiopanel.add(JFCMainClient.frame.stdbutton);
    radiopanel.add(JFCMainClient.frame.ldbutton);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridheight = 0;
    gbc.gridwidth = 0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.NORTHEAST;
    this.panel.add(radiopanel, gbc);

    // radio buttons for video format to download
    JFCMainClient.frame.mpgbutton = new JRadioButton("MPEG");
    JFCMainClient.frame.mpgbutton.setActionCommand("mpg");
    JFCMainClient.frame.mpgbutton.addActionListener(this);
    JFCMainClient.frame.mpgbutton.setToolTipText("Codec: H.264 MPEG-4");
    JFCMainClient.frame.webmbutton = new JRadioButton("WEBM");
    JFCMainClient.frame.webmbutton.setActionCommand("webm");
    JFCMainClient.frame.webmbutton.addActionListener(this);
    JFCMainClient.frame.webmbutton.setToolTipText("Codec: Google/On2's VP8 or Googles WebM");
    JFCMainClient.frame.flvbutton = new JRadioButton("FLV");
    JFCMainClient.frame.flvbutton.setActionCommand("flv");
    JFCMainClient.frame.flvbutton.addActionListener(this);
    JFCMainClient.frame.flvbutton.setToolTipText("Codec: Flash Video (FLV1)");

    bgroup = new ButtonGroup();
    bgroup.add(JFCMainClient.frame.mpgbutton);
    bgroup.add(JFCMainClient.frame.webmbutton);
    bgroup.add(JFCMainClient.frame.flvbutton);

    JFCMainClient.frame.mpgbutton.setSelected(true);
    JFCMainClient.frame.mpgbutton.setEnabled(true);
    JFCMainClient.frame.webmbutton.setEnabled(true);
    JFCMainClient.frame.flvbutton.setEnabled(true);

    JFCMainClient.frame.save3dcheckbox = new JCheckBox("3D");
    JFCMainClient.frame.save3dcheckbox.setToolTipText("stereoscopic video");
    JFCMainClient.frame.save3dcheckbox.setSelected(false);
    JFCMainClient.frame.save3dcheckbox.setEnabled(true);
    JFCMainClient.frame.save3dcheckbox.addItemListener(this);

    radiopanel = new JPanel(new GridLayout(1, 0));
    radiopanel.add(JFCMainClient.frame.save3dcheckbox);
    radiopanel.add(JFCMainClient.frame.mpgbutton);
    radiopanel.add(JFCMainClient.frame.webmbutton);
    radiopanel.add(JFCMainClient.frame.flvbutton);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridheight = 0;
    gbc.gridwidth = 0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.NORTHEAST;
    this.panel.add(radiopanel, gbc);

    JLabel hint = new JLabel(
            isgerman() ? "eingeben, reinkopieren, reinziehen von YT-Webadressen oder YT-Videobilder:"
                    : "Type, paste or drag'n drop a YouTube video address:");

    gbc.fill = 0;
    gbc.gridwidth = 0;
    gbc.gridheight = 1;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.anchor = GridBagConstraints.WEST;
    this.panel.add(hint, gbc);

    this.textinputfield = new JTextField(20);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.gridwidth = 2;
    this.textinputfield.setEnabled(true);
    this.textinputfield.setFocusable(true);
    this.textinputfield.addActionListener(this);
    this.textinputfield.getDocument().addDocumentListener(this);
    this.panel.add(this.textinputfield, gbc);

    this.quitbutton = new JButton("", createImageIcon("images/exit.png", ""));
    gbc.gridx = 2;
    gbc.gridy = 5;
    gbc.gridwidth = 0;
    this.quitbutton.addActionListener(this);
    this.quitbutton.setActionCommand("quit");
    this.quitbutton.setToolTipText("Exit.");

    this.panel.add(this.quitbutton, gbc);

    pane.add(this.panel);
    addWindowListener(this);

    JFCMainClient.frame.setDropTarget(new DropTarget(this, this));
    JFCMainClient.frame.textarea.setTransferHandler(null); // otherwise the dropped text would be inserted

}

From source file:zsk.JFCMainClient.java

public static void runCLI(boolean setVisibleFlg, String downloadDir) {
    JFCMainClient.dlm = new DefaultListModel<String>();

    Boolean bStartThreads = false;
    int istarturls = 0;
    try {//from   w  w  w .j a  v a 2 s .c om
        istarturls = parseargs(JFCMainClient.saargs);
    } catch (ArrayIndexOutOfBoundsException aioob) {
        output(JFCMainClient.isgerman() ? "nicht gengend Parameter" : "not enough parameters");
        debugoutput(JFCMainClient.isgerman() ? "nicht gengend Parameter" : "not enough parameters");
        istarturls = JFCMainClient.saargs.length;
    }

    if (istarturls < JFCMainClient.saargs.length) {
        for (int i = istarturls; i < JFCMainClient.saargs.length; i++) {
            if (JFCMainClient.saargs[i].matches(szYTREGEX.concat(".*"))) {
                JFCMainClient.addYTURLToList(JFCMainClient.saargs[i]);
                debugoutput("adding URL: ".concat(JFCMainClient.saargs[i]));
                bStartThreads = true;
            } else {
                debugoutput("wrong URL: ".concat(JFCMainClient.saargs[i]));
                output(String.format(
                        (JFCMainClient.isgerman() ? "URL: %d sieht nicht aus wie eine YouTube-URL - %s"
                                : "URL: %d does not look like a youtube-URL - %s"),
                        i, JFCMainClient.saargs[i]));
            }
        }
    }
    if (bStartThreads)
        JFCMainClient.initializeUI(setVisibleFlg, downloadDir);
    else {
        if (!JFCMainClient.saargs[0].equals("--help")) {
            JFCMainClient.saargs[0] = "--help";
            parseargs(JFCMainClient.saargs);
        }
    }
}