Wednesday, August 1, 2012

Dynamically generating Checkboxes and accessing their content

Here is code which automatically generates checkboxes based on an arraylist of strings. The trick to accessing the checkboxes afterwards is to also store them in a an arraylist. Here is the code for CheckboxTest.java


package com.spconger.www;
/*
 * This class creates two arrays
 * one for a list of titles
 * and one to store dynamically created
 * checkboxes
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

public class CheckboxTestForm {
 
 //declare private fields
 private ArrayList titles; //arraylist for titles
 private JFrame frame;
 private ArrayListchecks; //array of checkboxes
 private JPanel panel;
 private JButton button;
 private JLabel result;
 private JCheckBox chk;
 
 //constructor that calls the methods
 //and initializes the checkbox array
 public CheckboxTestForm(){
  fillArray();
  checks = new ArrayList();
  createCheckBoxes();
 }
 
 private void fillArray(){
  //this method populates the array
  //manually--you could do this
  //dynamically with user input
  titles=new ArrayList();
  titles.add("Foundation Trilogy");
  titles.add("lord of the Rings");
  titles.add("1@84");
  titles.add("The Martian Chronicles");
 }
 
 private void createCheckBoxes(){
  //set up the frame
  JFrame frame=new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  //call the method to create the panel
  createPanel();
  //add the panel to the frame and make it visible
  frame.add(panel);
  frame.setBounds(100,100,300,300);
  frame.setVisible(true);
 }
 
 private void createPanel(){
  //initialize the panel
  panel = new JPanel();
  panel.setLayout(new GridLayout(0,1,5,5));
  //loop through the title array
  for (String s:titles){
    //create a new checkbox 
   //for each string in the array
    chk=new JCheckBox(s);
    //add the checkbox to the panel
    panel.add(chk);
    //add the checkbox to the array
    //of checkboxes
    checks.add(chk);
  }
  
  //add a button
  button=new JButton("Test");
  //assign a listener
  button.addActionListener(new buttonListener());
  //add it to the panel
  panel.add(button);
  
  //add a label to show the results
  result = new JLabel();
  panel.add(result);
  
  
 }
 
 private class buttonListener implements ActionListener{
  /*
   * (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   * This class responds to the button click
   */
  @Override
  public void actionPerformed(ActionEvent arg0) {
   String s="";
   //loop through the array of checkboxes
   //and get the text of each checkbox
   //that is checked
   for (JCheckBox ck:checks){
    if(ck.isSelected()){
    s += ck.getText() + ", ";
    }
   }
   //display the results
   result.setText(s);
  }
  
 }

}


Here is the code for Program.java


package com.spconger.www;

public class Program {

 /**
  * this program is an example of 
  * creating checkboxes dynamically
  * on the fly. You can use this in
  * a todo list. Add your tasks to an 
  * Array List and then dynamically display them
  * The main merely calls the class
  */
 public static void main(String[] args) {
  CheckboxTestForm c= new CheckboxTestForm();

 }

}

3 comments:

  1. Hello sir

    I am developing a desktop application in which I want Admin have option to delete users, for which I planned that whenever Admin clicks on 'delete users' button a new tab will open in which check boxes with the name of all existing users in my database should appear(so that he can delete multiple users simultaneously, if needed); so basically I need to generate dynamic check boxes as per my database.

    I am using netbeans7.0.1, jdk 1.6, sqlite3.

    To fulfill my job I can create crate new java class and past above code, but before watching it I tried to write it myself and what I does is create a new JFrame using Netbeans GUI Maker, then write a method with following code and call it inside constructor:

    private void generatingCheckboxesDynamically(){
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout());
    for(int i = 0; i<4; i++){
    JCheckBox box = new JCheckBox("cb"+i);
    panel.add(box);
    panel.revalidate();
    panel.repaint();
    }
    this.add(panel);
    System.out.println("Worked!!!!!!!!");
    }

    Database connections are not done yet,I wrote it just to test first whether check boxes are generating dynamically or not. The output of above code is blank frame.I want to know why so? Your code is quite similar to mine for generating check boxes dynamically, still am getting blank frame!! Can you help me to know the reason,please.

    Also I have mentioned my main task in very first paragraph, is there any other more better way to accomplish it or I am on right path?

    ReplyDelete
  2. can you please send the java code(swings,jdbc) to load items in the checkbox dynamically from database by selecting a item in another static checkbox...,

    Mail id: vamsiraj.p@gmail.com

    ReplyDelete
  3. I think in above code some error is occurring please check it will be very helpful to all.

    ReplyDelete