List of usage examples for javax.swing JRadioButton addItemListener
public void addItemListener(ItemListener l)
ItemListener
to the checkbox
. From source file:ItemTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); ItemListener listener = new ItemListener() { public void itemStateChanged(ItemEvent e) { System.out.println("Source: " + name(e.getSource())); System.out.println("Item: " + name(e.getItem())); int state = e.getStateChange(); System.out.println("State: " + ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected")); }/*from ww w .java 2s . com*/ private String name(Object o) { if (o instanceof JComponent) { JComponent comp = (JComponent) o; return comp.getName(); } else { return o.toString(); } } }; JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton option = new JRadioButton("French Fries", true); option.setName(option.getText()); option.addItemListener(listener); group.add(option); panel.add(option); option = new JRadioButton("Onion Rings", false); option.setName(option.getText()); option.addItemListener(listener); group.add(option); panel.add(option); option = new JRadioButton("Ice Cream", false); option.setName(option.getText()); option.addItemListener(listener); group.add(option); panel.add(option); contentPane.add(panel, BorderLayout.NORTH); String flavors[] = { "Item 1", "Item 2", "Item 3" }; JComboBox jc = new JComboBox(flavors); jc.setName("Combo"); jc.addItemListener(listener); jc.setMaximumRowCount(4); contentPane.add(jc, BorderLayout.SOUTH); frame.pack(); frame.show(); }
From source file:JRadioButtonItemListener.java
public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ItemListener itemListener = new ItemListener() { String lastSelected;//w w w . ja v a2 s .co m 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); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addItemListener(itemListener); bRadioButton.addItemListener(itemListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JRadioButton dem = new JRadioButton("Bill", false); dem.setActionCommand("Bill"); JRadioButton rep = new JRadioButton("Bob", false); rep.setActionCommand("Bob"); JRadioButton ind = new JRadioButton("Ross", false); ind.setActionCommand("Ross"); final ButtonGroup group = new ButtonGroup(); group.add(dem);/*from ww w. j a va2 s . co m*/ group.add(rep); group.add(ind); class VoteActionListener implements ActionListener { public void actionPerformed(ActionEvent ex) { String choice = group.getSelection().getActionCommand(); System.out.println("ACTION Candidate Selected: " + choice); } } class VoteItemListener implements ItemListener { public void itemStateChanged(ItemEvent ex) { String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand(); boolean selected = (ex.getStateChange() == ItemEvent.SELECTED); System.out.println("ITEM Candidate Selected: " + selected + " Selection: " + item); } } ActionListener al = new VoteActionListener(); dem.addActionListener(al); rep.addActionListener(al); ind.addActionListener(al); ItemListener il = new VoteItemListener(); dem.addItemListener(il); rep.addItemListener(il); ind.addItemListener(il); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = frame.getContentPane(); c.setLayout(new GridLayout(4, 1)); c.add(new JLabel("Please Cast Your Vote")); c.add(dem); c.add(rep); c.add(ind); frame.pack(); frame.setVisible(true); }
From source file:SimpleButtonGroupExample.java
public static void main(String[] args) { // Some choices JRadioButton choice1, choice2, choice3; choice1 = new JRadioButton("Bach: Well Tempered Clavier, Book I"); choice1.setActionCommand("bach1"); choice2 = new JRadioButton("Bach: Well Tempered Clavier, Book II"); choice2.setActionCommand("bach2"); choice3 = new JRadioButton("Shostakovich: 24 Preludes and Fugues"); choice3.setActionCommand("shostakovich"); // A group, to ensure that we only vote for one. final ButtonGroup group = new ButtonGroup(); group.add(choice1);//from w ww . j ava 2 s . co m group.add(choice2); group.add(choice3); // A simple ActionListener, showing each selection using the ButtonModel class VoteActionListener implements ActionListener { public void actionPerformed(ActionEvent ev) { String choice = group.getSelection().getActionCommand(); System.out.println("ACTION Choice Selected: " + choice); } } // A simple ItemListener, showing each selection and deselection class VoteItemListener implements ItemListener { public void itemStateChanged(ItemEvent ev) { boolean selected = (ev.getStateChange() == ItemEvent.SELECTED); AbstractButton button = (AbstractButton) ev.getItemSelectable(); System.out .println("ITEM Choice Selected: " + selected + ", Selection: " + button.getActionCommand()); } } // Add listeners to each button ActionListener alisten = new VoteActionListener(); choice1.addActionListener(alisten); choice2.addActionListener(alisten); choice3.addActionListener(alisten); ItemListener ilisten = new VoteItemListener(); choice1.addItemListener(ilisten); choice2.addItemListener(ilisten); choice3.addItemListener(ilisten); // Throw everything together JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = frame.getContentPane(); c.setLayout(new GridLayout(0, 1)); c.add(new JLabel("Vote for your favorite prelude & fugue cycle")); c.add(choice1); c.add(choice2); c.add(choice3); frame.pack(); frame.setVisible(true); }
From source file:GroupRadio.java
public static Container createRadioButtonGrouping(String elements[], String title, ActionListener actionListener, ItemListener itemListener, ChangeListener changeListener) { JPanel panel = new JPanel(new GridLayout(0, 1)); // If title set, create titled border if (title != null) { Border border = BorderFactory.createTitledBorder(title); panel.setBorder(border);/*from w ww . j a v a2s. c om*/ } // Create group ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton; // For each String passed in: // Create button, add to panel, and add to group for (int i = 0, n = elements.length; i < n; i++) { aRadioButton = new JRadioButton(elements[i]); panel.add(aRadioButton); group.add(aRadioButton); if (actionListener != null) { aRadioButton.addActionListener(actionListener); } if (itemListener != null) { aRadioButton.addItemListener(itemListener); } if (changeListener != null) { aRadioButton.addChangeListener(changeListener); } } return panel; }
From source file:ToggleButtonCheckBoxRadioButton.java
public TogglePanel() { JToggleButton tog = new JToggleButton("Toggle"); ItemListener listener = new ItemListener() { public void itemStateChanged(ItemEvent e) { AbstractButton src = (AbstractButton) (e.getSource()); System.out.println("Toggle: " + src.getText()); }/*from ww w. j a v a 2 s . co m*/ }; tog.addItemListener(listener); add(tog); JCheckBox cbox = new JCheckBox("Checkbox"); cbox.addItemListener(listener); add(cbox); ButtonGroup btngroup = new ButtonGroup(); for (int i = 1; i <= 3; i++) { JRadioButton radio = new JRadioButton("Radio " + i); btngroup.add(radio); radio.addItemListener(listener); add(radio); } }
From source file:com.google.code.facebook.graph.sna.applet.RadialTreeLensDemo.java
/** * create an instance of a simple graph with controls to * demo the zoomand hyperbolic features. * /*from w w w. j a va 2s. c om*/ */ public RadialTreeLensDemo() { // create a simple graph for the demo // create a simple graph for the demo graph = new DelegateForest<String, Integer>(); createTree(); layout = new TreeLayout<String, Integer>(graph); radialLayout = new RadialTreeLayout<String, Integer>(graph); radialLayout.setSize(new Dimension(600, 600)); Dimension preferredSize = new Dimension(600, 600); final VisualizationModel<String, Integer> visualizationModel = new DefaultVisualizationModel<String, Integer>( radialLayout, preferredSize); vv = new VisualizationViewer<String, Integer>(visualizationModel, preferredSize); PickedState<String> ps = vv.getPickedVertexState(); PickedState<Integer> pes = vv.getPickedEdgeState(); vv.getRenderContext().setVertexFillPaintTransformer( new PickableVertexPaintTransformer<String>(ps, Color.red, Color.yellow)); vv.getRenderContext().setEdgeDrawPaintTransformer( new PickableEdgePaintTransformer<Integer>(pes, Color.black, Color.cyan)); vv.setBackground(Color.white); vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>()); vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line()); // add a listener for ToolTips vv.setVertexToolTipTransformer(new ToStringLabeller<String>()); Container content = getContentPane(); GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv); content.add(gzsp); /** * the regular graph mouse for the normal view */ final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse(); vv.setGraphMouse(graphMouse); vv.addKeyListener(graphMouse.getModeKeyListener()); rings = new Rings(); vv.addPreRenderPaintable(rings); hyperbolicViewSupport = new ViewLensSupport<String, Integer>(vv, new HyperbolicShapeTransformer(vv, vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW)), new ModalLensGraphMouse()); final ScalingControl scaler = new CrossoverScalingControl(); JButton plus = new JButton("+"); plus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1.1f, vv.getCenter()); } }); JButton minus = new JButton("-"); minus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1 / 1.1f, vv.getCenter()); } }); final JRadioButton hyperView = new JRadioButton("Hyperbolic View"); hyperView.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { hyperbolicViewSupport.activate(e.getStateChange() == ItemEvent.SELECTED); } }); graphMouse.addItemListener(hyperbolicViewSupport.getGraphMouse().getModeListener()); JMenuBar menubar = new JMenuBar(); menubar.add(graphMouse.getModeMenu()); gzsp.setCorner(menubar); JPanel controls = new JPanel(); JPanel zoomControls = new JPanel(new GridLayout(2, 1)); zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom")); JPanel hyperControls = new JPanel(new GridLayout(3, 2)); hyperControls.setBorder(BorderFactory.createTitledBorder("Examiner Lens")); zoomControls.add(plus); zoomControls.add(minus); JPanel modeControls = new JPanel(new BorderLayout()); modeControls.setBorder(BorderFactory.createTitledBorder("Mouse Mode")); modeControls.add(graphMouse.getModeComboBox()); hyperControls.add(hyperView); controls.add(zoomControls); controls.add(hyperControls); controls.add(modeControls); content.add(controls, BorderLayout.SOUTH); }
From source file:edu.uci.ics.jung.samples.RadialTreeLensDemo.java
/** * create an instance of a simple graph with controls to * demo the zoomand hyperbolic features. * /*from w w w . ja va 2s. c o m*/ */ @SuppressWarnings({ "unchecked", "rawtypes" }) public RadialTreeLensDemo() { // create a simple graph for the demo // create a simple graph for the demo graph = new DelegateForest<String, Integer>(); createTree(); layout = new TreeLayout<String, Integer>(graph); radialLayout = new RadialTreeLayout<String, Integer>(graph); radialLayout.setSize(new Dimension(600, 600)); Dimension preferredSize = new Dimension(600, 600); final VisualizationModel<String, Integer> visualizationModel = new DefaultVisualizationModel<String, Integer>( radialLayout, preferredSize); vv = new VisualizationViewer<String, Integer>(visualizationModel, preferredSize); PickedState<String> ps = vv.getPickedVertexState(); PickedState<Integer> pes = vv.getPickedEdgeState(); vv.getRenderContext().setVertexFillPaintTransformer( new PickableVertexPaintTransformer<String>(ps, Color.red, Color.yellow)); vv.getRenderContext().setEdgeDrawPaintTransformer( new PickableEdgePaintTransformer<Integer>(pes, Color.black, Color.cyan)); vv.setBackground(Color.white); vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>()); vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line()); // add a listener for ToolTips vv.setVertexToolTipTransformer(new ToStringLabeller<String>()); Container content = getContentPane(); GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv); content.add(gzsp); /** * the regular graph mouse for the normal view */ final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse(); vv.setGraphMouse(graphMouse); vv.addKeyListener(graphMouse.getModeKeyListener()); rings = new Rings(); vv.addPreRenderPaintable(rings); hyperbolicViewSupport = new ViewLensSupport<String, Integer>(vv, new HyperbolicShapeTransformer(vv, vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW)), new ModalLensGraphMouse()); final ScalingControl scaler = new CrossoverScalingControl(); JButton plus = new JButton("+"); plus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1.1f, vv.getCenter()); } }); JButton minus = new JButton("-"); minus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1 / 1.1f, vv.getCenter()); } }); final JRadioButton hyperView = new JRadioButton("Hyperbolic View"); hyperView.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { hyperbolicViewSupport.activate(e.getStateChange() == ItemEvent.SELECTED); } }); graphMouse.addItemListener(hyperbolicViewSupport.getGraphMouse().getModeListener()); JMenuBar menubar = new JMenuBar(); menubar.add(graphMouse.getModeMenu()); gzsp.setCorner(menubar); JPanel controls = new JPanel(); JPanel zoomControls = new JPanel(new GridLayout(2, 1)); zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom")); JPanel hyperControls = new JPanel(new GridLayout(3, 2)); hyperControls.setBorder(BorderFactory.createTitledBorder("Examiner Lens")); zoomControls.add(plus); zoomControls.add(minus); JPanel modeControls = new JPanel(new BorderLayout()); modeControls.setBorder(BorderFactory.createTitledBorder("Mouse Mode")); modeControls.add(graphMouse.getModeComboBox()); hyperControls.add(hyperView); controls.add(zoomControls); controls.add(hyperControls); controls.add(modeControls); content.add(controls, BorderLayout.SOUTH); }
From source file:com.google.code.facebook.graph.sna.applet.BalloonLayoutDemo.java
public BalloonLayoutDemo() { // create a simple graph for the demo graph = new DelegateForest<String, Integer>(); createTree();/*from w ww. j a va2 s. com*/ layout = new TreeLayout<String, Integer>(graph); radialLayout = new BalloonLayout<String, Integer>(graph); radialLayout.setSize(new Dimension(900, 900)); vv = new VisualizationViewer<String, Integer>(layout, new Dimension(600, 600)); vv.setBackground(Color.white); vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line()); vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller()); // add a listener for ToolTips vv.setVertexToolTipTransformer(new ToStringLabeller()); vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray)); rings = new Rings(radialLayout); Container content = getContentPane(); final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv); content.add(panel); final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse(); vv.setGraphMouse(graphMouse); vv.addKeyListener(graphMouse.getModeKeyListener()); hyperbolicViewSupport = new ViewLensSupport<String, Integer>(vv, new HyperbolicShapeTransformer(vv, vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW)), new ModalLensGraphMouse()); graphMouse.addItemListener(hyperbolicViewSupport.getGraphMouse().getModeListener()); JComboBox modeBox = graphMouse.getModeComboBox(); modeBox.addItemListener(graphMouse.getModeListener()); graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING); final ScalingControl scaler = new CrossoverScalingControl(); vv.scaleToLayout(scaler); JButton plus = new JButton("+"); plus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1.1f, vv.getCenter()); } }); JButton minus = new JButton("-"); minus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1 / 1.1f, vv.getCenter()); } }); JToggleButton radial = new JToggleButton("Balloon"); radial.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, layout, radialLayout); Animator animator = new Animator(lt); animator.start(); vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setToIdentity(); vv.addPreRenderPaintable(rings); } else { LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, radialLayout, layout); Animator animator = new Animator(lt); animator.start(); vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setToIdentity(); vv.removePreRenderPaintable(rings); } vv.repaint(); } }); final JRadioButton hyperView = new JRadioButton("Hyperbolic View"); hyperView.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { hyperbolicViewSupport.activate(e.getStateChange() == ItemEvent.SELECTED); } }); JPanel scaleGrid = new JPanel(new GridLayout(1, 0)); scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom")); JPanel controls = new JPanel(); scaleGrid.add(plus); scaleGrid.add(minus); controls.add(radial); controls.add(scaleGrid); controls.add(modeBox); controls.add(hyperView); content.add(controls, BorderLayout.SOUTH); }
From source file:edu.uci.ics.jung.samples.BalloonLayoutDemo.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public BalloonLayoutDemo() { // create a simple graph for the demo graph = new DelegateForest<String, Integer>(); createTree();//from w w w . ja v a 2 s . co m layout = new TreeLayout<String, Integer>(graph); radialLayout = new BalloonLayout<String, Integer>(graph); radialLayout.setSize(new Dimension(900, 900)); vv = new VisualizationViewer<String, Integer>(layout, new Dimension(600, 600)); vv.setBackground(Color.white); vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line()); vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller()); // add a listener for ToolTips vv.setVertexToolTipTransformer(new ToStringLabeller()); vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray)); rings = new Rings(radialLayout); Container content = getContentPane(); final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv); content.add(panel); final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse(); vv.setGraphMouse(graphMouse); vv.addKeyListener(graphMouse.getModeKeyListener()); hyperbolicViewSupport = new ViewLensSupport<String, Integer>(vv, new HyperbolicShapeTransformer(vv, vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW)), new ModalLensGraphMouse()); graphMouse.addItemListener(hyperbolicViewSupport.getGraphMouse().getModeListener()); JComboBox modeBox = graphMouse.getModeComboBox(); modeBox.addItemListener(graphMouse.getModeListener()); graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING); final ScalingControl scaler = new CrossoverScalingControl(); vv.scaleToLayout(scaler); JButton plus = new JButton("+"); plus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1.1f, vv.getCenter()); } }); JButton minus = new JButton("-"); minus.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scaler.scale(vv, 1 / 1.1f, vv.getCenter()); } }); JToggleButton radial = new JToggleButton("Balloon"); radial.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, layout, radialLayout); Animator animator = new Animator(lt); animator.start(); vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setToIdentity(); vv.addPreRenderPaintable(rings); } else { LayoutTransition<String, Integer> lt = new LayoutTransition<String, Integer>(vv, radialLayout, layout); Animator animator = new Animator(lt); animator.start(); vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setToIdentity(); vv.removePreRenderPaintable(rings); } vv.repaint(); } }); final JRadioButton hyperView = new JRadioButton("Hyperbolic View"); hyperView.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { hyperbolicViewSupport.activate(e.getStateChange() == ItemEvent.SELECTED); } }); JPanel scaleGrid = new JPanel(new GridLayout(1, 0)); scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom")); JPanel controls = new JPanel(); scaleGrid.add(plus); scaleGrid.add(minus); controls.add(radial); controls.add(scaleGrid); controls.add(modeBox); controls.add(hyperView); content.add(controls, BorderLayout.SOUTH); }