Example usage for javax.swing AbstractButton getModel

List of usage examples for javax.swing AbstractButton getModel

Introduction

In this page you can find the example usage for javax.swing AbstractButton 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[] 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);
        }//from w ww. j  a  va2s  .  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) {
            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:RadioButtonSample.java

public static void main(String args[]) {

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            boolean selected = aButton.getModel().isSelected();
            System.out.println(actionEvent.getActionCommand() + " - selected? " + selected);
        }/* ww w.  j a va  2s  . co m*/
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton aButton = (AbstractButton) itemEvent.getSource();
            int state = itemEvent.getStateChange();
            String selected = ((state == ItemEvent.SELECTED) ? "selected" : "not selected");
            System.out.println(aButton.getText() + " - selected? " + selected);
        }
    };

    JFrame frame = new JFrame("Radio Menu Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    ButtonGroup buttonGroup = new ButtonGroup();
    menu.setMnemonic(KeyEvent.VK_M);

    JRadioButtonMenuItem emptyMenuItem = new JRadioButtonMenuItem();
    emptyMenuItem.setActionCommand("Empty");
    emptyMenuItem.addActionListener(actionListener);
    buttonGroup.add(emptyMenuItem);
    menu.add(emptyMenuItem);

    JRadioButtonMenuItem oneMenuItem = new JRadioButtonMenuItem("Partridge");
    oneMenuItem.addActionListener(actionListener);
    buttonGroup.add(oneMenuItem);
    menu.add(oneMenuItem);

    JRadioButtonMenuItem twoMenuItem = new JRadioButtonMenuItem("Turtle Doves", true);
    twoMenuItem.addActionListener(actionListener);
    buttonGroup.add(twoMenuItem);
    menu.add(twoMenuItem);

    JRadioButtonMenuItem threeMenuItem = new JRadioButtonMenuItem("French Hens", threeIcon);
    threeMenuItem.addItemListener(itemListener);
    buttonGroup.add(threeMenuItem);
    menu.add(threeMenuItem);

    JRadioButtonMenuItem fourMenuItem = new JRadioButtonMenuItem("Calling Birds", fourIcon, true);
    fourMenuItem.addActionListener(actionListener);
    buttonGroup.add(fourMenuItem);
    menu.add(fourMenuItem);

    JRadioButtonMenuItem fiveMenuItem = new JRadioButtonMenuItem(fiveIcon);
    fiveMenuItem.addActionListener(actionListener);
    fiveMenuItem.setActionCommand("Rings");
    buttonGroup.add(fiveMenuItem);
    menu.add(fiveMenuItem);

    JRadioButtonMenuItem sixMenuItem = new JRadioButtonMenuItem(sixIcon, true);
    sixMenuItem.addActionListener(actionListener);
    sixMenuItem.setActionCommand("Geese");
    buttonGroup.add(sixMenuItem);
    menu.add(sixMenuItem);

    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:CheckBoxSample.java

public static void main(String args[]) {

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            String newLabel;//from w  ww  .ja  va  2  s. c  o m
            Icon newIcon;
            if (selected) {
                newLabel = "Girl";
                newIcon = girlIcon;
            } else {
                newLabel = "Boy";
                newIcon = boyIcon;
            }
            aButton.setText(newLabel);
            aButton.setIcon(newIcon);
        }
    };

    ItemListener iListener = new ItemListener() {
        public void itemStateChanged(ItemEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            int state = event.getStateChange();
            String newLabel;
            Icon newIcon;
            if (state == ItemEvent.SELECTED) {
                newLabel = "Girl";
                newIcon = girlIcon;
            } else {
                newLabel = "Boy";
                newIcon = boyIcon;
            }
            aButton.setText(newLabel);
            aButton.setIcon(newIcon);
        }
    };

    JFrame frame = new JFrame("CheckBox Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.setMnemonic(KeyEvent.VK_M);
    JCheckBoxMenuItem one = new JCheckBoxMenuItem();
    menu.add(one);
    JCheckBoxMenuItem two = new JCheckBoxMenuItem("Boy");
    menu.add(two);
    JCheckBoxMenuItem three = new JCheckBoxMenuItem(boyIcon);
    menu.add(three);
    JCheckBoxMenuItem four = new JCheckBoxMenuItem("Girl", true);
    menu.add(four);
    JCheckBoxMenuItem five = new JCheckBoxMenuItem("Boy", boyIcon);
    five.addItemListener(iListener);
    menu.add(five);
    Icon stateIcon = new DiamondAbstractButtonStateIcon(Color.black);

    UIManager.put("CheckBoxMenuItem.checkIcon", stateIcon);
    JCheckBoxMenuItem six = new JCheckBoxMenuItem("Girl", girlIcon, true);
    six.addActionListener(aListener);
    menu.add(six);

    bar.add(menu);
    frame.setJMenuBar(bar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:FindingAButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);//from   ww  w.  ja v a 2  s  .c om
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
    group.setSelected(abstract1.getModel(), true);
    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
        AbstractButton button = (AbstractButton) elements.nextElement();
        if (button.isSelected()) {
            System.out.println("The winner is: " + button.getText());
        }
    }
}

From source file:SettingSelectedAButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);/*from   w  w  w . j a v  a 2s.c  o  m*/
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);

    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);

    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);

    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);

    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
    group.setSelected(abstract1.getModel(), true);

    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
        AbstractButton button = (AbstractButton) elements.nextElement();
        if (button.isSelected()) {
            System.out.println("The winner is: " + button.getText());
        }
    }
}

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());
        }//w w w. ja  va 2s.com
    };
    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:JXButtonPanel.java

