NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

How to add a JCheckbox in a JTable using the NetBeans IDE

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
mervyn



Joined: 23 Feb 2009
Posts: 2
Location: INDIA

PostPosted: Mon Feb 23, 2009 11:27 am    Post subject: How to add a JCheckbox in a JTable using the NetBeans IDE Reply with quote

It's been quite a while developing standalone applications, can someone please help me in creating Checkbox within a JTable and assign a boolean value to it.

Please provide a walkthrough in rendering a checkbox within a JTable. I couldn' find a way doing this in NetBeans.
Back to top
dajense



Joined: 11 Dec 2009
Posts: 1

PostPosted: Fri Dec 11, 2009 6:41 pm    Post subject: Inserting a JCheckBox/JComboBox in a JTable Reply with quote

Ok, this post is quite old but I have been struggling to figure this out myself, so now that I got it, I thought I should share as the number of views indicates other people are interested in this too. Note the stuff commented out for JComboBoxes. Here it goes.

After using the IDE to create a table you need only to add two lines:
Code:
JCheckBox checkBox = new javax.swing.JCheckBox();
and
Code:
jTable1.getColumn("State").setCellEditor(new DefaultCellEditor(checkBox));


They can be added by right clicking your table, choosing 'customize code'. The first line goes on top, the second line on the bottom. Also in the 'Table Contents' menu you need to specify the datatype of the corresponding coloumn as boolean.
The resulting code should look something like this.
Code:



// Use this line to add a CheckBox
JCheckBox checkBox = new javax.swing.JCheckBox();
// Use this line to add a ComboBox
//JComboBox combo = new JComboBox(new String[]{"Administrator", "Professional", "Staff"});



jTable1 = new javax.swing.JTable();

jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null},
        {null, null, null, null, null, null}
    },
    new String [] {
        "State", "Edit Data", "Description", "Rule Group", "Relative Deviation", "Absolute Deviation"
    }
) {
    Class[] types = new Class [] {
        java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
    };

    public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
    }
});

jTable1.setName("jTable1"); // NOI18N



jScrollPane1.setViewportView(jTable1);

jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title5")); // NOI18N
jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N
jTable1.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N
jTable1.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("jTable1.columnModel.title3")); // NOI18N
jTable1.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N
//jTable1.getColumn(2).setCellEditor(jButton1);

// Use this line to add a CheckBox
jTable1.getColumn("State").setCellEditor(new DefaultCellEditor(checkBox));

// Use this line to add a ComboBox
//jTable1.getColumn("Group").setCellEditor(new DefaultCellEditor(combo));



Also, you need to import the following classes yousing the source view.
Code:

import javax.swing.JCheckBox;
import javax.swing.DefaultCellEditor;
Back to top
mervyn



Joined: 23 Feb 2009
Posts: 2
Location: INDIA

PostPosted: Fri Dec 11, 2009 11:26 pm    Post subject: Thanks a lot dajense. Reply with quote

I stopped looking at this site as there was no reply and my account was deactivated.
Thank you for posting a reply inspite of this being a very old post.
Back to top
savdeep



Joined: 11 Mar 2010
Posts: 1

PostPosted: Thu Mar 11, 2010 5:47 am    Post subject: TY..but Reply with quote

well thank u for such a help...but 1 thing more i wana ask...
tht if we want that in the label(column) of the check box field i want a check box which will mark/unmark every entry in that column if itself its clicked then how should i do it? Question Question Question Question Question
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo