Example usage for javax.swing ButtonModel isPressed

List of usage examples for javax.swing ButtonModel isPressed

Introduction

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

Prototype

boolean isPressed();

Source Link

Document

Indicates if the button is pressed.

Usage

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);
        }/* w  ww .  j a v  a  2s. 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:Main.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);
        }/* w w w . ja v a 2s.  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.getModel().addActionListener(actionListener);
    checkBox.getModel().addChangeListener(changeListener);
    checkBox.getModel().addItemListener(itemListener);

    checkBox.getModel().removeActionListener(actionListener);
    checkBox.getModel().removeChangeListener(changeListener);
    checkBox.getModel().removeItemListener(itemListener);

    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JButton button = new JButton("Test");
    ButtonModel model = button.getModel();
    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Armed: " + model.isArmed() + " Enabled: " + model.isEnabled() + " Pressed: "
                    + model.isPressed());
        }//from w  w w.  jav  a  2s  . com
    });

    JOptionPane.showMessageDialog(null, button);
}

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());
        }/* ww 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:RedGreenBorder.java

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Insets insets = getBorderInsets(c);
    Color horizontalColor;/*from w ww . j ava2  s. c om*/
    if (c.isEnabled()) {
        boolean pressed = false;
        if (c instanceof AbstractButton) {
            ButtonModel model = ((AbstractButton) c).getModel();
            pressed = model.isPressed();
        }
        if (pressed) {
            horizontalColor = Color.RED;
        } else {
            horizontalColor = Color.GREEN;
        }
    } else {
        horizontalColor = Color.LIGHT_GRAY;
    }
    g.setColor(horizontalColor);

    g.fillRect(0, 0, width, insets.top);
}

From source file:RedGreenBorder.java

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Insets insets = getBorderInsets(c);
    Color horizontalColor;//  w  w  w. ja  va  2 s  .co m
    Color verticalColor;
    if (c.isEnabled()) {
        boolean pressed = false;
        if (c instanceof AbstractButton) {
            ButtonModel model = ((AbstractButton) c).getModel();
            pressed = model.isPressed();
        }
        if (pressed) {
            horizontalColor = Color.red;
            verticalColor = Color.green;
        } else {
            horizontalColor = Color.green;
            verticalColor = Color.red;
        }
    } else {
        horizontalColor = Color.lightGray;
        verticalColor = Color.lightGray;
    }
    g.setColor(horizontalColor);

    g.translate(x, y);

    // top
    g.fillRect(0, 0, width, insets.top);
    // bottom
    g.fillRect(0, height - insets.bottom, width, insets.bottom);

    g.setColor(verticalColor);
    // left
    g.fillRect(0, insets.top, insets.left, height - insets.top - insets.bottom);
    g.fillRect(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
    g.translate(-x, -y);
}

From source file:MainClass.java

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Insets insets = getBorderInsets(c);
    Color horizontalColor;/*from   w w w .jav a 2 s  .co  m*/
    Color verticalColor;
    if (c.isEnabled()) {
        boolean pressed = false;
        if (c instanceof AbstractButton) {
            ButtonModel model = ((AbstractButton) c).getModel();
            pressed = model.isPressed();
        }
        if (pressed) {
            horizontalColor = Color.WHITE;
            verticalColor = Color.BLACK;
        } else {
            horizontalColor = Color.BLACK;
            verticalColor = Color.WHITE;
        }
    } else {
        horizontalColor = Color.LIGHT_GRAY;
        verticalColor = Color.LIGHT_GRAY;
    }
    g.setColor(horizontalColor);

    g.translate(x, y);

    // top
    g.fillRect(0, 0, width, insets.top);
    // bottom
    g.fillRect(0, height - insets.bottom, width, insets.bottom);

    g.setColor(verticalColor);
    // left
    g.fillRect(0, insets.top, insets.left, height - insets.top - insets.bottom);
    // right
    g.fillRect(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
    g.translate(-x, -y);
}

From source file:com.joey.software.regionSelectionToolkit.controlers.ImageProfileToolDynamicRangePanel.java

public JPanel getControls() {
    if (controls == null) {
        controls = new JPanel(new BorderLayout());

        JPanel dirButton = new JPanel(new GridLayout(1, 2));
        dirButton.add(moveUpData);/*  w  w w  .ja va 2  s .  c  o m*/
        dirButton.add(moveDownData);

        JPanel pointsPanel = new JPanel(new BorderLayout());
        pointsPanel.add(saveData, BorderLayout.NORTH);
        pointsPanel.add(numPoints, BorderLayout.CENTER);
        pointsPanel.add(updatePoints, BorderLayout.EAST);
        pointsPanel.add(showFlattenedButton, BorderLayout.SOUTH);

        JPanel offsetPane = new JPanel(new BorderLayout());
        offsetPane.add(showOffset, BorderLayout.WEST);
        offsetPane.add(offset, BorderLayout.CENTER);

        JPanel temp = new JPanel(new BorderLayout());
        temp.add(offsetPane, BorderLayout.NORTH);
        temp.add(transparance, BorderLayout.CENTER);

        JPanel toolPanel = new JPanel(new BorderLayout());
        toolPanel.add(dirButton, BorderLayout.NORTH);
        toolPanel.add(pointsPanel, BorderLayout.CENTER);
        toolPanel.add(temp, BorderLayout.SOUTH);

        controls.add(toolPanel, BorderLayout.NORTH);

        transparance.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                panel.repaint();

            }
        });
        showOffset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.repaint();
            }
        });

        offset.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                panel.shapeChanged();
                panel.repaint();
            }

        });
        showFlattenedButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                showFlattenedImage();
            }
        });
        updatePoints.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setDataPoints((Integer) numPoints.getValue());
            }
        });

        moveUPTimer = new Timer(delay, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(1.0f / view.getImage().getImage().getHeight());
            }
        });

        moveDownTimer = new Timer(delay, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(-1.0f / view.getImage().getImage().getHeight());
            }
        });

        moveUpData.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                JButton btn2 = (JButton) e.getSource();
                ButtonModel model = btn2.getModel();
                if (model.isPressed() && !moveUPTimer.isRunning()) {
                    moveUPTimer.start();
                } else if (!model.isPressed() && moveUPTimer.isRunning()) {
                    moveUPTimer.stop();
                }

            }
        });

        moveDownData.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                JButton btn2 = (JButton) e.getSource();
                ButtonModel model = btn2.getModel();
                if (model.isPressed() && !moveDownTimer.isRunning()) {
                    moveDownTimer.start();
                } else if (!model.isPressed() && moveDownTimer.isRunning()) {
                    moveDownTimer.stop();
                }

            }
        });
        moveUpData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(1.0f / view.getImage().getImage().getHeight());

            }
        });

        moveDownData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(-1.0f / view.getImage().getImage().getHeight());

            }
        });

        saveData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    saveData();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    }

    return controls;
}

From source file:com.joey.software.regionSelectionToolkit.controlers.ImageProfileTool.java

public JPanel getControls() {
    if (controls == null) {
        controls = new JPanel(new BorderLayout());

        JPanel dirButton = new JPanel(new GridLayout(1, 2));
        dirButton.add(moveUpData);//  www  .  jav a  2  s . c  o m
        dirButton.add(moveDownData);

        JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
        buttonPanel.add(showFlattenedButton);
        buttonPanel.add(showAScanButton);
        buttonPanel.add(estimageSurface);

        JPanel pointsPanel = new JPanel(new BorderLayout());
        pointsPanel.add(saveData, BorderLayout.SOUTH);
        pointsPanel.add(axisToggle, BorderLayout.WEST);
        pointsPanel.add(numPoints, BorderLayout.CENTER);
        pointsPanel.add(updatePoints, BorderLayout.EAST);
        pointsPanel.add(buttonPanel, BorderLayout.NORTH);

        JPanel offsetPane = new JPanel(new BorderLayout());
        offsetPane.add(showOffset, BorderLayout.WEST);
        offsetPane.add(offset, BorderLayout.CENTER);

        JPanel temp = new JPanel(new BorderLayout());
        temp.add(offsetPane, BorderLayout.SOUTH);
        temp.add(transparance, BorderLayout.NORTH);

        JPanel toolPanel = new JPanel(new BorderLayout());
        toolPanel.add(temp, BorderLayout.NORTH);
        toolPanel.add(pointsPanel, BorderLayout.CENTER);
        toolPanel.add(dirButton, BorderLayout.SOUTH);

        controls.add(toolPanel, BorderLayout.NORTH);

        estimageSurface.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                estimateSurface();

            }
        });
        showAScanButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                showAScanPanel();
            }
        });
        axisToggle.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (axis == AXIS_X) {
                    setAxis(AXIS_Y);
                    axisToggle.setText("Y Axis");
                } else {
                    setAxis(AXIS_X);
                    axisToggle.setText("X Axis");
                }
                panel.repaint();
            }
        });
        transparance.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                panel.repaint();

            }
        });
        showOffset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                panel.repaint();
            }
        });

        offset.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                panel.shapeChanged();
                panel.repaint();
                updatePlotPanel();
            }

        });
        showFlattenedButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                showFlattenedImage();
            }
        });
        updatePoints.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setDataPoints((Integer) numPoints.getValue());
            }
        });

        moveUPTimer = new Timer(delay, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(1.0f / view.getImage().getHeight());
            }
        });

        moveDownTimer = new Timer(delay, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(-1.0f / view.getImage().getHeight());
            }
        });

        moveUpData.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                JButton btn2 = (JButton) e.getSource();
                ButtonModel model = btn2.getModel();
                if (model.isPressed() && !moveUPTimer.isRunning()) {
                    moveUPTimer.start();
                } else if (!model.isPressed() && moveUPTimer.isRunning()) {
                    moveUPTimer.stop();
                }

            }
        });

        moveDownData.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                JButton btn2 = (JButton) e.getSource();
                ButtonModel model = btn2.getModel();
                if (model.isPressed() && !moveDownTimer.isRunning()) {
                    moveDownTimer.start();
                } else if (!model.isPressed() && moveDownTimer.isRunning()) {
                    moveDownTimer.stop();
                }

            }
        });
        moveUpData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(1.0f / view.getImage().getHeight());

            }
        });

        moveDownData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addValue(-1.0f / view.getImage().getHeight());

            }
        });

        saveData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    saveData();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    }

    return controls;
}