private static ButtonGroup getButtonGroup(AbstractButton button) {
    ButtonModel model = button.getModel();
    if (model instanceof DefaultButtonModel) {
        return ((DefaultButtonModel) model).getGroup();
    }//from   www  .  j a v  a  2 s.  c  o m
    return null;
}

From source file:CheckBoxIcon.java

public void paintIcon(Component component, Graphics g, int x, int y) {
    AbstractButton abstractButton = (AbstractButton) component;
    ButtonModel buttonModel = abstractButton.getModel();

    Color color = buttonModel.isSelected() ? Color.BLUE : Color.RED;
    g.setColor(color);//from w  w w  .  j  a v a 2  s. c o m

    g.drawRect(1, 1, 20, 20);

}

From source file:MyCheckBoxUI.java

public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    ButtonModel model = b.getModel();
    Dimension d = b.getSize();//from  w  ww .  ja  v a2 s. co  m

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.white);
    g.drawString("Am I a check box", 10, 10);

}

From source file:MainFrame.MainFrame.java

public MainFrame() {
    //Create menu
    this.menuBar = new JMenuBar();
    this.menuProxy = new JMenu("Proxy");
    this.menuBar.add(menuProxy);
    this.cbMenuItem = new JCheckBoxMenuItem("? proxy");
    this.cbMenuItem.setMnemonic(KeyEvent.VK_C);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            if (selected) {
                SingleDataHolder.getInstance().isProxyActivated = true;
                SingleDataHolder.getInstance().proxyIpAdress = MainFrame.this.proxyIpAdress.getText();
                SingleDataHolder.getInstance().proxyPort = Integer.parseInt(MainFrame.this.proxyPort.getText());
                SingleDataHolder.getInstance().proxyLogin = MainFrame.this.proxyLogin.getText();
                SingleDataHolder.getInstance().proxyPassword = String
                        .valueOf(MainFrame.this.proxyPassword.getPassword());
            } else {
                SingleDataHolder.getInstance().isProxyActivated = false;
            }//w ww.  jav  a 2s .c o  m
        }
    };
    this.cbMenuItem.addActionListener(aListener);

    this.proxyIpAdress = new JTextField();
    this.proxyPort = new JTextField();
    this.proxyLogin = new JTextField();
    ;
    this.proxyPassword = new JPasswordField();
    this.menuProxy.add(this.cbMenuItem);
    this.menuProxy.add(new JLabel("IP ?"));
    this.menuProxy.add(this.proxyIpAdress);
    this.menuProxy.add(new JLabel("? "));
    this.menuProxy.add(this.proxyPort);
    this.menuProxy.add(new JLabel(""));
    this.menuProxy.add(this.proxyLogin);
    this.menuProxy.add(new JLabel(""));
    this.menuProxy.add(this.proxyPassword);
    this.setJMenuBar(this.menuBar);

    communicator = new HttpCommunicator();
    myActionListener = new mainFrameActionListener();
    mainPanel = new JPanel(new BorderLayout());
    this.setContentPane(mainPanel);

    JPanel P = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 5));
    mainPanel.add(P, BorderLayout.NORTH);

    //Create Group label
    labelGroup = new JLabel("");
    P.add(labelGroup);
    //Create Group combo
    comboGroup = new JComboBox();
    comboGroup.addItem("?");
    comboGroup.addActionListener(myActionListener);
    P.add(comboGroup);

    //Create Date label
    labelDate = new JLabel("");
    P.add(labelDate);
    //Create Date combo
    comboDate = new JComboBox();
    comboDate.addItem("?");
    comboDate.addActionListener(myActionListener);
    P.add(comboDate);

    //Create Delete button
    buttonDelete = new JButton("");
    buttonDelete.setEnabled(false);
    buttonDelete.addActionListener(myActionListener);
    P.add(buttonDelete);

    //this.checkConnection = new CheckConnection(buttonDelete);

    buttonPasswordsManager = new JButton(" ");
    buttonPasswordsManager.addActionListener(myActionListener);
    P.add(buttonPasswordsManager);

    try {
        Image img = ImageIO.read(getClass().getResource("resources/table_refresh.png"));
        this.buttonRefresh = new JButton(new ImageIcon(img));
        this.buttonRefresh.addActionListener(myActionListener);
        this.buttonRefresh.setToolTipText(" ");
        P.add(buttonRefresh);
    } catch (IOException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    //CreateLessonTableModel
    lessonTableModel = new LessonTableModel();

    try {
        communicator.setCombos(comboGroup, comboDate, lessonTableModel);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, " ? ? .");
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    //CreateTable
    table = new JTable(lessonTableModel);
    table.setFillsViewportHeight(true);
    table.setAutoCreateRowSorter(true);

    JScrollPane scrollP = new JScrollPane(table);
    mainPanel.add(scrollP, BorderLayout.CENTER);
    //Create Table sorter
    sorter = new TableRowSorter<LessonTableModel>(lessonTableModel);
    table.setRowSorter(sorter);
    table.getModel().addTableModelListener(new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            TableModel model = (TableModel) e.getSource();
            for (int i = 0; i < model.getRowCount(); i++) {
                if ((boolean) model.getValueAt(i, 2) == true) {
                    buttonDelete.setEnabled(true);
                    return;
                }
            }
            buttonDelete.setEnabled(false);
        }
    });

    //Standart block
    this.setSize(700, 400);
    this.setTitle(" ");
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent we) {
            System.exit(0);
        }
    });
    try {
        Image img = ImageIO.read(getClass().getResource("resources/appIcon.png"));
        this.setIconImage(img);
    } catch (IOException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

    this.setVisible(true);
}