Home Answers Viewqa Java-Beginners Adding checkbox to List as item

 
 


krishna v
Adding checkbox to List as item
1 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners

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 Pages:
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 JList(new...(MouseEvent event){ JList list = (JList) event.getSource(); int
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=<
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
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
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
Flex checkbox control example
Flex checkbox control example   ... checkbox control. Example is based on registration for online training courses... is demonstrated. Syntax for creating CheckBox control :   <mx:CheckBox/>
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
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
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
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
Java JComboBox Get Selected Item Value
will be displayed as follows : 3. When you will select any of the list item it will show... adding a data successfully a dialog box will be open. In the displayed list I...Java JComboBox Get Selected Item Value In this section we will discuss about
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
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
CheckBox in Flex
CheckBox item. <mx:CheckBox> tag is use to access this control. Like any...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
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
Dyanmically Adding Rows
Dyanmically Adding Rows  Hi sir Am doing project in that i need to add date picker in dynamically adding rows but the dates are storing in first test... = ""; break; case "checkbox
adding the delete code
adding the delete code  hello sir... i need to add a delete button..."); //Create menu item menuSort = new JMenuItem("Sort By UserName...); //set Mnemonic for menu item menuSort.setMnemonic('S
JSP - Checkbox remain checked although checked values should depend on values stored in a stateful session bean
;input type="checkbox" name="checkBoxes1" value="ITEM 2" <%= checkBox2 %> /> ITEM 2.<br> <input type="checkbox" name="checkBoxes1" value...="checkbox" name="checkBoxes1" value="ITEM 4" <%= checkBox4 %> /> ITEM 4
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
checkbox custom tag creation in jsf
checkbox custom tag creation in jsf  how to create check box custom... is checked names of cars must be populated in the same list 3.when both are checked names of bikes as well as cars must be populated in the same list
How to Add Array Item in PHP
i)    PHP array add item In this PHP tutorial we will discuss about the different techniques to add item into an array.  array_push() is one of the widely used function to add item into an array.General format
List in J2ME
List in J2ME       J2ME Canvas List Example will explain you, how to create list of items. In this example we are going to create a simple list of text, that will show
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
List Control in Flex4
List control in Flex4: Flex 4 List control is also called a Spark List control which displays a list of data items. The user can select one or more item... item do not fit in the List control then horizontal  and vertical scroller
Adding pop up menu on headlines.
Adding pop up menu on Headlines Here you will learn sticking a pop up menu on a title. We have added it on the product list. When the cursor of mouse goes on to pop up menu, it opens the list of the menu automatically. For generating
Checkbox method
Checkbox method  what is the method to check if the checkbox is checked
Dojo Checkbox
Dojo Checkbox        ... a checkbox in dojo. For creating checkbox you require "dijit.form.CheckBox". The checkbox button do some action on press.  [Checkboxes are used when
Dojo Checkbox
Dojo Checkbox        ... a checkbox in dojo. For creating checkbox you require "dijit.form.CheckBox". The checkbox button do some action on press.  [Checkboxes are used when
List of checkboxes - JSP-Servlet
List of checkboxes  Hi! I making a list of time-slots with a checkbox against every such time-slot in a JSP page. My requirement is that when one or more checkboxes are checked and the delete button is clicked
Display JSP selected listbox and checkbox in xml-please help me
select something from 2nd list, third page contains checkbox which is taken from... it should show 1st and 2nd page's selected listbox as well as checkbox list from...Display JSP selected listbox and checkbox in xml-please help me  Hi
ItemRenderer in spark list
ItemRenderer in spark list  Hi.. How can I get the Item Renderer that is at the top of the viewport in a Flex Spark List? please tell me about that..... Thanks   Ans: I think it will useful for you
Radio Button in Java
a RadioButton,we need to create a Checkbox first, followed by adding Radio Button... by the user. The AWT  Widget used to create a radio button is Checkbox. The Checkbox class is used by the AWT to create both Radio Button and Checkbox
code to alert when no checkbox is selected and verify them with orther set
code to alert when no checkbox is selected and verify them with orther set ... of checkboxes in the same jsp page , the list names are "attendess" and "Absentees... the list Attendees and also alert me when the user checks same name under both
Adding an employee
Adding an employee  coding for adding an employee
Setting the Dimensions of an Item in a JList Component in Java
Setting the Dimensions of an Item in a JList Component in Java..., you will learn about setting the dimensions of the List component in java. A list can have multiple data or items. Here, you will know how to set data or items
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
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
How to change object item to integer item
How to change object item to integer item  How to change objectitem to integer item?thankyou
List of checkboxes - JSP-Servlet
List of checkboxes  Hi! I appreciate your effort for giving the response but now it displays only the last value of ArrayList. In other words, it is now displaying just the last time slot. Also, without clicking the checkbox
Not Able to select an Item in Spark Combo Box control (Adobe Flex Object) using QTP
Not Able to select an Item in Spark Combo Box control (Adobe Flex Object) using... Flex controls. But i am not able to select any item from Spark Combo Box. I tied using .Open (Its not opening the drop down list) , also i tried using Select
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
how to return to main menu after adding all the info. - Java Beginners
how to return to main menu after adding all the info.  import.... Remove a student"); System.out.println("3. Display student list... { List list = new ArrayList(); list.add(new Student(1, "A")); list.add(new
List
List  i do have one list object, i want to compare this list from database table and if match found i want to update the list
delete an item from database
delete an item from database  how to delete an item from the database using jsp
List
List  getting values from a form and make a list of them and store them into database
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
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
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
enable disable checkbox in javascript
enable disable checkbox in javascript  How to enable disable checkbox in javascript

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.