Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 12:33 PM
Member
 
Join Date: Feb 2010
Posts: 1
Rep Power: 0
HellRider is on a distinguished road
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 .

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);
  }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-09-2010, 03:35 PM
Senior Member
 
Join Date: Aug 2009
Location: Pittsburgh, PA
Posts: 266
Rep Power: 2
zweibieren is on a distinguished road
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:
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
            }
        }
    });
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] CheckBox boss NetBeans 3 01-17-2010 04:51 AM
CheckBox in JTable nikosa AWT / Swing 1 08-05-2009 05:01 AM
Jtree with checkbox msankar.ravi AWT / Swing 1 07-09-2009 11:25 PM
Checkbox to list msixty New To Java 5 05-07-2009 08:10 PM
checkbox Alan AWT / Swing 3 05-18-2007 10:10 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:37 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org