Java AWT checkbox example program: This simple source code is to create a checkbox which is used to perform an usual multi selection functions. This program can be designed with the use of graphics class functions. This project is used by various degree students like engineering, MCA & all kind of IT professionals. Also this can be used as LAB program. A tutorial code is given below.
CHECKBOX IN JAVA AWT
Normally checkbox is used to select multiple options at a time. In cases like “on” or “off” & “true” or “false” people have an interest to go for a checkbox option! Because combo box will select only one at a time but in a checkbox we can select multiple options at once.
Java AWT checkbox Tutorial code:
Q: How to use checkbox in Java AWT? Explain with an example.
//Import statements import java.awt.*; import java.awt.event.*; //Class definition class CheckBo extends Frame implements ItemListener { //Variables declaration Checkbox ch1, ch2, ch3, ch4; Panel pa1; String mg; public CheckBo(String title) { super(title); mg=""; pa1=new Panel(); ch1=new Checkbox("Windows 7");//Object creation ch2=new Checkbox("Linux Ubuntu"); ch3=new Checkbox("Android"); ch4=new Checkbox("MacOS"); ch1.addItemListener(this); ch2.addItemListener(this); ch3.addItemListener(this); ch4.addItemListener(this); pa1.setLayout(new GridLayout(2,2)); pa1.add(ch1); pa1.add(ch2); pa1.add(ch3); pa1.add(ch4); add("South",pa1); addWindowListener(new MyWindowAdapter()); setSize(300,300); show(); } // © http://students3k.com – Karthikh Venkat public static void main(String args[])//Main method { CheckBo c=new CheckBo("CheckBoxes"); } public void itemStateChanged(ItemEvent e) { repaint(); } public void paint(Graphics g) { msg="Windows 7: "+c1.getState(); g.drawString(msg,30,60); msg="Linux Ubuntu: "+c2.getState(); g.drawString(msg,30,70); msg="Android: "+c3.getState(); g.drawString(msg,30,80); msg="Mac OS: "+c4.getState(); g.drawString(msg,30,90); } } // © http://students3k.com – Karthikh Venkat class MyWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } }
[Useful: Learn English Quickly] & [Career Guidance for All]