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:PropertyChangeListenerDemo.java

public static void main(String args[]) {
    Runnable runner = new Runnable() {
        public void run() {
            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();
            // Define ActionListener
            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));
                }/*www  . jav a  2s  .  c  o  m*/
            };
            // Define PropertyChangeListener
            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);
        }
    };
    EventQueue.invokeLater(runner);
}

From source file:org.jcurl.demo.zui.BroomPromptDemo.java

public static void main(final String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            final JFrame application = new JFrame();
            application.setTitle("JCurl BroomPrompt");
            application.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            final PCanvas pc = new PCanvas();
            pc.setAnimatingRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);
            pc.setInteractingRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);
            // pc.getRoot().getDefaultInputManager().setKeyboardFocus(
            // new KeyboardZoom(pc.getCamera()));
            pc.setBackground(new Color(0xE8E8FF));

            final PNode ice = new PIceFactory.Fancy().newInstance();
            pc.getLayer().addChild(ice);
            application.getContentPane().add(pc);
            application.setSize(500, 800);
            application.setVisible(true);
            animateToBounds(pc.getCamera(), twelveP, 500);
            final BroomPromptSimple bp;
            ice.addChild(bp = new BroomPromptSimple());
            final BroomPromptModel bpm;
            bp.setModel(bpm = new DefaultBroomPromptModel());
            bpm.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(final PropertyChangeEvent evt) {
                    // FIXME why doesn't fire this?
                    log.info(evt);/*  ww  w  .j  av  a 2  s  . c o  m*/
                }
            });
            bpm.getSplitTimeMillis().addChangeListener(new ChangeListener() {
                public void stateChanged(final ChangeEvent e) {
                    log.info(e);
                }
            });
            bpm.setIdx16(1);
            bpm.setOutTurn(false);
            bp.animateToPositionScaleRotation(1, 2, 1, -0.1 * Math.PI, 5000);
        }
    });
}

From source file:org.jdesktop.http.async.JsonHttpRequest.java

public static void main(String[] args) {
    final JsonHttpRequest req = new JsonHttpRequest();
    req.addReadyStateChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getNewValue() == ReadyState.LOADED) {
                //                    JSONObject json = req.getResponseJSON();
                //                    json = json.optJSONObject("ResultSet");
                //                    System.out.println("Total Results Avail.: " + json.optString("totalResultsAvailable"));
                //                    System.out.println("Results: ");
                //                    JSONArray results = json.optJSONArray("Result");
                //                    for (int i=0; i<results.length(); i++) {
                //                        System.out.println("\tTitle: " + results.optJSONObject(i).optString("Title"));
                //                    }

                Map map = (Map) req.getResponseMap().get("ResultSet");
                System.out.println("Total Results Avail.: " + map.get("totalResultsAvailable"));
                System.out.println("Results: ");
                Object[] results2 = (Object[]) map.get("Result");
                for (int i = 0; i < results2.length; i++) {
                    Map m = (Map) results2[i];
                    System.out.println("\tTitle: " + m.get("Title"));
                }/* www.j a  v a2s .c  o m*/
            }
        }
    });
    try {
        req.open(Method.GET,
                "http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=JavaOne&output=json");
        req.send();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void linkEnabled(JComponent watcher, final JComponent... actor) {
    watcher.addPropertyChangeListener(new PropertyChangeListener() {
        @Override/*from w  w w .j  av a 2  s.c om*/
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("enabled")) {
                Boolean newValue = (Boolean) evt.getNewValue();
                if (newValue) {
                    for (JComponent j : actor) {
                        j.setEnabled(true);
                    }
                } else {
                    for (JComponent j : actor) {
                        j.setEnabled(false);
                    }
                }
            }
        }
    });
}

From source file:Main.java

/**
 * Forces slave menu item to reflect master menu item using a property change
 * listener./* w  w w  . ja  va2  s .c  o m*/
 */
protected static void linkMenuItem(final JMenuItem master, final JMenuItem slave) {
    final JMenuItem source = master, dest = slave;
    source.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(final PropertyChangeEvent e) {
            syncMenuItem(source, dest);
        }
    });
}

From source file:Main.java

public ShowAction() {
    super("About");
    PropertyChangeListener lis = new PropertyChangeListener() {
        @Override//from  www. ja v  a2s. c  o  m
        public void propertyChange(PropertyChangeEvent e) {
            System.out.println(e);
        }
    };
    addPropertyChangeListener(lis);

    putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
    putValue(Action.NAME, "Go to number ");
    putValue(Action.LONG_DESCRIPTION, "Change the number to ");

    removePropertyChangeListener(lis);

}

From source file:Main.java

public TwoRoot() {
    setLayout(new GridLayout(0, 1));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(progressBar);//from   w  ww .  j  a  va2 s. co m
    add(label);
    setSize(300, 100);
    setVisible(true);

    progressBar.setIndeterminate(true);
    TwoWorker task = new TwoWorker();
    task.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            if ("progress".equals(e.getPropertyName())) {
                progressBar.setIndeterminate(false);
                progressBar.setValue((Integer) e.getNewValue());
            }
        }
    });
    task.execute();
}

From source file:Main.java

public BackgroundWorker(JTextArea ta, JProgressBar pb) {
    this.pb = pb;
    this.ta = ta;
    addPropertyChangeListener(new PropertyChangeListener() {
        @Override//www .  j a  v a  2 s .co  m
        public void propertyChange(PropertyChangeEvent evt) {
            if ("progress".equalsIgnoreCase(evt.getPropertyName())) {
                BackgroundWorker.this.pb.setValue(getProgress());
            }
        }

    });
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    JProgressBar progressBar = new JProgressBar(0, 100);
    progressBar.setValue(0);/*from w w  w .j  a va 2s. com*/
    progressBar.setStringPainted(true);

    taskOutput = new JTextArea(5, 20);
    taskOutput.setEditable(false);

    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            startButton.setEnabled(false);
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

            final Task task = new Task();
            task.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent pce) {
                    if ("progress".equals(pce.getPropertyName())) {
                        int progress = (Integer) pce.getNewValue();
                        progressBar.setValue(progress);
                        taskOutput.append(String.format("Completed %d%% of task.\n", task.getProgress()));
                    }
                }
            });
            task.execute();
        }
    });

    JPanel panel = new JPanel();
    panel.add(startButton);
    panel.add(progressBar);

    add(panel, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);

}

From source file:Main.java

public TestPane() {
    add(pbProgress);/* ww w . j  a  v a 2 s.com*/
    start = new JButton("Start");
    add(start);
    start.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            start.setEnabled(false);
            ProgressWorker pw = new ProgressWorker();
            pw.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    String name = evt.getPropertyName();
                    if (name.equals("progress")) {
                        int progress = (int) evt.getNewValue();
                        pbProgress.setValue(progress);
                        repaint();
                    } else if (name.equals("state")) {
                        SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
                        switch (state) {
                        case DONE:
                            start.setEnabled(true);
                            break;
                        }
                    }
                }
            });
            pw.execute();
        }
    });
}