Checkbox and List Example


import java.awt.*;
import java.awt.event.*;

class MyChoiceCheckBoxExample extends MasterAWTFrame implements ItemListener
{
    private Checkbox cbbold, cbitalic, cbred, cbblue, cbcyan, cbgray;
    private Choice ch1;
    private CheckboxGroup chg;
    private TextArea ta;
    private Font f;
    private List mylist, myfsize;
    private int style = Font.PLAIN;  private String fname =Font.SANS_SERIF;
    private int fontsize = 12;
    private String sstr="";
    private Canvas drawCan;
    MyChoiceCheckBoxExample() {
       
        cbbold = new Checkbox("Bold");
        cbitalic = new Checkbox("Italic");
        chg = new CheckboxGroup();
       
        cbred = new Checkbox("Blue", chg, true);
        cbgray = new Checkbox("Gray", chg, false);
        cbcyan = new Checkbox("Cyan", chg, false);
       
        drawCan = new Canvas(); drawCan.setBackground(Color.MAGENTA);
       
        ta = new TextArea("ISTAR Institute VVNAGAR , ANAND, GUJARAT", 20, 100);
        ta.setFont(new Font(fname,style,18));
       
        mylist = new List(15);
        mylist.addItemListener(this);
        mylist.add(Font.MONOSPACED); mylist.add(Font.SERIF);
     //   mylist.add("Arial"); mylist.add("Times New Roman");
       // getMySystemFont(mylist);
       
        myfsize = new List(7); myfsize.add("10"); myfsize.add("12"); myfsize.add("14");
        myfsize.add("18"); myfsize.add("22");  myfsize.add("24"); myfsize.add("30");
        myfsize.addItemListener(this);
       
       // mylist.add("Arial"); mylist.add("Times New Roman"); mylist.add("Calibri");
        //mylist.add(Font.SANS_SERIF); mylist.add(Font.SERIF);
        //mylist.add("Bookman Old Style"); mylist.add("Courier New");
        p1.add(cbbold);  p1.add(cbitalic);
       
        p1.add(drawCan);
       
       
             
        p1.add(cbred); p1.add(cbgray); p1.add(cbcyan);
       
        p2.add(mylist); p2.add(myfsize); p2.add(ta);
       
       
       
        mylist.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ta.setText(String.valueOf(mylist.getSelectedIndex()));
            }
        } );
       
     
       
        cbbold.addItemListener(this);
        cbitalic.addItemListener(this);
               
        cbred.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange()== ItemEvent.SELECTED)
                    {
                        p1.setBackground(Color.blue);
                    }
            }
        });
       
        cbgray.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                 
                if (e.getSource().equals(cbgray))
                {
                    p1.setBackground(Color.gray);
                }
            }
        });
       
        cbcyan.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange()== ItemEvent.SELECTED)
                    {
                        p1.setBackground(Color.cyan);
                    }
            }
        });
   
    }
   
   
    public void paint(Graphics g)
    {
            g.drawString("Hello World", 120,200);
            g.setFont(f);
           
    }
    public void getMySystemFont(List mylist)
    {
           
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String strfont[] = ge.getAvailableFontFamilyNames();
       
        for (String str: strfont) { mylist.add(str);
        }
       
    }
   
        //item state changed method
       
        public void itemStateChanged(ItemEvent e) {
           
          // int style = Font.PLAIN;
           
          if (e.getSource() instanceof Checkbox)
          {
            if (e.getSource().equals(cbbold))
            {
                if (e.getStateChange()== ItemEvent.SELECTED)
                    {
                       
                            style +=Font.BOLD;
                    }
                if (e.getStateChange()==ItemEvent.DESELECTED)
                    {
                        style -=Font.BOLD;
                    }  
            } // outer if BOLD
           
           else if (e.getSource().equals(cbitalic)) {
                 if (e.getStateChange()== ItemEvent.SELECTED)
                    {
                        style += Font.ITALIC;
                    }
                if (e.getStateChange()==ItemEvent.DESELECTED)
                    {
                        style -=Font.ITALIC;
                    }      
               
            } // outer if ITALIC
           
           
           
           
          } // instance of Checkbox
           
               if (e.getSource() instanceof List )
               {
               
                 if (e.getSource().equals(mylist))
                 {
                    fname=mylist.getSelectedItem();
                     //System.out.println(mylist.getSelectedItem());

                  }
               
                 if (e.getSource().equals(myfsize))
                 {
                     fontsize = Integer.parseInt(myfsize.getSelectedItem());
                 
                 }
                }
               //System.out.println("fname :"+ fname);
               f = new Font(fname,style ,fontsize);
               ta.setFont(f);
               repaint();
               System.out.println("f :" + f.getName());
               System.out.println("font family : "+ f.getFamily());
               System.out.println("font : "+ f.getFontName());
       
       
        } // end of method
       
   
} // end of class
   
   

public class ChoiceMainClass {

    public static void main(String[] args) {
   
        new MyChoiceCheckBoxExample();//.getMySystemFont();
       
       
    }
}

Comments

Popular posts from this blog

Custom Exception example for BankAccount

HashMap HashSet example

Annotation