How to handle with Jtextfield,JButton,JRadioButton,JList and MouseEvents


 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Assignment extends JFrame implements ItemListener,ActionListener
{
 JButton b1;
 JButton b2;
 JButton b3;
 JButton b4;
 JButton b5;
 JButton b6;
 Object o;
 CheckboxGroup ch;
 Checkbox c1;
 Checkbox c2;
 JTextField jt;
 Label lb;
 JList jl1;
 JList jl2;
 JScrollPane js1;
 JScrollPane js2;
 DefaultListModel listModel1;
 DefaultListModel listModel2;
 public Assignment()
 {
  Container c = getContentPane();
  listModel1 = new DefaultListModel();
  listModel2 = new DefaultListModel();
  c.setLayout(null);
  b1 = new JButton(">");
  b2 = new JButton(">>");
  b3 = new JButton("<");
  b4 = new JButton("<<");
  b5 = new JButton("clear");
  b6 = new JButton("clear");
  ch = new CheckboxGroup();
  c1 = new Checkbox("Indian",ch,false);
  c2 = new Checkbox("Non Indian",ch,false);
  lb = new Label("Name");
  jt = new JTextField("Your name here",30);
  jl1 = new JList(listModel1);
  jl2 = new JList(listModel2);
  js1 = new JScrollPane(jl1);
  js2 = new JScrollPane(jl2);
  c.add(b1);
  c.add(b2); 
  c.add(b3);
  c.add(b4);
  c.add(b5);
  c.add(b6); 
  c.add(c1);
  c.add(c2);
  c.add(lb);
  c.add(jt);
  c.add(jl1);
  c.add(jl2);
  c.add(js1);
  c.add(js2);
  c1.addItemListener(this);
  c2.addItemListener(this);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
  b4.addActionListener(this);
  b5.addActionListener(this);
  b6.addActionListener(this);
  lb.setBounds(250,50,50,20);
  jt.setBounds(300,50,200,20);
  c1.setBounds(300,100,70,40);
  c2.setBounds(400,100,80,40);
  jl1.setBounds(100,200,150,200);
  jl2.setBounds(550,200,150,200);
  b1.setBounds(350,200,100,20);
  b2.setBounds(350,250,100,20);
  b3.setBounds(350,300,100,20);
  b4.setBounds(350,350,100,20);
  b5.setBounds(130,450,100,30);
  b6.setBounds(580,450,100,30);
 }
 public static void main(String s[])
 {
  Assignment a = new Assignment();
  a.setTitle("Assignment");
  a.setBounds(100,100,800,800);
  a.show();
 }
 /*class CheckboxHandler implements ItemListener
 {*/
  public void itemStateChanged(ItemEvent e)
  {
   if(e.getSource()==c1)
   {
    String s;
    s=jt.getText();
    listModel1.addElement(s);
   }
   if(e.getSource()==c2)
   {
    String s1;
    s1=jt.getText();
    listModel2.addElement(s1);
   }
  }
  public void actionPerformed(ActionEvent e)
  {  
   if(e.getSource()==b1)
   {
  
    listModel2.addElement(jl1.getSelectedValue());
    listModel1.removeElement(jl1.getSelectedValue());
   }
   if(e.getSource()==b2)
   {
    String s1;
       int t = jl1.getLastVisibleIndex();
     
       for (int i = 0; i <= t; i++)
       {
           o = jl1.getModel().getElementAt(i);
           s1 = o.toString();

           listModel2.addElement(s1);
       }
      listModel1.clear();
    
   }
   if(e.getSource()==b4)
   {
    String s1;
    int t = jl2.getLastVisibleIndex();
    for(int i=0;i<=t;i++)
    {
     o=jl2.getModel().getElementAt(i);
     s1=o.toString();
     listModel1.addElement(s1);
    }
   listModel2.clear();
   }
   if(e.getSource()==b3)
   {
    listModel1.addElement(jl2.getSelectedValue());
    listModel2.removeElement(jl2.getSelectedValue());
   }
   if(e.getSource()==b5)
    listModel1.clear();
   if(e.getSource()==b6)
    listModel2.clear();
  }
  //}
 //}
  
}

Related product you might see:

Share this product :

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. TECHBEAN stars - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger