Example usage for javax.swing.event ListDataListener ListDataListener

List of usage examples for javax.swing.event ListDataListener ListDataListener

Introduction

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

Prototype

ListDataListener

Source Link

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);/* w w w  .j a  v a  2s.  com*/
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

    ListDataListener listDataListener = new ListDataListener() {
        public void contentsChanged(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalAdded(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalRemoved(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        private void appendEvent(ListDataEvent listDataEvent) {
            switch (listDataEvent.getType()) {
            case ListDataEvent.CONTENTS_CHANGED:
                System.out.println("Type: Contents Changed");
                break;
            case ListDataEvent.INTERVAL_ADDED:
                System.out.println("Type: Interval Added");
                break;
            case ListDataEvent.INTERVAL_REMOVED:
                System.out.println("Type: Interval Removed");
                break;
            }
            System.out.println(", Index0: " + listDataEvent.getIndex0());
            System.out.println(", Index1: " + listDataEvent.getIndex1());
            DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
            System.out.println(theModel);
        }
    };

    model.addListDataListener(listDataListener);

    JButton jb = new JButton("add F");

    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.add(0, "First");
        }
    });

    frame.add(jb, BorderLayout.SOUTH);

    frame.setSize(640, 300);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);/*  w  ww  . ja  v  a  2  s. c o  m*/
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.WEST);

    ListDataListener listDataListener = new ListDataListener() {
        public void contentsChanged(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalAdded(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalRemoved(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        private void appendEvent(ListDataEvent listDataEvent) {
            switch (listDataEvent.getType()) {
            case ListDataEvent.CONTENTS_CHANGED:
                System.out.println("Type: Contents Changed");
                break;
            case ListDataEvent.INTERVAL_ADDED:
                System.out.println("Type: Interval Added");
                break;
            case ListDataEvent.INTERVAL_REMOVED:
                System.out.println("Type: Interval Removed");
                break;
            }
            System.out.println(", Index0: " + listDataEvent.getIndex0());
            System.out.println(", Index1: " + listDataEvent.getIndex1());
            DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
            System.out.println(theModel);
        }
    };

    model.addListDataListener(listDataListener);

    model.add(0, "First");
    model.addElement("Last");
    int size = model.getSize();
    model.insertElementAt("Middle", size / 2);
    size = model.getSize();
    if (size != 0)
        model.set(0, "New First");
    size = model.getSize();
    if (size != 0)
        model.setElementAt("New Last", size - 1);
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);
    }
    model.clear();
    size = model.getSize();
    if (size != 0)
        model.remove(0);

    model.removeAllElements();
    model.removeElement("Last");
    size = model.getSize();
    if (size != 0)
        model.removeElementAt(size / 2);
    size = model.getSize();
    if (size != 0)
        model.removeRange(0, size / 2);
    frame.setSize(640, 300);
    frame.setVisible(true);
}

