Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


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


can we add checkox to List


Advertisement
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 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=<
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
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
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
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
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
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
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
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
Adding an employee
Adding an employee  coding for adding an employee
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
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
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 change object item to integer item
How to change object item to integer item  How to change objectitem to integer item?thankyou
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
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
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
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 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
List
List  getting values from a form and make a list of them and store them into database
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
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
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
delete an item from database
delete an item from database  how to delete an item from the database using jsp
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
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
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
Reset checkbox style to default
Reset checkbox style to default  Reset checkbox style to default   input[type="checkbox"] { /* styles
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
Checkbox Tag <html:checkbox>:
Checkbox Tag <html:checkbox>:       html: checkbox Tag -  is used to create a  Checkbox Input Field. This tag creates an HTML <input> element of type checkbox
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> <
PHP Dynamic CheckBox
PHP Dynamic CheckBox  Help with Dynamic Checkbox in PHP
Addin checkbox to table
Addin checkbox to table  how to add checkboxes in JTable
checkbox value in php
checkbox value in php  In my HTML,PHP form ..we have a check box that is working fine. But anyhow i am not able to get the value of it. Can anyone suggest how to get the value of checkbox in PHP
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
How to check a checkbox - Struts
How to check a checkbox  Hello Community, How can i check a checkbox... please help me with the syntax??  Hi friend, my checkbox... setting the checked attribute of a checkbox to true/checked when using the tags
Checkbox blocked - WebSevices
Checkbox blocked  Hi, i design online ticket reservation for bus. It contains no of seats. if i choose 3 or 4 seats checkbox,it will stored in my database. After i refreshing the page that checkbox seats are blocked
Help to maintain state of checkbox
the state of checkboxes across pages. What happens is when i check one checkbox and then go to say 2nd page then check one checkbox there and when i return to previous page the checked box is again unchecked. Means the status of the checkbox
Advertisements
 

 

 

DMCA.com