Results 1 to 6 of 6

Thread: ButtonModel interface of java

  1. #1
    Join Date
    Jan 2010
    Posts
    99

    ButtonModel interface of java

    Hi All,

    I am beginner to the java programming. I want to be ware about the ButtonModel interface of java. I don't know anything about the constructors and method of ButtonModel interface of java. I have referred manu java books but nothing was helpful. I wonder if you are able let me know something about ButtonModel interface of java. I will be great if you provide proper example for the ButtonModel interface.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: ButtonModel interface of java

    A ButtonModel interface is a state Model for buttons. This model is used for check boxes and radio buttons, which are special kinds of buttons, as well as for normal buttons. For check boxes and radio buttons, pressing the mouse selects the button. For normal buttons, pressing the mouse "arms" the button. Releasing the mouse over the button then initiates a button press, firing its action event. Releasing the mouse elsewhere disarms the button.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: ButtonModel interface of java

    Hi,

    Please review following example of ButtonModel interface of java:
    Code:
    import javax.swing.ButtonModel;
    import javax.swing.JFrame;
    import javax.swing.JToggleButton;
    
    public class ButtonModelMain {
    
      public static void main(String[] agr) {
        JFrame frame01 = new JFrame();
        frame01.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        JToggleButton tgb = new JToggleButton("Selected");
    
        ActionListener actionListener01 = new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
          }    };
    
        ItemListener itemListener01 = new ItemListener() {
          public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
              System.out.println("Selected");
            } else {
              System.out.println("Deselected");
            } } };
    
        tgb.addActionListener(actionListener01);
        tgb.addChangeListener(changeListener);
        frame01.add(tgb, BorderLayout.NORTH);
        JToggleButton tgb2 = new JToggleButton("Focused");
        frame01.add(tgb2, BorderLayout.CENTER);
        frame01.setSize(300, 225);
        frame01.setVisible(true);
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: ButtonModel interface of java

    Following are some available methods of ButtonModel interface:
    • setActionCommand(String strg)
    • removeItemListener(ItemListener ltmlstner)
    • removeChangeListener(ChangeListener chngln)
    • removeActionListener(ActionListener actln)
    • addItemListener(ItemListener itmln)

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: ButtonModel interface of java

    While using ButtonModel interface it is essential to invoke method known as 'setSelected(boolean)'. That is when a click of mouse occurs over the radio button. It will call method setArmed(boolean) only when the mouse is pressed button. A superinterface of 'ButtonModel interface' is nothing but 'ItemSelectable'. This interface implements all the classes of the 'DefaultButtonModel' class of java.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: ButtonModel interface of java

    In below java example I have used 'isPressed()' method of 'ButtonModel interface', please study it carefully:
    Code:
    import javax.swing.ButtonModel;
    
    public class BMMainClass {
    
      public static void main(String[] avnd) {
        final String DL = "Deselected";
        final String SL = "Selected";
        
        JFrame FL = new JFrame("Selecting CheckBox");
        FL.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JCheckBox checkBox01 = new JCheckBox(DL);
       }    }
    
        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);
          }    }    }
    
        checkBox01.addActionListener(actionListener);
        checkBox01.addChangeListener(changeListener);
        checkBox01.addItemListener(itemListener);
        checkBox01.setMnemonic(KeyEvent.VK_S);
        FL.add(checkBox01, BorderLayout.NORTH);
        FL.setSize(300, 100);
        FL.setVisible(true);
      }
    }

Similar Threads

  1. what is interface in java
    By Ansari Bros in forum Software Development
    Replies: 4
    Last Post: 10-01-2011, 10:07 AM
  2. CharSequence interface problem in Java
    By Feng in forum Software Development
    Replies: 7
    Last Post: 06-08-2010, 06:18 AM
  3. Shape interface of java
    By Gajananvihari in forum Software Development
    Replies: 4
    Last Post: 09-03-2010, 05:08 PM
  4. SpinnerModel interface of java
    By Gadin in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 12:56 PM
  5. Action Interface of java
    By Protectors in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 08:19 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,566,734.04669 seconds with 17 queries