From source file:ModifyModelSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);/*from  w ww  . j  av a2  s  .  com*/
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    contentPane.add(scrollPane1, BorderLayout.WEST);

    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    JScrollPane scrollPane2 = new JScrollPane(textArea);
    contentPane.add(scrollPane2, BorderLayout.CENTER);

    ListDataListener listDataListener = new ListDataListener() {
        public void contentsChanged(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalAdded(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalRemoved(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        private void appendEvent(ListDataEvent listDataEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            switch (listDataEvent.getType()) {
            case ListDataEvent.CONTENTS_CHANGED:
                pw.print("Type: Contents Changed");
                break;
            case ListDataEvent.INTERVAL_ADDED:
                pw.print("Type: Interval Added");
                break;
            case ListDataEvent.INTERVAL_REMOVED:
                pw.print("Type: Interval Removed");
                break;
            }
            pw.print(", Index0: " + listDataEvent.getIndex0());
            pw.print(", Index1: " + listDataEvent.getIndex1());
            DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
            Enumeration elements = theModel.elements();
            pw.print(", Elements: ");
            while (elements.hasMoreElements()) {
                pw.print(elements.nextElement());
                pw.print(",");
            }
            pw.println();
            textArea.append(sw.toString());
        }
    };

    model.addListDataListener(listDataListener);

    // Setup buttons
    JPanel jp = new JPanel(new GridLayout(2, 1));
    JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
    JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
    jp.add(jp1);
    jp.add(jp2);
    JButton jb = new JButton("add F");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.add(0, "First");
        }
    });
    jb = new JButton("addElement L");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("Last");
        }
    });
    jb = new JButton("insertElementAt M");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            model.insertElementAt("Middle", size / 2);
        }
    });
    jb = new JButton("set F");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.set(0, "New First");
        }
    });
    jb = new JButton("setElementAt L");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.setElementAt("New Last", size - 1);
        }
    });
    jb = new JButton("load 10");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            for (int i = 0, n = labels.length; i < n; i++) {
                model.addElement(labels[i]);
            }
        }
    });
    jb = new JButton("clear");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.clear();
        }
    });
    jb = new JButton("remove F");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.remove(0);
        }
    });
    jb = new JButton("removeAllElements");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.removeAllElements();
        }
    });
    jb = new JButton("removeElement 'Last'");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.removeElement("Last");
        }
    });
    jb = new JButton("removeElementAt M");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.removeElementAt(size / 2);
        }
    });
    jb = new JButton("removeRange FM");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.removeRange(0, size / 2);
        }
    });
    contentPane.add(jp, BorderLayout.SOUTH);
    frame.setSize(640, 300);
    frame.setVisible(true);
}

From source file:Main.java

public SortedListModel() {
    model = new TreeSet<Object>();
    ListDataListener lis = new ListDataListener() {
        @Override/*w  w  w.  ja  v  a 2s .  co  m*/
        public void contentsChanged(ListDataEvent arg0) {
        }

        @Override
        public void intervalAdded(ListDataEvent arg0) {
        }

        @Override
        public void intervalRemoved(ListDataEvent arg0) {
        }
    };
    super.addListDataListener(lis);
    super.removeListDataListener(lis);
}

From source file:Main.java

public SortedListModel() {
    model = new TreeSet<Object>();
    ListDataListener lis = new ListDataListener() {
        @Override/*from  w w  w.java  2 s  .c  o  m*/
        public void contentsChanged(ListDataEvent arg0) {
        }

        @Override
        public void intervalAdded(ListDataEvent arg0) {
        }

        @Override
        public void intervalRemoved(ListDataEvent arg0) {
        }
    };
    super.addListDataListener(lis);
    ListDataListener[] ldls = getListDataListeners();
    super.removeListDataListener(lis);
}

From source file:Main.java

public SortedListModel() {
    model = new TreeSet<Object>();
    ListDataListener lis = new ListDataListener() {
        @Override/*  w ww  .ja  v a  2 s  .co  m*/
        public void contentsChanged(ListDataEvent arg0) {
        }

        @Override
        public void intervalAdded(ListDataEvent arg0) {
        }

        @Override
        public void intervalRemoved(ListDataEvent arg0) {
        }
    };
    super.addListDataListener(lis);
    ListDataListener[] ldls = (ListDataListener[]) (getListeners(ListDataListener.class));
    super.removeListDataListener(lis);
}

From source file:com.anrisoftware.prefdialog.fields.buttongroup.ButtonsRowPanel.java

/**
 * Create the {@link JPanel} and sets a one-row {@link TableLayout}.
 *///from  w  w  w.  j av  a 2  s  .  c o m
ButtonsRowPanel() {
    this.name = "";
    this.buttons = new ArrayList<JButton>();
    this.actionsListener = new ListDataListener() {

        @Override
        public void intervalRemoved(ListDataEvent e) {
            removeInterval(e.getIndex0(), e.getIndex1());
        }

        @Override
        public void intervalAdded(ListDataEvent e) {
            addInterval(e.getIndex0(), e.getIndex1());
        }

        @Override
        public void contentsChanged(ListDataEvent e) {
            replaceInterval(e.getIndex0(), e.getIndex1());
        }
    };
    setup();
}

