Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Adding checkbox to List as item

Ads

 
 

Share on Google+Share on Google+

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

can we add checkox to List

Ads
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 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
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
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
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
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
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
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
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
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
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
How to change object item to integer item
How to change object item to integer item  How to change objectitem to integer item?thankyou
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
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
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
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
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
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