Results 1 to 2 of 2
  1. #1
    HellRider is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default checkbox question

    hi all, I would like to ask how to make the following code , so when the button is pressed to display which checkbox are selected, thank you very much :).

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.Border;
    
    public class ACheckBox {
      public static void main(String args[]) {
        String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
        JFrame frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridLayout(0, 1));
        Border border = BorderFactory.createTitledBorder("Pizza Toppings");
        panel.setBorder(border);
        JCheckBox check = new JCheckBox("Anchovies");
        panel.add(check);
        check = new JCheckBox("Garlic");
        panel.add(check);
        check = new JCheckBox("Onions");
        panel.add(check);
        check = new JCheckBox("Pepperoni");
        panel.add(check);
        check = new JCheckBox("Spinach");
        panel.add(check);
        JButton button = new JButton("Submit");
        Container contentPane = frame.getContentPane();
        contentPane.add(panel, BorderLayout.CENTER);
        contentPane.add(button, BorderLayout.SOUTH);
        frame.setSize(300, 200);
        frame.setVisible(true);
      }
    }

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    285
    Rep Power
    11

    Default

    You will want an ItemListener.
    The tutorial How to use Checkboxes hints at this, but is not clear.
    Here is how to do it with an anonymous listener:
    Java Code:
        check = new JCheckBox("Spinach");
        panel.add(check);
        check.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.DESELECTED) {
                    // "Spinach" has been clicked off, process it
                }
                else { 
                    // "Spinach" has been clicked ON, process it
                }
            }
        });

Similar Threads

  1. CheckBox
    By boss in forum NetBeans
    Replies: 3
    Last Post: 01-17-2010, 05:51 AM
  2. CheckBox in JTable
    By nikosa in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2009, 06:01 AM
  3. Jtree with checkbox
    By msankar.ravi in forum AWT / Swing
    Replies: 1
    Last Post: 07-10-2009, 12:25 AM
  4. Checkbox to list
    By msixty in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 09:10 PM
  5. checkbox
    By Alan in forum AWT / Swing
    Replies: 3
    Last Post: 05-18-2007, 11:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •