Re: check-boxes in JTable
- From: "vijju" <vijaymp11@xxxxxxxxx>
- Date: 10 Apr 2006 06:01:34 -0700
I was also Facing the same problem ..
You can make use of the following the classes.
(1).
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.util.EventObject;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.tree.TreeCellEditor;
public class Components1 {
public static void addComponentToCell(JTable table, JComponent
comp,int row,int col){
table.setValueAt(comp,row,col);
}
public JTable createTable(int row,int column){
JTable table = new JTable(row,column)
{
public TableCellRenderer getCellRenderer(int row, int column) {
TableColumn tableColumn = getColumnModel().getColumn(column);
TableCellRenderer renderer = tableColumn.getCellRenderer();
if (renderer == null) {
Class c = getColumnClass(column);
if( c.equals(Object.class) )
{
Object o = getValueAt(row,column);
if( o != null )
c = getValueAt(row,column).getClass();
}
renderer = getDefaultRenderer(c);
}
return renderer;
}
public TableCellEditor getCellEditor(int row, int column) {
TableColumn tableColumn = getColumnModel().getColumn(column);
TableCellEditor editor = tableColumn.getCellEditor();
if (editor == null) {
Class c = getColumnClass(column);
if( c.equals(Object.class) )
{
Object o = getValueAt(row,column);
if( o != null )
c = getValueAt(row,column).getClass();
}
editor = getDefaultEditor(c);
}
return editor;
}
};
return table;
}
}
class JComponentCellRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object
value,
boolean isSelected, boolean hasFocus, int row, int column) {
return (JComponent)value;
}
}
class JComponentCellEditor implements TableCellEditor, TreeCellEditor,
Serializable {
protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
protected JComponent editorComponent = null;
protected JComponent container = null; // Can be tree or table
public Component getComponent() {
return editorComponent;
}
public Object getCellEditorValue() {
return editorComponent;
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public boolean shouldSelectCell(EventObject anEvent) {
if( editorComponent != null && anEvent instanceof MouseEvent
&& ((MouseEvent)anEvent).getID() == MouseEvent.MOUSE_PRESSED )
{
Component dispatchComponent =
SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3 );
MouseEvent e = (MouseEvent)anEvent;
MouseEvent e2 = new MouseEvent( dispatchComponent,
MouseEvent.MOUSE_RELEASED,
e.getWhen() + 100000, e.getModifiers(), 3, 3, e.getClickCount(),
e.isPopupTrigger() );
dispatchComponent.dispatchEvent(e2);
e2 = new MouseEvent( dispatchComponent, MouseEvent.MOUSE_CLICKED,
e.getWhen() + 100001, e.getModifiers(), 3, 3, 1,
e.isPopupTrigger() );
dispatchComponent.dispatchEvent(e2);
}
return false;
}
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
public void cancelCellEditing() {
fireEditingCanceled();
}
public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
protected void fireEditingStopped() {
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
}
}
}
// implements javax.swing.tree.TreeCellEditor
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row) {
String stringValue = tree.convertValueToText(value,
isSelected,expanded,leaf,row,false);
editorComponent = (JComponent)value;
container = tree;
return editorComponent;
}
// implements javax.swing.table.TableCellEditor
public Component getTableCellEditorComponent(JTable table, Object
value,
boolean isSelected, int row, int column) {
editorComponent = (JComponent)value;
container = table;
return editorComponent;
}
}
(2).....................................................
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
/**
*
public class TableRowSelection1 {
public void rowSelection(JTable table,int index){
switch(index){
case 0:
// Get default selection mode
int selMode = table.getSelectionModel().getSelectionMode();
// MULTIPLE_INTERVAL_SELECTION
break;
case 1:
// Allow only single a selection
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
break;
case 2:
// Allow selection to span one contiguous set of rows,
// visible columns, or block of cells
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
break;
case 3:
// Allow multiple selections of rows, visible columns, or cell
blocks (default)
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
break;
case 4:table.setRowSelectionAllowed( true );
break;
case 5:table.setColumnSelectionAllowed(true);
break;
}
}
}
(3)....................................................
package GenericTable_methods;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class test2 extends JPanel{
String
str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14,
str15,str16,str17,str18,str19,str20,str21,str22,str23,str24;
JLabel lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7,lbl8,lbl9,lbl10,lbl11,
lbl12,lbl13,lbl14,lbl15,lbl16,lbl17,lbl18,lbl19,lbl20,lbl21,lbl22,lbl23,lbl24;
test2() {
str1 = "Tasks";
str2 = "cpu(%)";
str3 ="pri";
str4 = "*SYSTEM/MCPCRYPTOAPI/SU..";
str5 = "6.30";
str6 = "75";
str7 = "SYSTEM/STATION/TRANSFER";
str8 = "8.01";
str9 = "75";
str10 = "TASKING/MESSAGE/HANDLER";
str11 = "0.36";
str12 = "75";
str13 = "CCF/MARC1/DRIVER";
str14 = "0.36";
str15 = "80";
str16 = "*SYSTEM/CCF/PCM/LOGON";
str17 = "0.18";
str18 ="75";
str19 = "*SYSTEM/MXSERVICES/FILESERVER..";
str20 = "0.18";
str21 = "75";
str22 = "SOCKET/ROUTER/WORKER/2";
str23 = "0.02";
str24 = "80";
try{
lbl1 = new JLabel(str1);
lbl2 = new JLabel(str2);
lbl3 = new JLabel(str3);
lbl4 = new JLabel(str4);
lbl5 = new JLabel(str5);
lbl6 = new JLabel(str6);
lbl7 = new JLabel(str7);
lbl8 = new JLabel(str8);
lbl9 = new JLabel(str9);
lbl10 = new JLabel(str10);
lbl11 = new JLabel(str11);
lbl12 = new JLabel(str12);
lbl13 = new JLabel(str13);
lbl14 = new JLabel(str14);
lbl15 = new JLabel(str15);
lbl16 = new JLabel(str16);
lbl17 = new JLabel(str17);
lbl18 = new JLabel(str18);
lbl19 = new JLabel(str19);
lbl20 = new JLabel(str20);
lbl21 = new JLabel(str21);
lbl22 = new JLabel(str22);
lbl23 = new JLabel(str23);
lbl24 = new JLabel(str24);
}
catch(NullPointerException e){
e.printStackTrace();
}
//JFrame f=new JFrame("table");
//TableCellRenderer h = column.getHeaderRenderer();
//Container c=f.getContentPane();
//JComponent but=new JButton("OK");
//JButton but1=new JButton("cancel");
Components1 comp=new Components1();
JTable t=comp.createTable(8,4);
t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// Set the first visible column to 100 pixels wide
int vColIndex = 0;
TableColumn col = t.getColumnModel().getColumn(vColIndex);
int width = 20;
col.setPreferredWidth(width);
int vColIndex1 = 1;
TableColumn col1 = t.getColumnModel().getColumn(vColIndex1);
int width1 = 200;
col1.setPreferredWidth(width1);
//t.getPreferredSize()/
//Component c = h.getTableCellRendererComponent(table,
column.getHeaderValue(), false, false, -1, i);
// width = c.getPreferredSize().width;
JCheckBox cb1,cb2,cb3,cb4,cb5,cb6,cb7,cb8;
cb1 = new JCheckBox();
cb2 = new JCheckBox();
cb3 = new JCheckBox();
cb4 = new JCheckBox();
cb5 = new JCheckBox();
cb6 = new JCheckBox();
cb7 = new JCheckBox();
cb8 = new JCheckBox();
comp.addComponentToCell(t,cb1,0,0);
comp.addComponentToCell(t,lbl1,0,1);
comp.addComponentToCell(t,lbl2,0,2);
comp.addComponentToCell(t,lbl3,0,3);
comp.addComponentToCell(t,cb2,1,0);
comp.addComponentToCell(t,lbl4,1,1);
comp.addComponentToCell(t,lbl5,1,2);
comp.addComponentToCell(t,lbl6,1,3);
comp.addComponentToCell(t,cb3,2,0);
comp.addComponentToCell(t,lbl7,2,1);
comp.addComponentToCell(t,lbl8,2,2);
comp.addComponentToCell(t,lbl9,2,3);
comp.addComponentToCell(t,cb4,3,0);
comp.addComponentToCell(t,lbl10,3,1);
comp.addComponentToCell(t,lbl11,3,2);
comp.addComponentToCell(t,lbl12,3,3);
comp.addComponentToCell(t,cb5,4,0);
comp.addComponentToCell(t,lbl13,4,1);
comp.addComponentToCell(t,lbl14,4,2);
comp.addComponentToCell(t,lbl15,4,3);
comp.addComponentToCell(t,cb6,5,0);
comp.addComponentToCell(t,lbl16,5,1);
comp.addComponentToCell(t,lbl17,5,2);
comp.addComponentToCell(t,lbl18,5,3);
comp.addComponentToCell(t,cb7,6,0);
comp.addComponentToCell(t,lbl19,6,1);
comp.addComponentToCell(t,lbl20,6,2);
comp.addComponentToCell(t,lbl21,6,3);
comp.addComponentToCell(t,cb8,7,0);
comp.addComponentToCell(t,lbl22,7,1);
comp.addComponentToCell(t,lbl23,7,2);
comp.addComponentToCell(t,lbl24,7,3);
t.setDefaultRenderer(JComponent.class, new JComponentCellRenderer() );
t.setDefaultEditor( JComponent.class, new JComponentCellEditor() );
add(t);
}
/* public static void main (String args[]) {
try{
new test2();
}
catch(NullPointerException e){
e.printStackTrace();
}
} */
}
(4).........................................
import javax.swing.JTable;
public class CellEditing1 {
public void cellEditing(JTable table,int row,int column){
// Enable the ability to select a single cell
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(false);
// Set the cell on the 2nd row, 4th column in edit mode
boolean success = table.editCellAt(row, column);
if (success) {
// Select cell
boolean toggle = true;
boolean extend = true;
table.changeSelection(row, column, toggle, extend);
} else {
System.out.println("Cells could not be Edited");
}
}
}
............................................
...............................................
You just call the test2.java in a Frame .
You can see the output with checkBox in the table.
Table is Hard Coded by Some other Person.
If you have any doubts about Renderer .
Get in touch with me.
.
- References:
- check-boxes in JTable
- From: hellbent4u
- check-boxes in JTable
- Prev by Date: Re: checkbox in Jtable cannot be set to editable
- Next by Date: Re: I need help choosing a LayoutManager
- Previous by thread: Re: check-boxes in JTable
- Next by thread: JTable(A few more queries )
- Index(es):