Adding checkbox to List as item

Ads
 

Adding checkbox to List as item

can we add checkox to List

View Answers

December 14, 2010 at 12:41 PM

Hi Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CheckBoxInList{
   public static void main(String args[])   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JList list = new JList(new CheckListItem[] {
            new CheckListItem("1"),
            new CheckListItem("2"),
            new CheckListItem("3"),
            new CheckListItem("4"),
            new CheckListItem("5")});

      list.setCellRenderer(new CheckListRenderer());
      list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      list.addMouseListener(new MouseAdapter(){
         public void mouseClicked(MouseEvent event){
            JList list = (JList) event.getSource();
            int index = list.locationToIndex(event.getPoint());
            CheckListItem item = (CheckListItem)
               list.getModel().getElementAt(index);
            item.setSelected(! item.isSelected());
            list.repaint(list.getCellBounds(index, index));
         }
      });   

      frame.getContentPane().add(new JScrollPane(list));
      frame.pack();
       frame.setVisible(true);
   }
}
class CheckListItem{
   private String  label;
   private boolean isSelected = false;

   public CheckListItem(String label){
      this.label = label;
   }
   public boolean isSelected(){
      return isSelected;
   }
   public void setSelected(boolean isSelected){
      this.isSelected = isSelected;
   }
   public String toString(){
      return label;
   }
}
class CheckListRenderer extends JCheckBox  implements ListCellRenderer{
   public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus)
   {
      setEnabled(list.isEnabled());
      setSelected(((CheckListItem)value).isSelected());
      setFont(list.getFont());
      setBackground(list.getBackground());
      setForeground(list.getForeground());
      setText(value.toString());
      return this;
   }
}

Thanks

Ads









Related Tutorials/Questions & Answers:
Adding checkbox in table using that delete table rows
Adding checkbox in table using that delete table rows  I need coding for how to add checkbox in table,the table data from database.Using that checkbox select more than than one rows,and using delete button i want to delete
how to update checkbox list in database
how to update checkbox list in database  Issues: i am using different checkboxs for getting role then all role list store in one string array i want to update one by one but it's not updating by using below code .it's not adding
Advertisements
i am getting an error in adding a menu item to a menu.
i am getting an error in adding a menu item to a menu.   i am not able to execute the menu program. i am getting an error at menu.add(menuitem... to add a menu item to a menu. can anyone help me
Generic method that returns the biggest item in the list.
Generic method that returns the biggest item in the list.  class... } } Write a generic method that returns the biggest item in the list. Each item is a type variable E that extends EngineerProject. The value comparison
Dropdown Checkbox list
Dropdown Checkbox list This tutorial explains how to create jquery Dropdown Checkbox list in JSP.  This example is created in eclipse IDE and run... jquery Dropdown Checkbox list. The application directory structure should look like
checkbox
checkbox  how to insert multiple values in database by using checkbox in jsp
ModuleNotFoundError: No module named 'odoo9-addon-product-pricelist-item-list-view'
ModuleNotFoundError: No module named 'odoo9-addon-product-pricelist-item-list...: ModuleNotFoundError: No module named 'odoo9-addon-product-pricelist-item-list-view...-product-pricelist-item-list-view' error? Thanks   Hi, In your
how to make drop down list in JSF & fetch data Item from database
how to make drop down list in JSF & fetch data Item from database  how to make drop down list in JSF & fetch data Item from database
Displaying checked checkbox in same page as well as print on another page ....also add next pages cheches checkbox into previous list
page's selected checkbox in display list on same pages respectively.( i don't...Displaying checked checkbox in same page as well as print on another page ....also add next pages cheches checkbox into previous list  1
Version of com.leonarduk>freecycle-item-finder dependency
List of Version of com.leonarduk>freecycle-item-finder dependency
Checkbox method
Checkbox method  what is the method to check if the checkbox is checked
I want to display the quantity of the selected item of a drop down list in a textbox. The data is stored in database.
I want to display the quantity of the selected item of a drop down list... from database into Drop Down List. <% DataSource data = new... = pre.executeQuery(); %> Book List : - select
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?  I have a jsp page containing a list box... want to copy the selected item(one at a time) from the list box to the text box. I
Adding an employee
Adding an employee  coding for adding an employee
How to solve this java code by adding the student marks not in the list of the table. For example -10 and 156 in GUI?
How to solve this java code by adding the student marks not in the list...; marks >= 100) { data[a].setGrade("Not in the list of the table...++; } if (g.equals("Not in the list of the table")){ count12
UIButton checkbox
UIButton checkbox  iPhone UIButton checkbox example required.. can any one please explain me.. how to create a checkbox button using UIButton in XIB
item lookup
item lookup  I need to create a lookup box in a grid that searches and filters at the same time. Upon selection the item with its price should be displayed. The control would then position below the first selection, so
Ajax Checkbox
Ajax Checkbox  I want ajax code for checkboxes.The scenario is that if a selectbox is selected the corresponding values to it in the database table are to be checked in the checkbox.....Please do help me
JSP Checkbox
JSP Checkbox In this tutorial you will learn how to create a checkbox using JSP. As we know, checkbox allows multiple selection at a time, but radio... selection always go for checkbox. Using the example we will try to understand how
JSP CheckBox
JSP CheckBox          JSP CheckBox is used to create a CheckBox in JSP... checkbox and also print the userdefined message that specify the languages option
adding loop
adding loop  Hi I have a program that is not compiling when I add... []args){ ArrayList<HardwareItems> list=new ArrayList<... "+" Description "+" Unit Price "); for(HardwareItems e : list
enable disable checkbox in javascript
enable disable checkbox in javascript  How to enable disable checkbox in javascript
refresh checkbox jframe
refresh checkbox jframe  How to refresh checkbox button in JFrame
Reset checkbox style to default
Reset checkbox style to default  Reset checkbox style to default   input[type="checkbox"] { /* styles
How to change object item to integer item
How to change object item to integer item  How to change objectitem to integer item?thankyou
return item in jquery
return item in jquery  How to return items/variables in JQuery?   See the code below to find out how to return an Item in Jquery: function findItem() { var item; while(if_item_not_found) { // search
adding a dialogue
adding a dialogue  Blockquote Hi can you help with the program below,the program is a loop that prints out a code and a quantity when prompt for the user input.what I need is to modify the code to incorporate a dialogue asking
delete an item from database
delete an item from database  how to delete an item from the database using jsp
ModuleNotFoundError: No module named 'item'
ModuleNotFoundError: No module named 'item'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'item' How to remove the ModuleNotFoundError: No module named 'item' error
Addin checkbox to table
Addin checkbox to table  how to add checkboxes in JTable
Item renderer in flex
Item renderer in flex  Hi...... please tell me about How to create item renderers in flex? give an example for that Thanks

Ads