Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdown list box & check box listener 1

Status
Not open for further replies.

Ngai88

Programmer
Sep 8, 2004
56
US
Hello,
On this page, there exists 20 checkboxes of recipients. I am asked to add a dropdown list box. On the same page, there are same 20 recipients as checkboxes. The task is when the user select a recipient from the dropdown box, the checkbox with the same recipient name will automatically be checked. I looked at the examples of what is published on the web, often they talk about dropbox, checkbox or buttons separately, no example of dropbox and checkbox interacting as listener, pairing together. Would anyone have any examples or templates that I can read up on?

Thanks,
Ngai
 
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mycom extends JFrame implements ActionListener
      {
       String nameList[] = {"Adam","Bob"};
       JComboBox selectName;
       JCheckBox checkName[];
       public mycom()
              {
               super("JComboBox and checkbox");
               selectName = new JComboBox(nameList);
               checkName = new JCheckBox[20];
               getContentPane().setLayout(new GridLayout(21,1));
               getContentPane().add(selectName);
               selectName.addActionListener(this);
               for (int i=0;i<nameList.length;i++)
                   {
                    checkName[i] = new JCheckBox(nameList[i]);
                    getContentPane().add(checkName[i]);
                   }
              }
       public void actionPerformed(ActionEvent e)
              {
               System.out.println(selectName.getSelectedIndex());
               if (selectName.getSelectedIndex()!=-1)
                  {
                   for (int i=0;i<nameList.length;i++)
                       {
                        checkName[i].setSelected(false);
                       }
                   checkName[selectName.getSelectedIndex()].setSelected(true);
                  }
              }
       public static void main(String args[])
              {
               mycom mycomObj = new mycom();
               mycomObj.setSize(400,400);
               mycomObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               mycomObj.setVisible(true);
              }
      }
 
Wow!! You are good Prosper !! Thanks a lot. This will definitely give me a very good start. I appreciate it.

Thanks again,
Ngai
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top