Cell Rendering: Why does this not work?
Hi,
I have created a test class to do cell rendering. The data for the table is obtained from a database (postgresql). I just do not understand why this programe does not work. Please give me suggestion what i am doing wrong.
<<TABLE-->>
//For postgresql
CREATE TABLE test (
testid serial not null,
boolbooleandefault false,
constraint test_pk primary key(testid)
);
//For mysql
CREATE TABLE test (
testid primary key not null,
boolbooleandefaultfalse
);
<<CODE->>
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.swing.*;
import java.swing.event.*;
import java.swing.table.*;
publicclass Testextends JFrameimplements Runnable{
private Container container;
protected JTable table =new JTable();
publicstaticvoid main(String[] args){
Runnable runner =new Test()
EventQUeue.invokeLater(runner);
}
publicvoid run(){
try{
init();
}catch(Exception ex){
ex.printStackTrace();
}
}
privatevoid init()throws Exception{
container = (JPanel) this.getContentPane();
table =new JTable(new TableValues("SELECT * FROM test"));
TableColumnModel tcm = table.getColumnModel();
TableColumn tc = tcm.getColumn(1);
tc.setColumnRenderer(new CheckBoxRenderer());
tc.setColumnEditor(new CheckBoxEditor());
container.add(new JScrollPane(table));
setSize(new Dimension(400, 400));
setDefaultColseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
privateclass TableValuesextends AbstractTableModel{
privatestaticfinal String __DRIVER__ ="org.postgresql.Driver";
privatestaticfinal String __URL__ ="jdbc:postgresql:testdb";
privatestaticfinal String __USERNAME__ ="username";
privatestaticfinal String __PASSWORD__ ="password";
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
public TableValues(String query){
try{
init(query);
}catch(Exception ex){
ex.printStackTrace();
}
}
privatevoid init(String query)throws Exception{
Class.forName(__DRIVER__);
con = DriverManager.getConnection(__URL__, __USERNAME__, __PASSWORD__);
pstmt = con.preparedStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
rs.next();
}
publicint getRowCount(){
try{
if(rs !=null){
rs.last();
return rs.getRow();
}
}catch(Exception ex){
ex.printStactTrace();
}
return 0;
}
publicint getColumnCount(){
try{
if(rs !=null){
return rs.getMetaData().getColumnCount();
}
}catch(Exception ex){
ex.printStactTrace();
}
return 0;
}
public Object getValueAt(itn row,int column){
try{
if(rs !=null){
return rs.getString(row+1);
}
}catch(Exception ex){
ex.printStactTrace();
}
returnnull;
}
public String getColumnName(itn column){
try{
if(rs !=null){
return rs.getMetaData().getColumnName();
}
}catch(Exception ex){
ex.printStactTrace();
}
returnnull;
}
publicboolean isCellEditable(int row,int column){
if(column == 2){
returntrue;
}
returnfalse;
}
publicvoid setValueAt(Object value,int row,int column){
//not implemented
}
}
privateclass CheckBoxRendererextends JCheckBoximplements TableCellRenderer{
public CheckBoxRenderer(){
super();
}
publicboolean isCellEditable(EventObject event){
returntrue;
}
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,
boolean hasFocus,int row,int column){
setSelected(new Boolean(value.toString());
returnthis;
}
}
privateclass CheckBoxEditorextends JCheckBoximplements TableCellEditor{
protected EventListenerList listenerList =new EventListenerList();
protected ChangeEvent changeEvent =new ChangeEvent(this);
public CheckBoxEditor(){
super();
addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
fireEditingStopped();
}
});
}
publicvoid addCellEditorListener(CellEditorListener listener){
listenerList.add(CellEditorListener.class, listener);
}
publicvoid removeCellEditorListener(CcellEditorListener listener){
listenerList.remove(CellEditorListener.class, listener);
}
protectedvoid fireEditingStopped(){
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for(int i=0; i<listeners.length; i++){
if(listeners[i] == CellEditorListener.class){
listener = (CellEditorListener) listeners[i+1];
listener.editingCanceled(changeEvent);
}
}
}
protectedvoid fireEditingCanceled(){
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for(int i=0; i><listeners.length; i++){
if(listeners[i] == CellEditorListener.class){
listener = (CellEditorListener) listeners[i+1];
listener.editingCanceled(changeEvent);
}
}
}
publicvoid cancelCellEditing(){
fireEditingCanceled();
}
publicboolean stopCellEditing(){
fireEditingStopped();
returntrue;
}
publicboolean isCellEditable(EventObject event){
returntrue;
}
publicboolean shouldSelectedCell(EventObject event){
returnnew Boolean(this.isSelected());
}
public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected,
int row,int column){
setSelect(value.toString());
returnthis;
}
}
}
>

