List of usage examples for javax.swing JToggleButton JToggleButton
public JToggleButton(Action a)
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { System.out.println("Selected"); } else { System.out.println("Deselected"); }//w w w .j ava 2s .c o m } }; // Attach Listeners toggleButton.addItemListener(itemListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); // Define ActionListener ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); }/*from ww w . j a va 2s . c o m*/ }; // Attach Listeners toggleButton.addActionListener(actionListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); // Define ActionListener ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); }// w ww .j a v a 2s . c om }; // Attach Listeners toggleButton.addActionListener(actionListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); }
From source file:ToolBarTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); JToolBar bar;// w w w. j a va 2 s . c om bar = new JToolBar(); JToggleButton jb; for (int i = 0; i < 8; i++) { jb = new JToggleButton("" + i); bar.add(jb); if (i == 5) { bar.addSeparator(); } } contentPane.add(bar, BorderLayout.NORTH); frame.setSize(300, 300); frame.show(); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Selecting Toggle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Toggle Button"); 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); }// w ww. j av a2 s. c o m }; // Attach Listeners toggleButton.addChangeListener(changeListener); frame.add(toggleButton, BorderLayout.NORTH); frame.setSize(300, 125); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { JFrame f = new JFrame("JToolBar Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToolBar toolbar = new JToolBar(); Icon icon = MetalIconFactory.getFileChooserDetailViewIcon(); JToggleButton button = new JToggleButton(icon); toolbar.add(button);/*from w w w. j a va 2 s .c o m*/ icon = MetalIconFactory.getFileChooserHomeFolderIcon(); button = new JToggleButton(icon); toolbar.add(button); icon = MetalIconFactory.getFileChooserListViewIcon(); button = new JToggleButton(icon); toolbar.add(button); f.add(toolbar, BorderLayout.NORTH); f.setSize(300, 100); f.setVisible(true); }
From source file:JToggleButtonEvents.java
public static void main(String[] args) { JToggleButton jtb = new JToggleButton("Press Me"); jtb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { System.out.println("ActionEvent!"); }/*from w ww . j ava2 s . co m*/ }); jtb.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ev) { System.out.println("ItemEvent!"); } }); jtb.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent ev) { System.out.println("ChangeEvent!"); } }); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = f.getContentPane(); c.setLayout(new FlowLayout()); c.add(jtb); f.pack(); f.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame("JToolbar Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); JToolBar toolbar = new JToolBar(); Icon icon = MetalIconFactory.getFileChooserDetailViewIcon(); JToggleButton button = new JToggleButton(icon); toolbar.add(button);/*from w ww . java2s .com*/ icon = MetalIconFactory.getFileChooserHomeFolderIcon(); button = new JToggleButton(icon); toolbar.add(button); icon = MetalIconFactory.getFileChooserListViewIcon(); button = new JToggleButton(icon); toolbar.add(button); content.add(toolbar, BorderLayout.NORTH); f.setSize(300, 100); f.setVisible(true); }
From source file:MainClass.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Selected"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); }/* w w w . ja v a 2 s .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) { int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { System.out.println("Selected"); } else { System.out.println("Deselected"); } } }; toggleButton.addActionListener(actionListener); toggleButton.addChangeListener(changeListener); toggleButton.addItemListener(itemListener); frame.add(toggleButton, BorderLayout.NORTH); JToggleButton toggleButton2 = new JToggleButton("Focused"); frame.add(toggleButton2, BorderLayout.CENTER); JToggleButton toggleButton3 = new JToggleButton("Not Selected"); frame.add(toggleButton3, BorderLayout.SOUTH); frame.setSize(300, 125); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToggleButton toggleButton = new JToggleButton("Selected"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println("Action - selected=" + selected + "\n"); }/*from www. j a v a2s . c om*/ }; 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) { int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { System.out.println("Selected"); } else { System.out.println("Deselected"); } } }; // Attach Listeners toggleButton.addActionListener(actionListener); toggleButton.addChangeListener(changeListener); toggleButton.addItemListener(itemListener); Container contentPane = frame.getContentPane(); contentPane.add(toggleButton, BorderLayout.NORTH); JToggleButton toggleButton2 = new JToggleButton("Focused"); contentPane.add(toggleButton2, BorderLayout.CENTER); JToggleButton toggleButton3 = new JToggleButton("Not Selected"); contentPane.add(toggleButton3, BorderLayout.SOUTH); frame.setSize(300, 125); frame.setVisible(true); }