home *** CD-ROM | disk | FTP | other *** search
/ PC World 1998 October / PCWorld_1998-10_cd.bin / software / prehled / inprise / JRUNTIME.Z / DualListBox.java < prev    next >
Text File  |  1998-05-08  |  5KB  |  195 lines

  1. // This snippet creates a panel containing two list boxes, and buttons to
  2. // copy elements from one list to the other.
  3. // <File=DualListBox.java>
  4.  
  5. //Title:
  6. //Version:
  7. //Copyright:
  8. //Author:
  9. //Company:
  10. //Description:
  11. //
  12.  
  13. //<PACKAGE>
  14.  
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import borland.jbcl.layout.*;
  18. import borland.jbcl.control.*;
  19.  
  20. public class DualListBox extends BevelPanel {
  21.   XYLayout xYLayout1 = new XYLayout();
  22.   Label label1 = new Label();
  23.   List source = new List();
  24.   Label label2 = new Label();
  25.   List destination = new List();
  26.   Button addOne = new Button();
  27.   Button addAll = new Button();
  28.   Button removeOne = new Button();
  29.   Button removeAll = new Button();
  30.   Button ok = new Button();
  31.   Button cancel = new Button();
  32.  
  33.   public DualListBox() {
  34.     try {
  35.       jbInit();
  36.     }
  37.     catch (Exception e) {
  38.       e.printStackTrace();
  39.     }
  40.   }
  41.  
  42.   private void jbInit()  throws Exception {
  43.     xYLayout1.setWidth(368);
  44.     xYLayout1.setHeight(231);
  45.     label1.setText("Source list");
  46.     label2.setText("Destination list");
  47.     addOne.setLabel(">");
  48.     addOne.addActionListener(new DualListBox_addOne_actionAdapter(this));
  49.     addAll.setLabel(">>");
  50.     addAll.addActionListener(new DualListBox_addAll_actionAdapter(this));
  51.     removeOne.setLabel("<");
  52.     removeOne.addActionListener(new DualListBox_removeOne_actionAdapter(this));
  53.     removeAll.setLabel("<<");
  54.     removeAll.addActionListener(new DualListBox_removeAll_actionAdapter(this));
  55.     ok.setLabel("OK");
  56.     ok.addActionListener(new DualListBox_ok_actionAdapter(this));
  57.     cancel.setLabel("Cancel");
  58.     cancel.addActionListener(new DualListBox_cancel_actionAdapter(this));
  59.     this.setLayout(xYLayout1);
  60.     this.add(label1, new XYConstraints(12, 4, 140, 14));
  61.     this.add(source, new XYConstraints(12, 20, 140, 176));
  62.     this.add(label2, new XYConstraints(214, 4, 140, 14));
  63.     this.add(destination, new XYConstraints(214, 20, 140, 176));
  64.     this.add(addOne, new XYConstraints(166, 35, 32, 21));
  65.     this.add(addAll, new XYConstraints(166, 61, 32, 21));
  66.     this.add(removeOne, new XYConstraints(166, 130, 32, 21));
  67.     this.add(removeAll, new XYConstraints(166, 160, 32, 21));
  68.     this.add(ok, new XYConstraints(198, 205, 72, 21));
  69.     this.add(cancel, new XYConstraints(283, 205, 72, 21));
  70.   }
  71.  
  72.   void addOne_actionPerformed(ActionEvent e) {
  73.     if (source.getSelectedItem() != null) {
  74.       destination.addItem(source.getSelectedItem());
  75.       source.remove(source.getSelectedIndex());
  76.     }
  77.   }
  78.  
  79.   void addAll_actionPerformed(ActionEvent e) {
  80.     for (int i = 0; i < source.getItemCount(); i++) {
  81.       destination.addItem(source.getItem(i));
  82.     }
  83.     source.removeAll();
  84.   }
  85.  
  86.   void removeOne_actionPerformed(ActionEvent e) {
  87.     if (destination.getSelectedItem() != null) {
  88.       source.addItem(destination.getSelectedItem());
  89.       destination.remove(destination.getSelectedIndex());
  90.     }
  91.   }
  92.  
  93.   void removeAll_actionPerformed(ActionEvent e) {
  94.     for (int i = 0; i < destination.getItemCount(); i++) {
  95.       source.addItem(destination.getItem(i));
  96.     }
  97.     destination.removeAll();
  98.   }
  99.  
  100.   void ok_actionPerformed(ActionEvent e) {
  101.  
  102.   }
  103.  
  104.   void cancel_actionPerformed(ActionEvent e) {
  105.  
  106.   }
  107. //<Exclude>
  108.   // Test case
  109.   public static void main(String[] argv) {
  110.     DecoratedFrame frame = new DecoratedFrame();
  111.     DualListBox dlb=new DualListBox();
  112.     dlb.source.addItem("One");
  113.     dlb.source.addItem("Two");
  114.     dlb.source.addItem("Three");
  115.     dlb.source.addItem("Four");
  116.     dlb.source.addItem("Five");
  117.     frame.add(dlb);
  118.     frame.pack();
  119.     frame.show();
  120.   }
  121. //</Exclude>
  122. }
  123.  
  124. class DualListBox_addOne_actionAdapter implements ActionListener {
  125.   DualListBox adaptee;
  126.  
  127.   DualListBox_addOne_actionAdapter(DualListBox adaptee) {
  128.     this.adaptee = adaptee;
  129.   }
  130.  
  131.   public void actionPerformed(ActionEvent e) {
  132.     adaptee.addOne_actionPerformed(e);
  133.   }
  134. }
  135.  
  136. class DualListBox_addAll_actionAdapter implements ActionListener {
  137.   DualListBox adaptee;
  138.  
  139.   DualListBox_addAll_actionAdapter(DualListBox adaptee) {
  140.     this.adaptee = adaptee;
  141.   }
  142.  
  143.   public void actionPerformed(ActionEvent e) {
  144.     adaptee.addAll_actionPerformed(e);
  145.   }
  146. }
  147.  
  148. class DualListBox_removeOne_actionAdapter implements ActionListener {
  149.   DualListBox adaptee;
  150.  
  151.   DualListBox_removeOne_actionAdapter(DualListBox adaptee) {
  152.     this.adaptee = adaptee;
  153.   }
  154.  
  155.   public void actionPerformed(ActionEvent e) {
  156.     adaptee.removeOne_actionPerformed(e);
  157.   }
  158. }
  159.  
  160. class DualListBox_removeAll_actionAdapter implements ActionListener {
  161.   DualListBox adaptee;
  162.  
  163.   DualListBox_removeAll_actionAdapter(DualListBox adaptee) {
  164.     this.adaptee = adaptee;
  165.   }
  166.  
  167.   public void actionPerformed(ActionEvent e) {
  168.     adaptee.removeAll_actionPerformed(e);
  169.   }
  170. }
  171.  
  172. class DualListBox_ok_actionAdapter implements ActionListener {
  173.   DualListBox adaptee;
  174.  
  175.   DualListBox_ok_actionAdapter(DualListBox adaptee) {
  176.     this.adaptee = adaptee;
  177.   }
  178.  
  179.   public void actionPerformed(ActionEvent e) {
  180.     adaptee.ok_actionPerformed(e);
  181.   }
  182. }
  183.  
  184. class DualListBox_cancel_actionAdapter implements ActionListener {
  185.   DualListBox adaptee;
  186.  
  187.   DualListBox_cancel_actionAdapter(DualListBox adaptee) {
  188.     this.adaptee = adaptee;
  189.   }
  190.  
  191.   public void actionPerformed(ActionEvent e) {
  192.     adaptee.cancel_actionPerformed(e);
  193.   }
  194. }
  195.