Example usage for javax.swing JToggleButton JToggleButton

List of usage examples for javax.swing JToggleButton JToggleButton

Introduction

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

Prototype

public JToggleButton(Action a) 

Source Link

Document

Creates a toggle button where properties are taken from the Action supplied.

Usage

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   w ww  . jav a  2  s  .  c om
    };
    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:RasterDemo.java

public RasterDemo() {
    super();/*w w  w . j  ava  2 s .  c  o  m*/
    Container container = getContentPane();

    displayPanel = new RasterPanel();
    container.add(displayPanel);

    Box box = Box.createHorizontalBox();
    flipButton = new JToggleButton("Flip the Image");
    flipButton.addActionListener(new ButtonListener());
    box.add(Box.createHorizontalGlue());
    box.add(flipButton);
    box.add(Box.createHorizontalGlue());
    container.add(box, BorderLayout.SOUTH);

    addWindowListener(new WindowEventHandler());
    setSize(450, 400);
    show();
}

From source file:UndoableToggleApp2.java

public UndoableToggleApp2() {
    JToggleButton tog = new JToggleButton("ToggleButton");
    JCheckBox cb = new JCheckBox("CompoundEdit ExampleCheckBox");
    JRadioButton radio = new JRadioButton("RadioButton");

    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);/*from  www.  ja  v a  2  s . c  om*/
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

    undoButton.setEnabled(false);
    redoButton.setEnabled(false);
    endButton.setEnabled(false);

    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.undo();
            } catch (CannotUndoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    endButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit.end();
            endButton.setEnabled(false);
            undoButton.setEnabled(edit.canUndo());
            redoButton.setEnabled(edit.canRedo());
        }
    });

    Box undoRedoEndBox = new Box(BoxLayout.X_AXIS);
    undoRedoEndBox.add(Box.createGlue());
    undoRedoEndBox.add(undoButton);
    undoRedoEndBox.add(Box.createHorizontalStrut(2));
    undoRedoEndBox.add(redoButton);
    undoRedoEndBox.add(Box.createHorizontalStrut(2));
    undoRedoEndBox.add(endButton);
    undoRedoEndBox.add(Box.createGlue());

    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoEndBox, BorderLayout.SOUTH);
    setSize(400, 150);
}

From source file:Buttons.java

public void init() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(jb);/*  w w w. jav a  2s  . c om*/
    cp.add(new JToggleButton("JToggleButton"));
    cp.add(new JCheckBox("JCheckBox"));
    cp.add(new JRadioButton("JRadioButton"));
    JPanel jp = new JPanel();
    jp.setBorder(new TitledBorder("Directions"));
    jp.add(up);
    jp.add(down);
    jp.add(left);
    jp.add(right);
    cp.add(jp);
}

From source file:UndoableToggleApp.java

public UndoableToggleApp() {

    // Create some toggle buttons (and subclasses)
    JToggleButton tog = new JToggleButton("ToggleButton");
    JCheckBox cb = new JCheckBox("CheckBox");
    JRadioButton radio = new JRadioButton("RadioButton");

    // Add our listener to each toggle button
    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);//  ww w  .  j  a  va2  s.  c om
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    // Layout the buttons
    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

    // Create undo and redo buttons (initially disabled)
    undoButton = new JButton("Undo");
    redoButton = new JButton("Redo");
    undoButton.setEnabled(false);
    redoButton.setEnabled(false);

    // Add a listener to the undo button. It attempts to call undo() on the
    // current edit, then enables/disables the undo/redo buttons as
    // appropriate.
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.undo();
            } catch (CannotUndoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Add a redo listener: just like the undo listener, but for redo this
    // time.
    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Layout the undo/redo buttons
    Box undoRedoBox = new Box(BoxLayout.X_AXIS);
    undoRedoBox.add(Box.createGlue());
    undoRedoBox.add(undoButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(redoButton);
    undoRedoBox.add(Box.createGlue());

    // Layout the main frame
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoBox, BorderLayout.SOUTH);
    setSize(400, 150);
}

From source file:UndoableToggleApp2.java

public UndoableToggleApp2() {

    // Create some toggle buttons (and subclasses)
    JToggleButton tog = new JToggleButton("ToggleButton");
    JCheckBox cb = new JCheckBox("CompoundEdit ExampleCheckBox");
    JRadioButton radio = new JRadioButton("RadioButton");

    // Add our listener to each toggle button
    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);//from   w  w  w.  ja v a  2s.co m
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    // Lay out the buttons
    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

    // Create undo and redo buttons (initially disabled)
    undoButton = new JButton("Undo");
    redoButton = new JButton("Redo");
    endButton = new JButton("End");
    undoButton.setEnabled(false);
    redoButton.setEnabled(false);
    endButton.setEnabled(false);

    // Add a listener to the undo button. It attempts to call undo() on the
    // current edit, then enables/disables the undo/redo buttons as
    // appropriate.
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.undo();
            } catch (CannotUndoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Add a redo listener: just like the undo listener, but for redo this
    // time.
    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Add an end listener. This listener will call end() on the
    // CompoundEdit
    // and update the undo/redo buttons.
    endButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit.end();
            endButton.setEnabled(false);
            undoButton.setEnabled(edit.canUndo());
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Layout the undo/redo/end buttons
    Box undoRedoEndBox = new Box(BoxLayout.X_AXIS);
    undoRedoEndBox.add(Box.createGlue());
    undoRedoEndBox.add(undoButton);
    undoRedoEndBox.add(Box.createHorizontalStrut(2));
    undoRedoEndBox.add(redoButton);
    undoRedoEndBox.add(Box.createHorizontalStrut(2));
    undoRedoEndBox.add(endButton);
    undoRedoEndBox.add(Box.createGlue());

    // Layout the main frame
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoEndBox, BorderLayout.SOUTH);
    setSize(400, 150);
}

From source file:UndoableToggleApp4.java

