List of usage examples for javax.swing.event ChangeListener ChangeListener
ChangeListener
From source file:MainClass.java
public static void main(String[] a) { final String DESELECTED_LABEL = "Deselected"; final String SELECTED_LABEL = "Selected"; JFrame frame = new JFrame("Selecting CheckBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL); abstractButton.setText(newLabel); }//from ww w .j a va2 s . c o m }; ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { AbstractButton abstractButton = (AbstractButton) changeEvent.getSource(); ButtonModel buttonModel = abstractButton.getModel(); boolean armed = buttonModel.isArmed(); boolean pressed = buttonModel.isPressed(); boolean selected = buttonModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { AbstractButton abstractButton = (AbstractButton) itemEvent.getSource(); Color foreground = abstractButton.getForeground(); Color background = abstractButton.getBackground(); int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { abstractButton.setForeground(background); abstractButton.setBackground(foreground); } } }; checkBox.addActionListener(actionListener); checkBox.addChangeListener(changeListener); checkBox.addItemListener(itemListener); checkBox.setMnemonic(KeyEvent.VK_S); frame.add(checkBox, BorderLayout.NORTH); frame.setSize(300, 100); frame.setVisible(true); }
From source file:SelectingCheckBox.java
public static void main(String args[]) { JFrame frame = new JFrame("Selecting CheckBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL); // Define ActionListener ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL); abstractButton.setText(newLabel); }// www. j a va2s .co m }; ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { AbstractButton abstractButton = (AbstractButton) changeEvent.getSource(); ButtonModel buttonModel = abstractButton.getModel(); boolean armed = buttonModel.isArmed(); boolean pressed = buttonModel.isPressed(); boolean selected = buttonModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { AbstractButton abstractButton = (AbstractButton) itemEvent.getSource(); Color foreground = abstractButton.getForeground(); Color background = abstractButton.getBackground(); int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { abstractButton.setForeground(background); abstractButton.setBackground(foreground); } } }; checkBox.addActionListener(actionListener); checkBox.addChangeListener(changeListener); checkBox.addItemListener(itemListener); checkBox.setMnemonic(KeyEvent.VK_S); Container contentPane = frame.getContentPane(); contentPane.add(checkBox, BorderLayout.NORTH); frame.setSize(300, 100); frame.setVisible(true); }
From source file:TabbedPaneSample.java
public static void main(String args[]) { String title = (args.length == 0 ? "Tabbed Pane Sample" : args[0]); JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" }; for (int i = 0, n = titles.length; i < n; i++) { add(tabbedPane, titles[i]);/* w ww . java 2 s . co m*/ } ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource(); int index = sourceTabbedPane.getSelectedIndex(); System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index)); } }; tabbedPane.addChangeListener(changeListener); Container contentPane = frame.getContentPane(); contentPane.add(tabbedPane, BorderLayout.CENTER); frame.setSize(400, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel main = new JPanel(new GridLayout(2, 1)); JPanel scrollBarPanel = new JPanel(); final JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 48, 0, 255); int height = scrollBar.getPreferredSize().height; scrollBar.setPreferredSize(new Dimension(175, height)); scrollBarPanel.add(scrollBar);/*from w ww.j a v a2s . c o m*/ main.add(scrollBarPanel); JPanel sliderPanel = new JPanel(); final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 255, 128); slider.setMajorTickSpacing(48); slider.setMinorTickSpacing(16); slider.setPaintTicks(true); sliderPanel.add(slider); main.add(sliderPanel); frame.add(main, BorderLayout.CENTER); scrollBar.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println("JScrollBar's current value = " + scrollBar.getValue()); } }); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { System.out.println("JSlider's current value = " + slider.getValue()); } }); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
From source file:GroupActionRadio.java
public static void main(String args[]) { String title = (args.length == 0 ? "Grouping Example" : args[0]); JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Slice Parts ActionListener sliceActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); System.out.println("Selected: " + aButton.getText()); }//from w w w. j ava 2 s .c o m }; Container sliceContainer = RadioButtonUtils.createRadioButtonGrouping(sliceOptions, "Slice Count", sliceActionListener); // Crust Parts ActionListener crustActionListener = new ActionListener() { String lastSelected; public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); String label = aButton.getText(); String msgStart; if (label.equals(lastSelected)) { msgStart = "Reselected: "; } else { msgStart = "Selected: "; } lastSelected = label; System.out.println(msgStart + label); } }; ItemListener itemListener = new ItemListener() { String lastSelected; public void itemStateChanged(ItemEvent itemEvent) { AbstractButton aButton = (AbstractButton) itemEvent.getSource(); int state = itemEvent.getStateChange(); String label = aButton.getText(); String msgStart; if (state == ItemEvent.SELECTED) { if (label.equals(lastSelected)) { msgStart = "Reselected -> "; } else { msgStart = "Selected -> "; } lastSelected = label; } else { msgStart = "Deselected -> "; } System.out.println(msgStart + label); } }; ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changEvent) { AbstractButton aButton = (AbstractButton) changEvent.getSource(); ButtonModel aModel = aButton.getModel(); boolean armed = aModel.isArmed(); boolean pressed = aModel.isPressed(); boolean selected = aModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; final Container crustContainer = RadioButtonUtils.createRadioButtonGrouping(crustOptions, "Crust Type", crustActionListener, itemListener, changeListener); // Button Parts ActionListener buttonActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Enumeration selected = RadioButtonUtils.getSelectedElements(crustContainer); while (selected.hasMoreElements()) { System.out.println("Selected -> " + selected.nextElement()); } } }; JButton button = new JButton("Order Pizza"); button.addActionListener(buttonActionListener); Container contentPane = frame.getContentPane(); contentPane.add(sliceContainer, BorderLayout.WEST); contentPane.add(crustContainer, BorderLayout.EAST); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:JSliderOnJOptionPane.java
static JSlider getSlider(final JOptionPane optionPane) { JSlider slider = new JSlider(); slider.setMajorTickSpacing(10);/*from w w w.ja v a2s. c o m*/ slider.setPaintTicks(true); slider.setPaintLabels(true); ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { JSlider theSlider = (JSlider) changeEvent.getSource(); if (!theSlider.getValueIsAdjusting()) { optionPane.setInputValue(new Integer(theSlider.getValue())); } } }; slider.addChangeListener(changeListener); return slider; }
From source file:Main.java
public static void synchronizeView(final JViewport masterViewport, final JViewport slaveViewport, final int orientation) { final ChangeListener c1 = new ChangeListener() { public void stateChanged(ChangeEvent e) { if (masterViewport.getView() == null || slaveViewport.getView() == null) { return; }//w ww . jav a 2 s .c o m if (orientation == SwingConstants.HORIZONTAL) { Point v1 = masterViewport.getViewPosition(); Point v2 = slaveViewport.getViewPosition(); if (v1.x != v2.x) { slaveViewport.setViewPosition(new Point(v1.x, v2.y)); } } else if (orientation == SwingConstants.VERTICAL) { Point v1 = masterViewport.getViewPosition(); Point v2 = slaveViewport.getViewPosition(); if (v1.y != v2.y) { slaveViewport.setViewPosition(new Point(v2.x, v1.y)); } } } }; masterViewport.addChangeListener(c1); }
From source file:Main.java
public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JSlider slider = new JSlider(); BoundedRangeModel model = slider.getModel(); model.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { BoundedRangeModel m = (BoundedRangeModel) e.getSource(); System.out.println("Slider position changed to " + m.getValue()); }/*from w w w. j av a 2 s . c om*/ }); getContentPane().add(slider); pack(); }
From source file:Main.java
public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); model.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { System.out.println("Armed: " + model.isArmed() + " Enabled: " + model.isEnabled() + " Pressed: " + model.isPressed()); }/* ww w . j ava 2 s . c o m*/ }); getContentPane().add(b, BorderLayout.CENTER); pack(); setVisible(true); }
From source file:Main.java
public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(slider, BorderLayout.CENTER); model.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { System.out.println("Value: " + model.getValue()); }//w w w .j a va2 s.c o m }); pack(); setVisible(true); }