samedi 30 avril 2016

How do I get each checkbox to have one name?

I wanted to make checkboxes and names added dynamically. The number of checkboxes is follow the number of row in MySQL and the name is retrieved from MySQL.

So far, the checkboxes can add dynamically but the problem is I have no idea on how to break the names so that each checkbox only have one name.

This is what I want to archive.

enter image description here

This is the output I get

enter image description here

Eerything start with staffManagement

adminAPI api= new adminAPI();
         try {
            num= api.displayCheckBoxAndLabel(); // there are 5 row
            //allName= api.displayName();
            //System.out.println(allName);

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }    
        deleteAdmin delete= new deleteAdmin(num);
        delete.setVisible(true);
        setVisible(false);
        dispose();

adminAPI

public int displayCheckBoxAndLabel() throws Exception
    {
        int count = 0;
        String sql="Select count(*) AS adminID from admin";
        DatabaseConnection db = new DatabaseConnection();
        Connection  conn =db.getConnection();
        PreparedStatement  ps = conn.prepareStatement(sql);
         ResultSet rs = ps.executeQuery();
         while(rs.next()) 
         {  
             count= rs.getInt("adminID");

         }
         ps.close();
         rs.close();
         conn.close();
         return count ;
    }

    public List<String> displayName() throws Exception // get all the name
    {
        String sql = "Select name from admin";
        List<String> names = new ArrayList<>();
        DatabaseConnection db = new DatabaseConnection();
        try (Connection conn = db.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                names.add(rs.getString("name"));
            }
        }
        return names;
    }

deleteAdmin

public deleteAdmin(int num)
    {
        super("Delete Admin");
        setBounds(100, 200, 340, 229);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        panel.setBounds(35, 19, 242, 146);
        contentPane.add(panel);
        panel.setLayout(null);

        adminAPI admin = new adminAPI();
        List<String>allName = null;
        try {
            allName= admin.displayName();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(allName); //[john,1,1,23,sd]

        JCheckBox[] checkBoxList = new JCheckBox[num];

        for(int i = 0; i < num; i++) {
            checkBoxList[i] = new JCheckBox(""+allName);
           contentPane.add(checkBoxList[i]);

        }


    }

How can I break the arrayList so each checkbox will only have one name? Thanks




Aucun commentaire:

Enregistrer un commentaire