Example usage for java.beans PropertyChangeListener PropertyChangeListener

List of usage examples for java.beans PropertyChangeListener PropertyChangeListener

Introduction

In this page you can find the example usage for java.beans PropertyChangeListener PropertyChangeListener.

Prototype

PropertyChangeListener

Source Link

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Property Split");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

    splitPane.setTopComponent(new JLabel("www.java2s.com"));

    splitPane.setBottomComponent(new JLabel("www.java2s.com"));

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent changeEvent) {
            JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
            String propertyName = changeEvent.getPropertyName();
            if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
                int current = sourceSplitPane.getDividerLocation();
                System.out.println("Current: " + current);
                Integer last = (Integer) changeEvent.getNewValue();
                System.out.println("Last: " + last);
                Integer priorLast = (Integer) changeEvent.getOldValue();
                System.out.println("Prior last: " + priorLast);
            }//  www. j  a  v  a2s .co m
        }
    };

    // Attach listener
    splitPane.addPropertyChangeListener(propertyChangeListener);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:ActiveTray.java

public static void main(String args[]) throws Exception {
    if (SystemTray.isSupported() == false) {
        System.err.println("No system tray available");
        return;// w w w .j ava2 s.c o  m
    }
    final SystemTray tray = SystemTray.getSystemTray();
    PropertyChangeListener propListener = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            TrayIcon oldTray[] = (TrayIcon[]) evt.getOldValue();
            TrayIcon newTray[] = (TrayIcon[]) evt.getNewValue();
            System.out.println(oldTray.length + " / " + newTray.length);
        }
    };
    tray.addPropertyChangeListener("trayIcons", propListener);
    Image image = Toolkit.getDefaultToolkit().getImage("jpgIcon.jpg");
    PopupMenu popup = new PopupMenu();
    MenuItem item = new MenuItem("Hello, World");
    final TrayIcon trayIcon = new TrayIcon(image, "Tip Text", popup);
    ActionListener menuActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            trayIcon.displayMessage("Good-bye", "Cruel World", TrayIcon.MessageType.WARNING);
        }
    };
    item.addActionListener(menuActionListener);
    popup.add(item);
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tray.remove(trayIcon);
        }
    };
    trayIcon.addActionListener(actionListener);
    tray.add(trayIcon);
}

From source file:Main.java

public static void main(String[] args) {
    final JProgressBar progressBar = new JProgressBar(0, 10);

    final CounterTask task = new CounterTask();
    task.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equals(evt.getPropertyName())) {
                progressBar.setValue((Integer) evt.getNewValue());
            }//www  .  j av a2s.  c  o m
        }
    });
    JButton startButton = new JButton("Start");
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.execute();
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.cancel(true);
        }
    });

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);
    buttonPanel.add(cancelButton);

    JPanel cp = new JPanel();
    LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS);
    cp.setLayout(layout);
    cp.add(buttonPanel);
    cp.add(progressBar);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextArea textArea = new JTextArea(10, 20);
    final JProgressBar progressBar = new JProgressBar(0, 10);

    final CounterTask task = new CounterTask();
    task.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equals(evt.getPropertyName())) {
                progressBar.setValue((Integer) evt.getNewValue());
            }//  w w w.ja v a2 s .co m
        }
    });
    JButton startButton = new JButton("Start");
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.execute();
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            task.cancel(true);
        }
    });

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);
    buttonPanel.add(cancelButton);

    JPanel cp = new JPanel();
    LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS);
    cp.setLayout(layout);
    cp.add(buttonPanel);
    cp.add(new JScrollPane(textArea));
    cp.add(progressBar);

    JFrame frame = new JFrame("SwingWorker Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Color TRUE_COLOR = new Color(180, 200, 255);
    Color FALSE_COLOR = new Color(255, 100, 100);

    final MyBean panel = new MyBean();
    panel.setTitle(true);//  www  .ja v  a 2  s  .c  o m
    panel.setBackground(TRUE_COLOR);
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            panel.setTitle(!panel.getTitle());
        }
    });
    panel.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(MyBean.TITLE_PROP_NAME)) {
                panel.setBackground(panel.getTitle() ? TRUE_COLOR : FALSE_COLOR);
            }
        }
    });
    JFrame frame = new JFrame();
    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);

    frame.setVisible(true);

}