From source file:com.anrisoftware.prefdialog.fields.historycombobox.HistoryComboBoxField.java

/**
 * @see ComboBoxFieldFactory#create(Object, String)
 *///from  w w  w . j  av a2  s .c  om
@AssistedInject
HistoryComboBoxField(HistoryComboBoxFactory historyComboBoxFactory, @Assisted Object parentObject,
        @Assisted String fieldName) {
    super(ANNOTATION_CLASS, new JComboBox<Object>(), parentObject, fieldName);
    this.boxFactory = historyComboBoxFactory;
    this.history = new ArrayList<Object>();
    this.defaultItems = new ArrayList<Object>();
    this.historyListener = new ListDataListener() {

        @Override
        public void intervalRemoved(ListDataEvent e) {
            removeHistory(e.getIndex0(), e.getIndex1());
        }

        @Override
        public void intervalAdded(ListDataEvent e) {
            insertHistory(e.getIndex0(), e.getIndex1());
        }

        @Override
        public void contentsChanged(ListDataEvent e) {
            replaceHistory(e.getIndex0(), e.getIndex1());
        }
    };
}

From source file:hermes.browser.dialog.HermesAdminFactoryConfigPanel.java

public void init() {
    final Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED);
    setLayout(new BorderLayout());
    setBorder(BorderFactory.createTitledBorder(border, ADMIN_FACTORY));

    afCombo.setModel(cfComboModel);//from   w w  w . ja  v a2s  .c o  m

    propertyTableSP.setViewportView(propertyTable);
    propertyTable.setSortable(true);
    add(afCombo, BorderLayout.NORTH);
    add(propertyTableSP, BorderLayout.CENTER);

    popupMenu.add(addItem);
    popupMenu.add(removeItem);

    addItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                propertyTableModel.insertRow();
            } catch (Exception ex) {
                cat.error(ex.getMessage(), ex);
            }
        }
    });

    removeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (propertyTable.getSelectedRow() != -1) {
                propertyTableModel.removeRow(propertyTable.getSelectedRow());
            }
        }
    });

    final MouseAdapter m = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isRightMouseButton(e)) {
                if (e.getComponent() == propertyTableSP) {
                    removeItem.setEnabled(false);
                } else {
                    removeItem.setEnabled(true);
                }

                if (propertySelectionComboBox.getModel().getSize() == 0) {
                    addItem.setEnabled(false);
                } else {
                    addItem.setEnabled(true);
                }

                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    };

    propertyTableSP.addMouseListener(m);
    propertyTable.addMouseListener(m);
    propertyTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

    cfComboModel.addListDataListener(new ListDataListener() {
        public void contentsChanged(ListDataEvent arg0) {
            final String className = HermesBrowser.getConfigDAO()
                    .getAdminClassForPlugIn((String) cfComboModel.getSelectedItem());
            final ClassLoaderManager classLoaderManager = (ClassLoaderManager) SingletonManager
                    .get(ClassLoaderManager.class);

            try {
                if (propertyTableModel != null) {
                    final ClassLoader classLoader = classLoaderManager
                            .getClassLoader(dialog.getSelectedLoader());
                    Thread.currentThread().setContextClassLoader(classLoader);

                    bean = (HermesAdminFactory) classLoader.loadClass(className).newInstance();
                    propertyTableModel.setBean(bean);
                    updateCellEditor();
                    dialog.setDirty();
                }
            } catch (Throwable e) {
                HermesBrowser.getBrowser().showErrorDialog(
                        "Unable to locate this plugin.\nSelect the loader the JMS provider classes are in before choosing the plugin.");
                cfComboModel.setSelectedItem(ConfigDAO.DEFAULT_PLUGIN);
            }
        }

        public void intervalAdded(ListDataEvent arg0) {
            // NOP
        }

        public void intervalRemoved(ListDataEvent arg0) {
            // NOP
        }
    });

}

From source file:com.bwc.ora.views.LrpDisplayFrame.java

