Example usage for javax.swing JToggleButton addActionListener

List of usage examples for javax.swing JToggleButton addActionListener

Introduction

In this page you can find the example usage for javax.swing JToggleButton addActionListener.

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

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!");
        }/*  www. j av  a2  s  .c o  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 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");
        }/*ww  w  .j  a  v  a 2 s  . c om*/
    };
    // 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();
    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  w ww .j  av a 2s. c o  m
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:ScribbleDragAndDrop.java

/**
 * The main method. Creates a simple application using this class. Note the
 * buttons for switching between draw mode and drag mode.
 *//*www. ja va  2 s .c om*/
public static void main(String[] args) {
    // Create a frame and put a scribble pane in it
    JFrame frame = new JFrame("ScribbleDragAndDrop");
    final ScribbleDragAndDrop scribblePane = new ScribbleDragAndDrop();
    frame.getContentPane().add(scribblePane, BorderLayout.CENTER);

    // Create two buttons for switching modes
    JToolBar toolbar = new JToolBar();
    ButtonGroup group = new ButtonGroup();
    JToggleButton draw = new JToggleButton("Draw");
    JToggleButton drag = new JToggleButton("Drag");
    draw.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scribblePane.setDragMode(false);
        }
    });
    drag.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scribblePane.setDragMode(true);
        }
    });
    group.add(draw);
    group.add(drag);
    toolbar.add(draw);
    toolbar.add(drag);
    frame.getContentPane().add(toolbar, BorderLayout.NORTH);

    // Start off in drawing mode
    draw.setSelected(true);
    scribblePane.setDragMode(false);

    // Pop up the window
    frame.setSize(400, 400);
    frame.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");
        }/*from   ww  w  .j a  va2s .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) {
            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");
        }//  w w  w  .j  a  va 2 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) {
            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);
}

From source file:JToggleButtonEventListenerSequence.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");
        }/*from  w ww  .j a v a2  s  .  c  o m*/
    };
    // Define ChangeListener
    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);
        }
    };
    // Define ItemListener
    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);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:Main.java

public static JToggleButton createToggleButton(ImageIcon icon, int dimension, String tooltipText,
        java.awt.event.ActionListener action) {
    JToggleButton btn = createToggleButton(icon, dimension, tooltipText);
    if (action != null)
        btn.addActionListener(action);
    return btn;/*from   w w w.j av  a  2s.  c o m*/
}

From source file:TabbedPaneTest.java

public TabbedPaneFrame() {
    setTitle("TabbedPaneTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    tabbedPane = new JTabbedPane();
    // we set the components to null and delay their loading until the tab is shown
    // for the first time

    ImageIcon icon = new ImageIcon("yellow-ball.gif");

    tabbedPane.addTab("Mercury", icon, null);
    tabbedPane.addTab("Venus", icon, null);
    tabbedPane.addTab("Earth", icon, null);
    tabbedPane.addTab("Mars", icon, null);
    tabbedPane.addTab("Jupiter", icon, null);
    tabbedPane.addTab("Saturn", icon, null);
    tabbedPane.addTab("Uranus", icon, null);
    tabbedPane.addTab("Neptune", icon, null);
    tabbedPane.addTab("Pluto", null, null);

    final int plutoIndex = tabbedPane.indexOfTab("Pluto");
    JPanel plutoPanel = new JPanel();
    plutoPanel.add(new JLabel("Pluto", icon, SwingConstants.LEADING));
    JToggleButton plutoCheckBox = new JCheckBox();
    plutoCheckBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tabbedPane.remove(plutoIndex);
        }/*from ww  w .j a va  2s.  c o  m*/
    });
    plutoPanel.add(plutoCheckBox);
    tabbedPane.setTabComponentAt(plutoIndex, plutoPanel);

    add(tabbedPane, "Center");

    tabbedPane.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent event) {

            // check if this tab still has a null component

            if (tabbedPane.getSelectedComponent() == null) {
                // set the component to the image icon

                int n = tabbedPane.getSelectedIndex();
                loadTab(n);
            }
        }
    });

    loadTab(0);

    JPanel buttonPanel = new JPanel();
    ButtonGroup buttonGroup = new ButtonGroup();
    JRadioButton wrapButton = new JRadioButton("Wrap tabs");
    wrapButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
        }
    });
    buttonPanel.add(wrapButton);
    buttonGroup.add(wrapButton);
    wrapButton.setSelected(true);
    JRadioButton scrollButton = new JRadioButton("Scroll tabs");
    scrollButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        }
    });
    buttonPanel.add(scrollButton);
    buttonGroup.add(scrollButton);
    add(buttonPanel, BorderLayout.SOUTH);
}

From source file:components.CrayonPanel.java

protected JToggleButton createCrayon(String name, Border normalBorder) {
    JToggleButton crayon = new JToggleButton();
    crayon.setActionCommand(name);/*from w  ww .ja v  a  2 s. co m*/
    crayon.addActionListener(this);

    //Set the image or, if that's invalid, equivalent text.
    ImageIcon icon = createImageIcon("images/" + name + ".gif");
    if (icon != null) {
        crayon.setIcon(icon);
        crayon.setToolTipText("The " + name + " crayon");
        crayon.setBorder(normalBorder);
    } else {
        crayon.setText("Image not found. This is the " + name + " button.");
        crayon.setFont(crayon.getFont().deriveFont(Font.ITALIC));
        crayon.setHorizontalAlignment(JButton.HORIZONTAL);
        crayon.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }

    return crayon;
}