how to insert check box

how to insert check box

how to insert check box into jtable row in swing

View Answers

September 2, 2011 at 1:10 PM

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class CheckBoxInJTable extends JPanel {

    public CheckBoxInJTable() {
        Object[] columns = new Object[] {"Select", "Name"};
        Object[][] data = new Object[2][2];
        data[0][0] = new Boolean(false);
        data[0][1] = "John";
        data[1][0] = new Boolean(false);
        data[1][1] = "Bill";

        MyTableModel model = new MyTableModel(data, columns);
        JTable table = new JTable(model);

        table.getColumnModel().getColumn(0).setCellEditor(new CheckBoxCellEditor());
        table.getColumnModel().getColumn(0).setCellRenderer(new CWCheckBoxRenderer());
        JScrollPane tableScroller = new JScrollPane(table);
        add(tableScroller);
    }

    private class MyTableModel extends AbstractTableModel {

        private Object[][] data;
        private Object[] columns;

        public MyTableModel(Object[][] data, Object[] columns) {
            this.data = data;
            this.columns = columns;
        }

        public Class getColumnClass(int columnIndex) {
            return data[0][columnIndex].getClass();
        }
        public int getColumnCount() {
            return columns.length;
        }
        public int getRowCount() {
            return data.length;
        }
        public Object getValueAt(int rowIndex, int columnIndex) {
            return data[rowIndex][columnIndex];
        }
              public boolean isCellEditable(int rowIndex, int columnIndex) {
            return (columnIndex == 0 || columnIndex == 2);
        }
    }

    private class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
        protected JCheckBox checkBox;

        public CheckBoxCellEditor() {
            checkBox = new JCheckBox();
            checkBox.setHorizontalAlignment(SwingConstants.CENTER);
            checkBox.setBackground( Color.white);
        }

        public Component getTableCellEditorComponent(
                JTable table, 
                Object value, 
                boolean isSelected, 
                int row, 
                int column) {

            checkBox.setSelected(((Boolean) value).booleanValue());
            return checkBox;
        }
        public Object getCellEditorValue() {
            return Boolean.valueOf(checkBox.isSelected());
        }
    }

    private class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
    Border border = new EmptyBorder(1,2,1,2);
        public CWCheckBoxRenderer() {
        super();
        setOpaque(true);
        setHorizontalAlignment(SwingConstants.CENTER);
        }

        public Component getTableCellRendererComponent(
        JTable table,
        Object value,
        boolean isSelected,
        boolean hasFocus,
        int row,
        int column) {

        if (value instanceof Boolean) {
        setSelected(((Boolean)value).booleanValue()); 
        setEnabled(table.isCellEditable(row, column));

        if (isSelected) {
        setBackground(table.getSelectionBackground());
        setForeground(table.getSelectionForeground());
        } else {
        setForeground(table.getForeground());
        setBackground(table.getBackground());
        }
        }
        else {
        return null;
        }

        return this; 
        }
         }
        public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CheckBoxInJTable test = new CheckBoxInJTable();
        f.getContentPane().add(test);
        f.setSize(350, 350);
        f.show();
    }
}









