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



Home Answers Viewqa Java-Beginners how to insert check box
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


raghuveer Gowda
how to insert check box
1 Answer(s)      3 years and 4 months ago
Posted in : Java Beginners


how to insert check box into jtable row in swing


Advertisement
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
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
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
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
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
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
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
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
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 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 create a confirmation box?
How to create a confirmation box?  How to create a confirmation box
How to create an input box?
How to create an input box?  How to create an input box
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 insert image into server
how to insert image into server  how to insert an image into sql server and retrieve it using jSP
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 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 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
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 whether database exists?
How to check whether database exists?  Hi, I have to create... to creating the database, i want to check whether database exists or not? How can i do this check. can any one help me on this. Please find thesample code
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
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 a summary values in grid
how to insert a summary values in grid  how to insert a summary value in grid
How to check a file exists or not in java.
Description: This example demonstrate how to check a file exits at a given path or not.  The method isFile() of the File class check for its existence. As in the file path we see '\' (backslash) that is used to denote
How to check a file is hidden or not in java
Description: This example demonstrate how to check a file is hidden at a given path or not.  The method isHidden() of the File class check for its hidden property of a file. As in the file path we see '\' (backslash
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
Advertisements
 

 

 

DMCA.com