List of usage examples for java.beans PropertyChangeEvent getNewValue
public Object getNewValue()
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);//from w w w . jav 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) { final JFrame frame = new JFrame("Test"); KeyboardFocusManager.getCurrentKeyboardFocusManager().addVetoableChangeListener("focusedWindow", new VetoableChangeListener() { private boolean gained = false; @Override//from w w w.j a va 2 s. c o m public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { if (evt.getNewValue() == frame) { gained = true; } if (gained && evt.getNewValue() != frame) { frame.dispose(); } } }); frame.add(new JTextField(10), BorderLayout.NORTH); frame.add(new JTextField(10), BorderLayout.SOUTH); frame.pack(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 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;//from ww w . j ava 2 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) 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/*from ww w . jav 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: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")); }// w ww.j a va 2 s.c om } } }); 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 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()); }// w w w . java2 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(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()); }/*from w w w .j a v a 2 s . 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(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[] argv) throws Exception { JToolBar toolbar = new JToolBar(); toolbar.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { String propName = evt.getPropertyName(); if ("orientation".equals(propName)) { Integer oldValue = (Integer) evt.getOldValue(); Integer newValue = (Integer) evt.getNewValue(); if (newValue.intValue() == JToolBar.HORIZONTAL) { System.out.println("horizontal orientation"); } else { System.out.println("vertical orientation"); }/*from w w w . jav a 2 s . c o m*/ } } }); }
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)); }//from www . j a v a 2s . c o 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: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)); }/*from w ww .j a va 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); }