?
Solved

Set CheckBox in JTable

Posted on 2011-05-02
11
Medium Priority
?
600 Views
Last Modified: 2012-05-11
I am using JTable in my application and developing application in Net beans IDE. I dragged n drop JTable in my form but doing code manually like set model, set rows and columns & initialize it, etc.
Now I want to set CheckBox in table cells. How to set it?
Actually I wrote code for same but it’s not showing CheckBox in cells. Its showing ‘true’, ’false’ instead. I want that tick box in cells. How to do it? I want to set checkBox to columns from column 4 to column 34.
My code is below,

private String rows[][]= new String[1][0];
    private String col[]={"v-ua-","laiw.kZ ukao","gqn~nk","xko","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31",",dw.k gtj fnol","nj",",dw.k ixkj #i;s","n22ndex"};
    private javax.swing.table.DefaultTableModel tm = new javax.swing.table.DefaultTableModel(rows,col){
    public boolean isCellEditable(int row, int col)
    {
       if(col==0 || col==1 || col==2 || col==3 || col==35 || col==36 || col==37 || col==38)
       {return false;}
        else
       {return true;}
    }
    };

myTable.setModel(tm);
JCheckBox chkbx = new JCheckBox();
        for(int i=4;i<35;i++)
        {
            TableColumn formatColumn = tblSearch.getColumnModel().getColumn(i);
            formatColumn.setCellEditor(new DefaultCellEditor(chkbx));
        }

Please suggest solution for my code. PLEASE HELP.
0
Comment
Question by:Aurii
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35504199
You need to override getColumnClass too and return Boolean.class for the appropriate col
0
 

Author Comment

by:Aurii
ID: 35504207
but how?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35504209
(and that's all you need to do)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 35504214
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 35504216
0
 

Author Comment

by:Aurii
ID: 35504219
tell me it by code (in my code)
0
 
LVL 92

Expert Comment

by:objects
ID: 35504241
   private javax.swing.table.DefaultTableModel tm = new javax.swing.table.DefaultTableModel(rows,col){
    public boolean isColumnClass(int col)
    {
       if(col==0 || col==1 || col==2 || col==3 || col==35 || col==36 || col==37 || col==38)
       {return Boolean.class;}
        else
       {return Object.class;}
    }
    public boolean isCellEditable(int row, int col)
    {
       if(col==0 || col==1 || col==2 || col==3 || col==35 || col==36 || col==37 || col==38)
       {return false;}
        else
       {return true;}
    }
    };
0
 
LVL 92

Expert Comment

by:objects
ID: 35504244
and add Booleans to your model where you want checkboxes
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 35504812
Here's a runnable example of what you need to do:
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;
import javax.swing.table.*;


public class F extends JFrame {
    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();

            Object[] headers = {
                    "v-ua-", "laiw.kZ ukao", "gqn~nk", "xko", "1", "2", "3", "4",
                    "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
                    "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
                    "26", "27", "28", "29", "30", "31", ",dw.k gtj fnol", "nj",
                    ",dw.k ixkj #i;s", "n22ndex"
                };
            Object[][] rows = {
                    {
                        "v-ua-", "laiw.kZukao", "gqn~nk", "xko",
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), new Boolean(true), new Boolean(true),
                        new Boolean(true), "dw.kgtjfnol", "nj", "dw.kixkj#i;s",
                        "n22ndex"
                    }
                };
            javax.swing.table.DefaultTableModel tm = new javax.swing.table.DefaultTableModel(rows,
                    headers) {
                    public Class getColumnClass(int col) {
                        return ((col >= 4) && (col <= 34)) ? Boolean.class : String.class;
                    }
                };

            cp.add(new JScrollPane(new JTable(tm)), BorderLayout.CENTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        F f = new F();
                        f.setGui();
                        f.setSize(800, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36342911
:)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question