Example usage for javax.swing JButton getModel

List of usage examples for javax.swing JButton getModel

Introduction

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

Prototype

public ButtonModel getModel() 

Source Link

Document

Returns the model that this button represents.

Usage

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());
        }/* www .java 2s .  c  om*/
    });

    JOptionPane.showMessageDialog(null, button);
}

From source file:ButtonModelTesting.java

public static void main(String args[]) {
    JFrame f = new JFrame("Button Model Tester");
    JButton jb1 = new JButton("Hello");
    ButtonModel bm = jb1.getModel();
    JButton jb2 = new JButton("World");
    jb2.setModel(bm);// w  w  w  .  j a va 2  s . c o m
    Container c = f.getContentPane();
    c.add(jb1, BorderLayout.NORTH);
    c.add(jb2, BorderLayout.SOUTH);
    jb1.addActionListener(new MessageActionListener("Selected One"));
    jb2.addActionListener(new MessageActionListener("Selected Two"));
    f.pack();
    f.show();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    JButton jb1 = new JButton("Hello");
    ButtonModel bm = jb1.getModel();
    JButton jb2 = new JButton("World");
    jb2.setModel(bm);/* www .  j  a  v  a  2s.c o  m*/
    Container c = f.getContentPane();
    c.add(jb1, BorderLayout.NORTH);
    c.add(jb2, BorderLayout.SOUTH);
    jb1.addActionListener(new MessageActionListener("Selected One"));
    jb2.addActionListener(new MessageActionListener("Selected Two"));
    f.pack();
    f.show();
}

From source file:Main.java

public static void main(String[] args) {
    JButton button = new JButton("ok");
    button.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            DefaultButtonModel model = (DefaultButtonModel) button.getModel();
            if (model.isEnabled())
                System.out.println("Enabled: true");
            else/*from  ww w. j a v  a2 s  . c o m*/
                System.out.println("Enabled: false");

            if (model.isArmed())
                System.out.println("Armed: true");
            else
                System.out.println("Armed: false");

            if (model.isPressed())
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");
        }
    });
    JOptionPane.showMessageDialog(null, button);
}

From source file:ButtonModel.java

public static void main(String[] args) {
    final JButton ok = new JButton("ok");

    JCheckBox cb = new JCheckBox("Enabled", true);

    ok.setBounds(40, 30, 80, 25);//from   www.j  a  v a  2 s.com
    ok.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            DefaultButtonModel model = (DefaultButtonModel) ok.getModel();
            if (model.isEnabled())
                System.out.println("Enabled: true");
            else
                System.out.println("Enabled: false");

            if (model.isArmed())
                System.out.println("Armed: true");
            else
                System.out.println("Armed: false");

            if (model.isPressed())
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");
        }

    });

    cb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (ok.isEnabled())
                ok.setEnabled(false);
            else
                ok.setEnabled(true);
        }
    });

    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(ok);
    f.add(cb);
    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

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);//from  w  ww .  jav a  2  s .co  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:SortableTable.java

/**
 * Returns the renderer component./*from ww  w.  j  ava  2  s .c  o m*/
 *
 * @param table      the table.
 * @param value      the value.
 * @param isSelected selected?
 * @param hasFocus   focussed?
 * @param row        the row.
 * @param column     the column.
 * @return the renderer.
 */
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
        final boolean hasFocus, final int row, final int column) {

    if (table == null) {
        throw new NullPointerException("Table must not be null.");
    }

    final JComponent component;
    final SortableTableModel model = (SortableTableModel) table.getModel();
    final int cc = table.convertColumnIndexToModel(column);
    final boolean isSorting = (model.getSortingColumn() == cc);
    final boolean isAscending = model.isAscending();

    final JTableHeader header = table.getTableHeader();
    final boolean isPressed = (cc == this.pressedColumn);

    if (this.useLabels) {
        final JLabel label = getRendererLabel(isSorting, isAscending);
        label.setText((value == null) ? "" : value.toString());
        component = label;
    } else {
        final JButton button = getRendererButton(isSorting, isAscending);
        button.setText((value == null) ? "" : value.toString());
        button.getModel().setPressed(isPressed);
        button.getModel().setArmed(isPressed);
        component = button;
    }

    if (header != null) {
        component.setForeground(header.getForeground());
        component.setBackground(header.getBackground());
        component.setFont(header.getFont());
    }
    return component;
}

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);//w w  w  . j av a  2s  .  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;
}