Related Tutorials/Questions & Answers:
how to insert check box
how to insert check box   how to insert check box into jtable row in swing
check box left justification
check box left justification  how to make a check box left justification which is at right end? thanks in advance
Advertisements
check box in jsp
check box in jsp  hey guys i am working on a web based project using jsp. In my project 1 am having 9 check boxes in 3 rows in the same form. I want to select 1 check box from each row and also i want to avoid many check box
click on check box
click on check box  I create a check box in HTML and when i click on my check box relative information in page store in database
how to insert list box in java script dynamically and elements retrieving from database like oracle
how to insert list box in java script dynamically and elements retrieving from database like oracle  Hi, how to dynamically increase size of list... insert new course in a table.. It should be seen in my list box..ADS
how to insert list box in java script dynamically and elements retrieving from database like oracle
how to insert list box in java script dynamically and elements retrieving from database like oracle  hi all, how can i insert elements into java script list box retrieving from Database. whenever I insert any element in the Db
HI Jsp check box..!
HI Jsp check box..!  Hi all.. I want to update the multiple values of database table using checkbox..after clicking submit the edited field has to update and rest has to enable to update...please help me..its urgent
check box condition
check box condition  Hai, my application has two check box one for chart and another one for table.when We click chart check box download only chart but table also download.same problem in table slection..xsl coding was used
Dynamic check box problem
Dynamic check box problem  In my project i have used a dynamic table, in the each row of that table there is one check box [that i have created... check boxes ... pleas help me as soon as possible...   1)application.jsp
Creating Check Box in Java Swing
Creating Check Box in Java Swing       This section illustrates you how to create a Check Box component in Java Swing. In this section, you can learn simply creating the Check Box
Check Box Validation in PHP - PHP
Check Box Validation in PHP  How can validations done on check boxes more than 3?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/javascript-array/javascript-array-checkboxes.shtml
check box realtive information in page store in database
check box realtive information in page store in database  check box relative information in page store in database  hiiiiiiiii, Que I create a checkbox in html and when i click on my check box realtive information
how to insert image into server
how to insert image into server  how to insert an image into sql server and retrieve it using jSP
How to create an input box?
How to create an input box?  How to create an input box
How to create a confirmation box?
How to create a confirmation box?  How to create a confirmation box
Insert value of dynamic generated text box in jsp using javascript
Insert value of dynamic generated text box in jsp using javascript  hello sir , i want to generate dynamic text box at run time and i did that using javascript code given at your site now sir problem is i am not able to insert
How to insert rows in jTable?
How to insert rows in jTable?  Hi, I need to take input from user using JTable. I want an empty row to appear after clicking a insert button... not figure out how to. I used DefaultTableModel but wasnt able to insert a row
instead of text boxe i have to show check box as selected.
instead of text boxe i have to show check box as selected.  i have one text field. in that iam getting boolean value either true or false from... have to show check box as selected. Thanks In advance R.Raveen.
How to check a checkbox - Struts
How to check a checkbox  Hello Community, How can i check a checkbox defined with tags. with plain html, the tag checks the box. with the other tags i am getting an error that the property "checked" is not defined. Can someone
How to insert clob data??
How to insert clob data??  Can any one tell me in details how... to insert more than 4000 characters.I have heard that clob fields can hold 4 gb of data and need pl/sql to avail that feature.Can any one tell me how to create
How to check spark version
How to check spark version  Hi, How can I find which version of Apache Spark is running on my environment? How to check spark version? Thanks   Hi, You can login to your box where apache spark is running then run
How to check spark version
How to check spark version  Hi, How can I find which version of Apache Spark is running on my environment? How to check spark version? Thanks   Hi, You can login to your box where apache spark is running then run
how to insert a summary values in grid
how to insert a summary values in grid  how to insert a summary value in grid
insert data using drop down box in java into database postgresql
insert data using drop down box in java into database postgresql  can you help me out !!.......i wanna to know the appropriate java syntax to insert data into database using an drop down box .....as i m using eclipse as jdbc
insert data using drop down box in java into database postgresql
insert data using drop down box in java into database postgresql  can you help me out !!.......i wanna to know the appropriate java syntax to insert data into database using an drop down box .....as i m using eclipse as jdbc
insert data using drop down box in java into database postgresql
insert data using drop down box in java into database postgresql  can you help me out !!.......i wanna to know the appropriate java syntax to insert data into database using an drop down box .....as i m using eclipse as jdbc
insert data using drop down box in java into database postgresql
insert data using drop down box in java into database postgresql  can you help me out !!.......i wanna to know the appropriate java syntax to insert data into database using an drop down box .....as i m using eclipse as jdbc
How to insert a car in the picture, insert a car in the picture, insert a car
How to insert a car in the picture       This is very interesting example because you will learn here how to insert an object on the another place. Picture: Take a picture of the road
Check Box Midlet Example
J2ME CheckBox ChoiceGroup MIDlet       This example illustrates how to create check...;, "J2ME", "J2EE", "JSF"). if user select a check box
How to insert data into MySQL Table?
How to insert data into MySQL Table?  Hi, How to insert the data...   Hi, The insert into query is used to insert the data into MySQL... ) Then we can use the following query to insert the data: insert into email(first_name
how to check gremlin version
how to check gremlin version  Hi, I am inside gremlin console and want to check the version. How to check gremlin version? thanks   Hi, On the gremlin console you can run following command: gremlin>
How to check if a pid is alive?
How to check if a pid is alive?  Hi, I have a process which is running in background and I want a simple command to find if the process id still running? How to check if a pid is alive? Thanks   HI, You can use
how to insert the selected item of combobox in mysql - XML
how to insert the selected item of combobox in mysql  hi, i have to insert the selected item from combobox into MYSQL database.pls provide... of combobox in mxml.how to insert selecteditem in database. pls suggest me i have
approve or reject multiple selected rows using check box
approve or reject multiple selected rows using check box   Select Article Document Type Document Sub Type
How to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB:student; @table:stu_info; Combobox values:(class1,class2,class3); textbox1
how to insert the physical path of an image in database - JDBC
how to insert the physical path of an image in database  hello I m... , the image column is of varchar datatype.. plz can you tell me how to insert image's path.. and also how to retrive that image path and show that picture in a small
how to insert value in dynamic table
how to insert value in dynamic table  i am creating a project in Training and Placement.i have a created a company register part where i create...=con.prepareStatement("insert into "+company+year+"(firstname,lastname,email
how to insert excel file into mysql datbase in servlet
how to insert excel file into mysql datbase in servlet  emphashow to insert excel file into mysql datbase inservletized text
insert
insert  insert data in database from servlet through JDBC   Ho Friend, Please visit the following:ADS_TO_REPLACE_1 Insert data into database Thanks
how to insert one table to anothere table
how to insert one table to anothere table  i have insert the record one table then i retrve the value of that table i insert one value at a time
how to insert checkbox value into database using jsp
how to insert checkbox value into database using jsp  how to insert check box value to the database using jsp my code is <link href="font&colors.css" rel="stylesheet" type="text/css"> <p></p>
how to insert values from jsp into ms access
how to insert values from jsp into ms access   how to insert values using jsp into ms access database
how to insert data from netbeans into databse
how to insert data from netbeans into databse  how to insert data from netbeans into databse   Please visit the following link: http://www.roseindia.net/webservices/web-services-database.shtml
i want to select a multiple value of check box by using onclick or onchange event without using from submission
i want to select a multiple value of check box by using onclick or onchange event without using from submission   i have 4 check box name 1)profile... the result is ok but after getting value of first box now i want a result of second box
how to calculate the price on the option box
how to calculate the price on the option box  How i calculate the value when i using a option box with 2 option..first option i used for product name and for the second i used for the quantity..which function should i used
how to insert checkbox value into database using jsp
how to insert checkbox value into database using jsp  How to insert check box value to the oracle database using jsp? I want to create hotel's package. where the admin will select the activities to insert in the PACKAGE table. I
How to check no records - JSP-Servlet
How to check no records  Hi all, I am developing an onlineshopping project, in that how i have 2 check no records. I saw an answer....... At first when a user registers means, his/her cart is empty na... how
how to check a remote applications validity
how to check a remote applications validity  i am developing a network application to avoid unauthorized access to a port .. for that i need to get the application id . how to get the appln id of an appln. or is there any way
How to check keras version in anaconda
How to check keras version in anaconda  Hi, I have installed anaconda python for machine learning in my computer. Now I want to know which version... of keras library installed?ADS_TO_REPLACE_1 How to check keras version
How to check Hadoop version in Ubuntu?
How to check Hadoop version in Ubuntu?  Hi, I am trying various... to Hadoop cluster through ssh terminal. How to check Hadoop version in Ubuntu? Thanks   Hi, Its easy to check the version of Hadoop in Ubuntu. type

Ads