The java program given below does the following tasks,
1) Button(<<) moves all items from list1 to list2.
2) Button(<) moves single item from list1 to list2.
3) Button(>>) moves all items from list2 to list1.
4)Button(>) moves single item from list2 to list1.
5)When 1 item is moved the next item should be highlighted automatically.
6)No list box should contain duplicate entries
7)Add & Remove button should work for their own list box & not for other
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Slip2_1 extends JFrame implements ActionListener
{
List list1,list2;
JButton b1,b2,b3,b4,b5,b6,b7,b8;
JTextField t1;
int i,j;
// String d;
public static void main(String args[])
{
Slip2_1 s = new Slip2_1();
}
// Container c=getContentPane();
public Slip2_1()
{
super("List add/remove item ");
setSize(550,450);
setLocation(100,100);
//setBounds(100,100,550,450);
setVisible(true);
setLayout(null);
list1 = new List();
list1.add("Ball");
list1.add("Bat");
list1.add("Stump");
list1.add("Pad");
list1.add("Gloves");
list1.add("Shoes");
list1.add("Helemet");
list1.add("Bells");
list1.add("Caps");
list1.add("Medicine");
list1.setBounds(75,100,90,200);
add(list1);
list2 = new List();
list2.add("Computer");
list2.add("Laptop");
list2.add("Camera");
list2.add("Veg");
list2.add("Non_Veg");
list2.add("Medicine");
list2.setBounds(350,100,100,200);
add(list2);
b1 =new JButton("<<");
b1.setBounds(225,100,75,25);
add(b1);
b2 =new JButton("<");
b2.setBounds(225,150,75,25);
add(b2);
b3 =new JButton(">>");
b3.setBounds(225,200,75,25);
add(b3);
b4 =new JButton(">");
b4.setBounds(225,250,75,25);
add(b4);
//list1
b5 =new JButton("Add");
b5.setBounds(25,350,75,25);
add(b5);
b6 =new JButton("Remove");
b6.setBounds(150,350,85,25);
add(b6);
//list2
b7 =new JButton("Add");
b7.setBounds(325,350,75,25);
add(b7);
b8 =new JButton("Remove");
b8.setBounds(425,350,85,25);
add(b8);
b1.addActionListener(this) ;
b2.addActionListener(this);
b3.addActionListener(this) ;
b4.addActionListener(this) ;
b5.addActionListener(this);
b6.addActionListener(this) ;
b7.addActionListener(this) ;
b8.addActionListener(this) ;
}//slip2_1 constructor
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1) // list1 << list2
{
// << copy all item from list1 to list2
int cnt1 = list1.getItemCount();
int cnt2 = list2.getItemCount();
for(int i=0;i<cnt1;i++)
{
int flag=0;
for(int j=0;j<cnt2;j++)
if(list1.getItem(i).equalsIgnoreCase(list2.getItem(j))==true)
{
flag=1;
break;
}
if(flag==0)
list2.add(list1.getItem(i));
}
list1.removeAll();
} // b1 list1 << list2
if(ae.getSource()==b3) // list2 >> list1
{
// copy all item from list2 to list1
int cnt1 = list1.getItemCount();
int cnt2 = list2.getItemCount();
for(int i=0;i<cnt2;i++)
{
int flag=0;
for(int j=0;j<cnt1;j++)
if(list1.getItem(j).equalsIgnoreCase(list2.getItem(i))==true)
{
flag=1;
break;
}
if(flag==0)
list1.add(list2.getItem(i));
}
list2.removeAll();
}// b3 list1 >> list2
String d;
if(ae.getSource()==b2) // list1 < list2
{
// add single selected item from list1 to list2
d =list1.getSelectedItem();
int cnt = list2.getItemCount();
for(i=0;i<cnt;i++)
{
if(list2.getItem(i).equals(d)) // d is selcted item from list1
return;
}
list2.add(d);
}// b2 list1 < list2
if(ae.getSource()==b4) // list1 > list2
{
// add single selected item from list2 to list1
d =list2.getSelectedItem();
int cnt = list1.getItemCount();
for(i=0;i<cnt;i++)
{
if(list1.getItem(i).equals(d)) // d is selcted item from list1
return;
}
list1.add(d);
}// b4 list1 > list2
if(ae.getSource()==b5)
{
String m = "Enter the item";
String result =JOptionPane.showInputDialog(m);
int cnti=0,cnt = list1.getItemCount();
if(result!=null)
if(result.equals("")!=true)
{
for(int i=0;i<cnt;i++)
{
if(result.equalsIgnoreCase(list1.getItem(i))==true)
{cnti=1; break;}
}
if(cnt==0 || cnti==0)
list1.add(result);
}
}
if(ae.getSource()==b6)
{
String m = "Enter the item";
String result = JOptionPane.showInputDialog(m);
list1.remove(result);
}
if(ae.getSource()==b7)
{
String res=JOptionPane.showInputDialog("Enter The Item");
int cnti=0,cnt = list2.getItemCount();
if(res!=null)
if(res.equals("")!=true)
{
for(int i=0;i<cnt;i++)
{
if(res.equalsIgnoreCase(list2.getItem(i))==true)
{cnti=1; break;}
}
if(cnt==0 || cnti==0)
list2.add(res);
}
}
if(ae.getSource()==b8)
{
String res= JOptionPane.showInputDialog("Enter the item");
list2.remove(res);
}
}//actionperformed
}
1) Button(<<) moves all items from list1 to list2.
2) Button(<) moves single item from list1 to list2.
3) Button(>>) moves all items from list2 to list1.
4)Button(>) moves single item from list2 to list1.
5)When 1 item is moved the next item should be highlighted automatically.
6)No list box should contain duplicate entries
7)Add & Remove button should work for their own list box & not for other
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Slip2_1 extends JFrame implements ActionListener
{
List list1,list2;
JButton b1,b2,b3,b4,b5,b6,b7,b8;
JTextField t1;
int i,j;
// String d;
public static void main(String args[])
{
Slip2_1 s = new Slip2_1();
}
// Container c=getContentPane();
public Slip2_1()
{
super("List add/remove item ");
setSize(550,450);
setLocation(100,100);
//setBounds(100,100,550,450);
setVisible(true);
setLayout(null);
list1 = new List();
list1.add("Ball");
list1.add("Bat");
list1.add("Stump");
list1.add("Pad");
list1.add("Gloves");
list1.add("Shoes");
list1.add("Helemet");
list1.add("Bells");
list1.add("Caps");
list1.add("Medicine");
list1.setBounds(75,100,90,200);
add(list1);
list2 = new List();
list2.add("Computer");
list2.add("Laptop");
list2.add("Camera");
list2.add("Veg");
list2.add("Non_Veg");
list2.add("Medicine");
list2.setBounds(350,100,100,200);
add(list2);
b1 =new JButton("<<");
b1.setBounds(225,100,75,25);
add(b1);
b2 =new JButton("<");
b2.setBounds(225,150,75,25);
add(b2);
b3 =new JButton(">>");
b3.setBounds(225,200,75,25);
add(b3);
b4 =new JButton(">");
b4.setBounds(225,250,75,25);
add(b4);
//list1
b5 =new JButton("Add");
b5.setBounds(25,350,75,25);
add(b5);
b6 =new JButton("Remove");
b6.setBounds(150,350,85,25);
add(b6);
//list2
b7 =new JButton("Add");
b7.setBounds(325,350,75,25);
add(b7);
b8 =new JButton("Remove");
b8.setBounds(425,350,85,25);
add(b8);
b1.addActionListener(this) ;
b2.addActionListener(this);
b3.addActionListener(this) ;
b4.addActionListener(this) ;
b5.addActionListener(this);
b6.addActionListener(this) ;
b7.addActionListener(this) ;
b8.addActionListener(this) ;
}//slip2_1 constructor
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1) // list1 << list2
{
// << copy all item from list1 to list2
int cnt1 = list1.getItemCount();
int cnt2 = list2.getItemCount();
for(int i=0;i<cnt1;i++)
{
int flag=0;
for(int j=0;j<cnt2;j++)
if(list1.getItem(i).equalsIgnoreCase(list2.getItem(j))==true)
{
flag=1;
break;
}
if(flag==0)
list2.add(list1.getItem(i));
}
list1.removeAll();
} // b1 list1 << list2
if(ae.getSource()==b3) // list2 >> list1
{
// copy all item from list2 to list1
int cnt1 = list1.getItemCount();
int cnt2 = list2.getItemCount();
for(int i=0;i<cnt2;i++)
{
int flag=0;
for(int j=0;j<cnt1;j++)
if(list1.getItem(j).equalsIgnoreCase(list2.getItem(i))==true)
{
flag=1;
break;
}
if(flag==0)
list1.add(list2.getItem(i));
}
list2.removeAll();
}// b3 list1 >> list2
String d;
if(ae.getSource()==b2) // list1 < list2
{
// add single selected item from list1 to list2
d =list1.getSelectedItem();
int cnt = list2.getItemCount();
for(i=0;i<cnt;i++)
{
if(list2.getItem(i).equals(d)) // d is selcted item from list1
return;
}
list2.add(d);
}// b2 list1 < list2
if(ae.getSource()==b4) // list1 > list2
{
// add single selected item from list2 to list1
d =list2.getSelectedItem();
int cnt = list1.getItemCount();
for(i=0;i<cnt;i++)
{
if(list1.getItem(i).equals(d)) // d is selcted item from list1
return;
}
list1.add(d);
}// b4 list1 > list2
if(ae.getSource()==b5)
{
String m = "Enter the item";
String result =JOptionPane.showInputDialog(m);
int cnti=0,cnt = list1.getItemCount();
if(result!=null)
if(result.equals("")!=true)
{
for(int i=0;i<cnt;i++)
{
if(result.equalsIgnoreCase(list1.getItem(i))==true)
{cnti=1; break;}
}
if(cnt==0 || cnti==0)
list1.add(result);
}
}
if(ae.getSource()==b6)
{
String m = "Enter the item";
String result = JOptionPane.showInputDialog(m);
list1.remove(result);
}
if(ae.getSource()==b7)
{
String res=JOptionPane.showInputDialog("Enter The Item");
int cnti=0,cnt = list2.getItemCount();
if(res!=null)
if(res.equals("")!=true)
{
for(int i=0;i<cnt;i++)
{
if(res.equalsIgnoreCase(list2.getItem(i))==true)
{cnti=1; break;}
}
if(cnt==0 || cnti==0)
list2.add(res);
}
}
if(ae.getSource()==b8)
{
String res= JOptionPane.showInputDialog("Enter the item");
list2.remove(res);
}
}//actionperformed
}
No comments:
Post a Comment