vendredi 29 avril 2016

Add checkbox dynamically in java

I wonder how to add checkboxes and name dynamically .

enter image description here

The number of checkboxes is follow the number of row in MySQL and the name is retrieved from MySQL . Assume I have three data in MySQL, so I would get output as image above.

This is my code for class A

checkAPI api= new checkAPI();

try 
{
    num = api.displayCheckBoxAndLabel(); //  get 3
    List<String> allName= api.displayName(); // [John,Tony,Kik]
} 
catch (Exception e1) 
{
    // TODO Auto-generated catch block
    e1.printStackTrace();
                            }    
    deleteAdmin delete = new deleteAdmin(num,allName);
    delete.setVisible(true);
    setVisible(false);
    dispose();
} 

Then pass the two parameters to class deleteAdmin

public class deleteAdmin  extends JFrame {

    private JPanel contentPane;
    private JTextField userText;
    private JTextField txtpassword;
    JFrame f= new JFrame(" Add Admin");

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    button frame = new button();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public deleteAdmin(int num, List<String> names)
    {
        super("Delete Admin");
        setBounds(100, 200, 340, 229);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

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

        JCheckBox[] checkBoxList = new JCheckBox[num];

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

However, I get this kind of output. No checkboxes shown :(

enter image description here




Aucun commentaire:

Enregistrer un commentaire