public UndoableToggleApp4() {

    // Create some toggle buttons (and subclasses).
    tog = new JToggleButton("ToggleButton");
    cb = new JCheckBox("CheckBox");
    radio = new JRadioButton("RadioButton");

    // Add our listener to the buttons.
    SimpleListener sl = new SimpleListener();
    tog.addActionListener(sl);/*  w ww. j a  v  a  2s  .  co  m*/
    cb.addActionListener(sl);
    radio.addActionListener(sl);

    // Lay out the buttons.
    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    buttonBox.add(tog);
    buttonBox.add(cb);
    buttonBox.add(radio);

    // Create undo, redo, start, and end buttons.
    startButton = new JButton("Start");
    endButton = new JButton("End");
    undoButton = new JButton("Undo");
    redoButton = new JButton("Redo");
    startButton.setEnabled(true);
    endButton.setEnabled(false);
    undoButton.setEnabled(false);
    redoButton.setEnabled(false);

    // Add a listener to the start button. It creates a new StateEdit,
    // passing in this frame as the StateEditable.
    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit = new StateEdit(UndoableToggleApp4.this);
            startButton.setEnabled(false);
            endButton.setEnabled(true);
            //undoButton.setEnabled(edit.canUndo());
            //
            // NOTE: We really don't want to be able to undo until end() is
            // pressed,
            // but StateEdit does not enforce this for us!
            undoButton.setEnabled(false);
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Add a listener to the end button. It will call end() on the
    // StateEdit.
    endButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            edit.end();
            startButton.setEnabled(true);
            endButton.setEnabled(false);
            undoButton.setEnabled(edit.canUndo());
            redoButton.setEnabled(edit.canRedo());
        }
    });

    // Add a listener to the undo button. It attempts to call undo() on the
    // current edit, then enables/disables the undo/redo buttons as
    // appropriate.
    undoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.undo();
            } catch (CannotUndoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Add a redo listener: just like the undo listener.
    redoButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            try {
                edit.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            } finally {
                undoButton.setEnabled(edit.canUndo());
                redoButton.setEnabled(edit.canRedo());
            }
        }
    });

    // Lay out the state/end and undo/redo buttons.
    Box undoRedoBox = new Box(BoxLayout.X_AXIS);
    undoRedoBox.add(Box.createGlue());
    undoRedoBox.add(startButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(endButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(undoButton);
    undoRedoBox.add(Box.createHorizontalStrut(2));
    undoRedoBox.add(redoButton);
    undoRedoBox.add(Box.createGlue());

    // Lay out the main frame.
    Container content = getContentPane();
    content.setLayout(new BorderLayout());
    content.add(buttonBox, BorderLayout.CENTER);
    content.add(undoRedoBox, BorderLayout.SOUTH);
    setSize(400, 150);
}

From source file:Main.java

/**
 * Creates a {@link JCheckBoxMenuItem} for a menu or {@link JToggleButton}
 * for a tool bar/*from   w ww . j ava2  s . c  om*/
 * 
 * @param menuOrToolBar
 * @param action 
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
public static void addBooleanActionTo(Container menuOrToolBar, Action action) {
    Method addButton;
    try {
        addButton = Container.class.getMethod("add", Component.class);
        if (menuOrToolBar instanceof JMenu) {
            addButton.invoke(menuOrToolBar, new JCheckBoxMenuItem(action));
        } else {
            final JToggleButton jToggleButton = new JToggleButton(action);
            jToggleButton.setText(null);
            addButton.invoke(menuOrToolBar, jToggleButton);
        }
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:maltcms.ui.fileHandles.serialized.JFCPanel.java

/**
 *
 *//*from   w  w w . j  av a  2  s  .  c o m*/
public JFCPanel() {
    this.chartPanel = new ChartPanel(new JFreeChart(new XYPlot()));

    initComponents();
    jToggleButton1ActionPerformed(new ActionEvent(this, 0, ""));
    toggleYAxisFix = new JToggleButton(new AbstractAction("Fix y") {

        @Override
        public void actionPerformed(ActionEvent ae) {
            chartPanel.getChart().getXYPlot().getRangeAxis()
                    .setAutoRange(!chartPanel.getChart().getXYPlot().getRangeAxis().isAutoRange());
        }
    });
    this.jToolBar2.add(toggleYAxisFix);

    toggleXAxisFix = new JToggleButton(new AbstractAction("Fix x") {

        @Override
        public void actionPerformed(ActionEvent ae) {
            chartPanel.getChart().getXYPlot().getDomainAxis()
                    .setAutoRange(!chartPanel.getChart().getXYPlot().getDomainAxis().isAutoRange());
        }
    });
    this.jToolBar2.add(toggleXAxisFix);

    toggleAnnotations = new JToggleButton(new AbstractAction("Annotations") {

        @Override
        public void actionPerformed(ActionEvent ae) {
            if (chartPanel.getChart().getXYPlot().getAnnotations().isEmpty()) {
                Logger.getLogger(getClass().getName()).info("Adding annotations");
                for (XYAnnotation xya : annotations) {
                    chartPanel.getChart().getXYPlot().addAnnotation(xya);
                }
                chartPanel.getChart().fireChartChanged();
                toggleAnnotations.setSelected(true);
            } else {
                Logger.getLogger(getClass().getName()).info("Removing annotations");
                List<?> l = chartPanel.getChart().getXYPlot().getAnnotations();
                if (!l.isEmpty()) {
                    annotations.clear();
                    for (Object o : l) {
                        annotations.add((XYAnnotation) o);
                    }
                }
                chartPanel.getChart().getXYPlot().clearAnnotations();
            }
        }
    });
    this.jToolBar2.add(toggleAnnotations);
    toggleAnnotations.setSelected(true);
}

From source file:de.fhbingen.wbs.wpOverview.tabs.AvailabilityGraphGUI.java

/**
 * Creates the panel for the GUI./*from   w  w w  . ja  va2  s  .c  o  m*/
 * @return The created panel.
 */
protected final JPanel createOptionPanel() {
    General generalStrings = LocalizedStrings.getGeneralStrings();

    JPanel optionPanel = new JPanel();

    buttons = new JToggleButton[4];
    buttons[AvailabilityGraph.DAY] = new JToggleButton(generalStrings.day());
    buttons[AvailabilityGraph.WEEK] = new JToggleButton(generalStrings.week());
    buttons[AvailabilityGraph.MONTH] = new JToggleButton(generalStrings.month());
    buttons[AvailabilityGraph.YEAR] = new JToggleButton(generalStrings.year());
    for (int i = 0; i < buttons.length; i++) {
        optionPanel.add(buttons[i]);
    }
    optionPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    btnPrev = new JButton("<");
    optionPanel.add(btnPrev);

    btnNext = new JButton(">");
    optionPanel.add(btnNext);

    btnManualAv = new JToggleButton(
            LocalizedStrings.getButton().show(LocalizedStrings.getWbs().manualAvailabilities()));
    optionPanel.add(btnManualAv);

    return optionPanel;
}