From source file:PropertySplitPane.java

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

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);

    JComponent topComponent = new JButton("A");
    splitPane.setTopComponent(topComponent);

    JComponent bottomComponent = new JButton("B");
    splitPane.setBottomComponent(bottomComponent);

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent changeEvent) {
            JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
            String propertyName = changeEvent.getPropertyName();
            if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
                int current = sourceSplitPane.getDividerLocation();
                System.out.println("Current: " + current);
                Integer last = (Integer) changeEvent.getNewValue();
                System.out.println("Last: " + last);
                Integer priorLast = (Integer) changeEvent.getOldValue();
                System.out.println("Prior last: " + priorLast);
            }/*  w  w w . ja  v a 2  s  . c  o m*/
        }
    };

    splitPane.addPropertyChangeListener(propertyChangeListener);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int ROWS = 100;
    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.add(new JLabel("Thanks for helping out. Use tab to move around."));
    for (int i = 0; i < ROWS; i++) {
        JTextField field = new JTextField("" + i);
        field.setName("field#" + i);
        content.add(field);//  w  w  w  .  j  a  v a 2s .c o  m
    }
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner",
            new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (!(evt.getNewValue() instanceof JComponent)) {
                        return;
                    }
                    JViewport viewport = (JViewport) content.getParent();
                    JComponent focused = (JComponent) evt.getNewValue();
                    if (content.isAncestorOf(focused)) {
                        Rectangle rect = focused.getBounds();
                        Rectangle r2 = viewport.getVisibleRect();
                        content.scrollRectToVisible(
                                new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
                    }
                }
            });
    JFrame window = new JFrame();
    window.setContentPane(new JScrollPane(content));
    window.setSize(200, 200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyTreeNode root = new MyTreeNode(1, 0);
    DefaultTreeModel model = new DefaultTreeModel(root);
    JProgressBar bar = new JProgressBar();
    PropertyChangeListener progressListener = new PropertyChangeListener() {
        @Override//www.  j  av  a  2  s .c o m
        public void propertyChange(PropertyChangeEvent evt) {
            bar.setValue((Integer) evt.getNewValue());
        }
    };
    JTree tree = new JTree();
    tree.setShowsRootHandles(true);
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
            TreePath path = event.getPath();
            if (path.getLastPathComponent() instanceof MyTreeNode) {
                MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
                node.loadChildren(model, progressListener);
            }
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {

        }
    });
    tree.setModel(model);
    root.loadChildren(model, progressListener);
    frame.add(new JScrollPane(tree));
    frame.add(bar, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button1 = new JButton("Select Me");
    final JButton button2 = new JButton("No Select Me");
    final Random random = new Random();

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton button = (JButton) actionEvent.getSource();
            int red = random.nextInt(255);
            int green = random.nextInt(255);
            int blue = random.nextInt(255);
            button.setBackground(new Color(red, green, blue));
        }//  w  w w.  j  av  a2s  .co  m
    };

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
            String property = propertyChangeEvent.getPropertyName();
            if ("background".equals(property)) {
                button2.setBackground((Color) propertyChangeEvent.getNewValue());
            }
        }
    };

    button1.addActionListener(actionListener);
    button1.addPropertyChangeListener(propertyChangeListener);
    button2.addActionListener(actionListener);

    frame.add(button1, BorderLayout.NORTH);
    frame.add(button2, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:ShowHTMLViews.java

public static void main(String[] args) {
    try {//from  w  w  w .ja  va 2s  .c  o  m
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    try {
        JFrame f = new JFrame("HTML Document View Structure");
        final JEditorPane ep = new JEditorPane(args[0]);
        ep.setEditable(false);
        f.getContentPane().add(new JScrollPane(ep));
        f.setSize(400, 300);
        f.setVisible(true);

        ep.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("page")) {
                    System.out.println("Document:\n");
                    HTMLDocDisplay.displayModel(ep, System.out);
                    System.out.println("\n\nViews:\n");
                    HTMLViewDisplayer.displayViews(ep, System.out);
                }
            }
        });
    } catch (Exception e) {
        System.out.println(e);
        System.exit(1);
    }
}