private LrpDisplayFrame() {
    //set up frame and chart
    //configure initial display setting for the panel
    chartPanel = new ChartPanel(null);
    chartPanel.setFillZoomRectangle(true);
    chartPanel.setMouseWheelEnabled(true);
    this.setSize(800, 800);
    add(chartPanel, BorderLayout.CENTER);

    //make it so this jframe can't gain focus
    setFocusableWindowState(false);//from ww w  .  ja  v  a2  s  .c  o m

    //add listener to check to see which LRP should be displayed
    lrps.addListSelectionListener((ListSelectionEvent e) -> {
        if (!e.getValueIsAdjusting()) {
            if (lrps.getSelectedIndex() > -1) {
                setChart(lrps.getSelectedValue().getAllSeriesData());
                setVisible(true);
            } else {
                setVisible(false);
            }
        }
    });

    //add listener to check for updates to lrp selection to change lrp
    lrpSettings.addPropertyChangeListener(e -> {
        if (lrps.getSelectedIndex() > -1) {
            if (e.getPropertyName().equals(LrpSettings.PROP_LRP_WIDTH)) {
                Lrp newLrp;
                try {
                    newLrp = new Lrp(Collections.getInstance().getLrpCollection().getSelectedValue().getName(),
                            Collections.getInstance().getLrpCollection().getSelectedValue()
                                    .getLrpCenterXPosition(),
                            Collections.getInstance().getLrpCollection().getSelectedValue()
                                    .getLrpCenterYPosition(),
                            ModelsCollection.getInstance().getLrpSettings().getLrpWidth(),
                            ModelsCollection.getInstance().getLrpSettings().getLrpHeight(),
                            Collections.getInstance().getLrpCollection().getSelectedValue().getType());
                } catch (LRPBoundaryViolationException e1) {
                    JOptionPane.showMessageDialog(null, e1.getMessage() + " Try again.", "LRP generation error",
                            JOptionPane.ERROR_MESSAGE);
                    lrpSettings.setLrpWidth((int) e.getOldValue());
                    return;
                }
                Collections.getInstance().getLrpCollection().setLrp(newLrp,
                        Collections.getInstance().getLrpCollection().getSelectedIndex());
            } else {
                updateSeries(lrps.getSelectedValue().getAllSeriesData());
            }
        }
    });

    //add listener to check for updates to oct settings to change lrp
    octSettings.addPropertyChangeListener(e -> {
        if (lrps.getSelectedIndex() > -1) {
            switch (e.getPropertyName()) {
            case OctSettings.PROP_APPLY_CONTRAST_ADJUSTMENT:
            case OctSettings.PROP_APPLY_NOISE_REDUCTION:
            case OctSettings.PROP_DISPLAY_LOG_OCT:
            case OctSettings.PROP_SHARPEN_KERNEL_RADIUS:
            case OctSettings.PROP_SHARPEN_WEIGHT:
            case OctSettings.PROP_SMOOTHING_FACTOR:
                updateSeries(lrps.getSelectedValue().getAllSeriesData());
            default:
                break;
            }
        }
    });

    //add listener to see if the LRP display needs updating when a selection on oct changes
    lrps.addListDataChangeListener(new ListDataListener() {
        @Override
        public void intervalAdded(ListDataEvent e) {
        }

        @Override
        public void intervalRemoved(ListDataEvent e) {
        }

        @Override
        public void contentsChanged(ListDataEvent e) {
            clearAnnotations();
            updateSeries(lrps.getSelectedValue().getAllSeriesData());
        }
    });

    //add mouse listener for chart to detect when to display labels for
    //peaks in the pop-up menu, also specify what to do when label is clicked
    chartPanel.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseClicked(ChartMouseEvent cme) {
            ChartEntity entity = cme.getEntity();
            if (entity instanceof XYItemEntity && cme.getTrigger().getButton() == MouseEvent.BUTTON1) {

                XYItemEntity item = (XYItemEntity) entity;
                LabelPopupMenu labelMenu = new LabelPopupMenu(chartPanel, item, lrps.getSelectedValue());

                labelMenu.show(chartPanel, cme.getTrigger().getX(), cme.getTrigger().getY());
            }
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent cme) {
            //do nothing
        }

    });
}