Solved

How to disable a checkbox in Jtable

Posted on 2001-07-03
11
1,988 Views
Last Modified: 2012-05-04
In my Jtable i have a column which is a boolean type. I need to disable some of the cells in this column.

How it is possible?
0
Comment
Question by:Narendranath
  • 6
  • 5
11 Comments
 

Author Comment

by:Narendranath
ID: 6247854
and also i have to change the color of the disabled cells.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6247889
It depends what rule you want to use to decide which cells are "disabled".

To "disable" the check box you could use the isCellEditable() method of the underlying TableModel. To change the color of the cells you create a custom cell renderer.

You could also use the custome cell renderer to disable the checkbox.

Can you tell me which cells you would want to disable in the column ?
0
 

Author Comment

by:Narendranath
ID: 6247898
i want to disable some cells is the columns which are of type boolean.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6247907
You said above that you have a whole column that is boolean, are you saying that you want to diable all the cells in that column ?
0
 

Author Comment

by:Narendranath
ID: 6247932
not all the cells i want make some cell in that col as disabled.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ozymandias
ID: 6247940
Yes, what I want to know is what rule you will use to decide which of the cells in that column to disable and which ones not to disable.
0
 

Author Comment

by:Narendranath
ID: 6247960
suppose i have three columns
1. Name
2. age
3. Rights

if the age is less than 18 then i will disable the Rights cell for that record.
if not i will enable the column.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6248245
Here is an example using a table model to allow editing of cells based on the value of other cells in the same row.

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

public class TableCellRenderingExample3 extends JFrame{

     Container contentPane;
     JTable table;
     JScrollPane scroller;
     Vector headers;
     Vector data;

     int rows = 100;

     public TableCellRenderingExample3(String args[]){
          super("TableCellRenderingExample");
          this.setDefaultCloseOperation(EXIT_ON_CLOSE);
          contentPane = this.getContentPane();
          contentPane.setLayout(new BorderLayout());
          if (args.length > 1){rows = Integer.parseInt(args[0]);}
          headers = buildHeaders();
          data = buildData(rows);
          SimpleTableModel stm = new SimpleTableModel(data,headers);
          table = new JTable(stm);
          scroller = new JScrollPane(table);
          contentPane.add(scroller,BorderLayout.CENTER);
     }

     public static void main(String args[]){
          TableCellRenderingExample3 tcre = new TableCellRenderingExample3(args);
          tcre.setSize(600,400);
          tcre.setVisible(true);
     }

     private Vector buildData(int rows){
          Vector v = new Vector();
          Vector r;
          for (int i = 0; i < rows; i++){
               r = new Vector();
               r.addElement("Name " + i);
               Integer age = new Integer((int)(Math.floor(Math.random() * (50 - 1)) + 1));
               r.addElement(age);
               Boolean rights = new Boolean((age.intValue() > 17) ? true : false);
               r.addElement(rights);
               v.addElement(r);
          }
          return v;
     }

     private Vector buildHeaders(){
          Vector v = new Vector();
          v.addElement(new String("Name"));
          v.addElement(new String("Age"));
          v.addElement(new String("Rights"));
          return v;
     }
}

class SimpleTableModel extends AbstractTableModel {

     private Vector dataVector;
     private Vector headingVector;

     public SimpleTableModel(Vector dataVector, Vector headingVector) {
          this.dataVector = dataVector;
          this.headingVector = headingVector;
     }

     public int getColumnCount() {
          return headingVector.size();
     }

     public int getRowCount() {
          return dataVector.size();
     }

     public Object getValueAt(int row, int col) {
        Vector rowVector = (Vector)dataVector.elementAt(row);
        return rowVector.elementAt(col);
     }

     public String getColumnName(int column) {
          return headingVector.elementAt(column).toString();
     }

     public Class getColumnClass(int c) {
          return getValueAt(0, c).getClass();
     }

     public boolean isCellEditable(int row, int col) {
          if (col == 2 && ((Integer)getValueAt(row,col-1)).intValue() < 18){
               return false;
          }else{
               return true;
          }
     }

     public void setValueAt(Object aValue, int row, int col) {
        Vector rowVector = (Vector)dataVector.elementAt(row);
        rowVector.setElementAt(aValue, col);
     }
}
0
 

Author Comment

by:Narendranath
ID: 6248276
Ok one part of my problem is solved. but now i need to change the color of the disabled checkbox to gray.
Is it possible to change the color?
0
 

Author Comment

by:Narendranath
ID: 6248366
If i tab out from an enabled cell to the disabled cell then the focus should not go that disabled cell, instead it has to go to the next enabled cell.
How i have to achieve this?
Please tell.
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 40 total points
ID: 6248452
I can't post the rest of my answer.

Basically its the same code as above but with a TableCellRenderer to change the cell color depending on whether or not the cell is editable.

BTW, this is not a 10 point question.

You seem to have a lot of very low point questions at the moment.

I think that this ought to be 100 points minimum.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 84
Java. Convert method from recursion based to iteration based ( loop based ) 6 72
rhino JavaScript import, load 25 66
object oriented programming comparison 5 52
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now