Adding checkbox to List as item

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









Related Tutorials/Questions & Answers:
Adding checkbox to List as item
Adding checkbox to List as item   can we add checkox to List  ...(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JList list = new... mouseClicked(MouseEvent event){ JList list = (JList) event.getSource
Problem to display checkbox item
include next page's selected checkbox in display list on same pages respectively.( i...Problem to display checkbox item  Hi, Following is my code: <...;td> <input type="checkbox" name="check" value=<
Advertisements
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
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
awt list item* - Swing AWT
awt list item*  how do i make an item inside my listitem... frame=new Frame("Add list"); Label label=new Label("What is your Choice..."); choice.add("Struts"); List lst = new List(4, false); lst.add
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
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
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
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
remove item from list box using java script - Java Beginners
remove item from list box using java script  remove item from list box using java script  Hi friend, Code to remove list box item using java script : Add or Remove Options in Javascript function addItem
checkbox
checkbox  how to insert multiple values in database by using checkbox in jsp
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
Version of com.leonarduk>freecycle-item-finder dependency
List of Version of com.leonarduk>freecycle-item-finder dependency
Displaying checked checkbox in same page as well as print on another page ....also add next pages cheches checkbox into previous list
as well as i aslo include next page's selected checkbox in display list on same...Displaying checked checkbox in same page as well as print on another page ....also add next pages cheches checkbox into previous list  1
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
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
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
Checkbox method
Checkbox method  what is the method to check if the checkbox is checked
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
Item renderer in flex
Item renderer in flex  Hi..... How does item renderer work? How do I add item renderer at runtime? Please me the right answer so i cam implement this in my apps. Thanks  Ans: The base class for Spark item
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
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 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
Adding an employee
Adding an employee  coding for adding an employee
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
Dojo Tree With Checkbox - Framework
structure in a jsp along with checkbox besides each node. The tree structure... with tree structure. The checkbox will have 3 states: (1) partially checked, (2... (not corresponding to any item in the store) Clicking a folder node
Checkbox
Checkbox       A checkbox is again a label which is displayed as a pushbutton as shown in the example... of the checkbox is either true or false. However, the initial state is false
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
navigation item back button
navigation item back button  I wants to add a navigationBar on the top of my UIWebView also a navigation item "back button". can anyone please explain how the back and refresh button works
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
Add a new item to a dictionary in Python
Add a new item to a dictionary in Python  Hi, I have an dictionary in Python and I want to add new item to it. What is the best way to add an item to a python dictionary? Share me example for adding a new item to a dictionary
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
Item renderer in flex
Item renderer in flex  Hi... Please tell me about what does listData contain in ItemRenderers? What is the Type of listData? please give an example for that........ Thanks
Item renderer in flex
Item renderer in flex  Hi..... I have a problem How do you call a method in particular ItemRenderer. Also the ItemRenderer is your own Custom Component? please give me an example for that...... Thanks
highlight menu item html
highlight menu item html  highlight menu item in html or CSS   <body id="index"> <div id="menu"> <ul> <li class="index" ><a href="index.html">Index page</a></li> <
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
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
Linking JMenu Item with a JPane in Netbeans
Linking JMenu Item with a JPane in Netbeans  How do you link a Jpane window to a JMenu Item in Java Netbeans
CheckBox in Flex
a particular CheckBox item. <mx:CheckBox> tag is use to access...Flex CheckBox Control: The CheckBox control of Flex is a very common GUI, which is supported by almost every computer language. CheckBox control is used when
Dojo Checkbox
Dojo Checkbox          In this section, you will learn how to create a checkbox in dojo. For creating checkbox you require "dijit.form.CheckBox". The checkbox button do
ModuleNotFoundError: No module named 'item-tracking'
ModuleNotFoundError: No module named 'item-tracking'  Hi, My... 'item-tracking' How to remove the ModuleNotFoundError: No module named 'item-tracking' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Output_item'
ModuleNotFoundError: No module named 'Output_item'  Hi, My Python... 'Output_item' How to remove the ModuleNotFoundError: No module named 'Output_item' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'print-item'
ModuleNotFoundError: No module named 'print-item'  Hi, My Python... 'print-item' How to remove the ModuleNotFoundError: No module named 'print-item' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'item-tracking'
ModuleNotFoundError: No module named 'item-tracking'  Hi, My... 'item-tracking' How to remove the ModuleNotFoundError: No module named 'item-tracking' error? Thanks   Hi, In your python
PHP Dynamic CheckBox
PHP Dynamic CheckBox  Help with Dynamic Checkbox in PHP

Ads