- import javax.swing.JFrame;
- import javax.swing.JCheckBox;
- import javax.swing.JButton;
- import javax.swing.JOptionPane;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class GetSelectedJCheckBox implements ActionListener
- {
- //Create String that will be use to hold selected JCheckBox text
- String selectedCheckBox="";
- //Create check box using JCheckBox
- JCheckBox checkBox1=new JCheckBox("Duck");
- JCheckBox checkBox2=new JCheckBox("Chicken");
- JCheckBox checkBox3=new JCheckBox("Cow");
- JCheckBox checkBox4=new JCheckBox("Sheep");
- //Create button using JButton with text ( Get selected JCheckBox )
- JButton button=new JButton("Get selected JCheckBox");
- //method actionPerformed that we override because we implement ActionListener
- public void actionPerformed(ActionEvent event)
- {
- //Action for button
- if(event.getSource()==button)
- {
- //Check first JCheckBox is selected or not
- if(checkBox1.isSelected())
- {
- selectedCheckBox=selectedCheckBox+checkBox1.getText()+"\n";
- }
- //Check second JCheckBox is selected or not
- if(checkBox2.isSelected())
- {
- selectedCheckBox=selectedCheckBox+checkBox2.getText()+"\n";
- }
- //Check third JCheckBox is selected or not
- if(checkBox3.isSelected())
- {
- selectedCheckBox=selectedCheckBox+checkBox3.getText()+"\n";
- }
- //Check fourth JCheckBox is selected or not
- if(checkBox4.isSelected())
- {
- selectedCheckBox=selectedCheckBox+checkBox4.getText()+"\n";
- }
- //Show message that tell you, what check box that you selected
- JOptionPane.showMessageDialog(null,"Selected check box is : \n"+selectedCheckBox);
- //Make selectedCheckBox string like initial with empty string
- selectedCheckBox=new String("");
- }
- }
- //Constructor
- public GetSelectedJCheckBox()
- {
- //Create a window using JFrame with title ( Get selected JCheckBox )
- JFrame frame=new JFrame("Get selected JCheckBox");
- //Create layout that will be use by JFrame
- GridLayout gl=new GridLayout(5,1);
- //Add ActionListener to button
- button.addActionListener(this);
- //Set JFrame layout
- frame.setLayout(gl);
- //Add all created check box into JFrame
- frame.add(checkBox1);
- frame.add(checkBox2);
- frame.add(checkBox3);
- frame.add(checkBox4);
- //Add created button into JFrame
- frame.add(button);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(350,200);
- frame.setVisible(true);
- }
- //Main method
- public static void main(String[]args)
- {
- GetSelectedJCheckBox gsjcb=new GetSelectedJCheckBox();
- }
- }
Saturday, 27 July 2013
Categories: JAVA
Subscribe to:
Post Comments (Atom)
Blogger templates
Popular Posts
-
500 topics for projects. IT engineering Final year projects and mini projects,Computer Science & Engineering final year project idea Hi ...
-
If you want to block tough proxies like hidemyass.com, my previously posted .htaccess methods won’t work. Those methods will block quite...
-
Create your own API with Restler By Richard Askew on July 17, 2013 | 1 comment ...
-
60+ Awesome New WordPress Themes Blog Post In: http://www.webdesignerdepot.com It seems like every time you go looking for a new WordPress ...
-
There are many ways to encode and decode PHP code. From the perspective of site security, there are three PHP functions — str_rot13() , b...
-
Ajax-Powered Error Logs Update: Check out the new and improved Ajax Error Log Version 2.0! As an obsessive website administrator, I like to...
-
PHP Tip: Encode & Decode Data URLs Converting small images to data-URLs is a great way to eliminate HTTP requests and decrease loading...
-
40 useful APIs for web designers and developers An application programming interface (API) is a set of rules and specifications that softwar...
-
Computer Engineering: HTTP Headers for ZIP File Downloads : HTTP Headers for ZIP File Downloads You know when you you’re working on a proj...
-
Enterprise Resource Planning Tutorial : Download Here
0 comments:
Post a Comment