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;

}

}

}

>

[14813 byte] By [guguBananaa] at [2007-10-2 10:15:03]
# 1

I haven't look through the whole code in detail BUT in your table model method

public Object getValueAt(itn row, int column)

you use

rs.getString(row+1)

This gets the value as a string of column (row+1) in the row that the ResultSet is pointing to and NOT the database row! You will need to copy that values out of the ResultSet to build the table model.

It looks to me like you need to get a better understanding of JDBC and in particular of ResultSet so I suggest you go back to the JDBC tutorial.

sabre150a at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hi,Thank you for your reply but you may find that the implementation of getValueAt() is correct.Try executing the codeThank you.gugu
guguBananaa at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...
# 3
I have solved the problemThanks anywaygugu
guguBananaa at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Thank you for your reply but you may find that the

> implementation of getValueAt() is correct.

>

> Try executing the code

I don't know what I am missing but the code will not even compile! Two examples

1) Method signature public Object getValueAt(itn row, int column) {

The first parameter should be an 'int' not an 'itn'! There are several instances of this.

2) Call to rs.getMetaData().getColumnName();

rs.getMetaData() returns a ResultSetMetaData object and there is no method ResultSetMetaData.getColumnName(). There is a method ResultSetMetaData.getColumnName(int column).

On top of this there is

public void setValueAt(Object value, int row, int column) {

//not implemented

}

which will be invoked when an edit has finished so it will not update anything!

Then there is the abomination

public boolean isCellEditable(int row, int column) {

if(column == 2) {

return true;

}

return false;

}

which apart from being bad code (why not just return column == 2) uses the magic number 2 which requires that your editable column is column 2 BUT you select data using "SELECT * FROM test" where there is not guarantee that column 2 is the column you want.

I could go on but ...

sabre150a at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thank you for your reply but it is obvious there are some typing error in the code which you have correctly pointed out. I have no network connection hence, I have had to retype the code to post this thread. So I quite agree with you. But if you modified the obvious mistakes and leave out the one without implementation you might find the code works.

guguBananaa at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Thank you for your reply but it is obvious there are

> some typing error in the code which you have

> correctly pointed out.

Sorry but I don't intend to waste time finding your typing errors.

> I have no network connection

> hence, I have had to retype the code to post this

> thread.

Memory sticks are cheap! Floppy disks are cheap.

> So I quite agree with you. But if you

> modified the obvious mistakes and leave out the one

> without implementation

Come off it! I'm supposed to fix the obvious mistakes and then try to tell you what is wrong? No chance!

> you might find the code works.

Little or no chance!

Also -

1) How can edit work without setValue()?

2) I don't have a database matching yours!

3) I think your use of result set is wrong.

Finally - you say you have solved the problem yet you don't say what you found wrong!

sabre150a at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...
# 7
Good for you!
guguBananaa at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...
# 8
> Good for you!:-) Do I detect a hint of sarcasm? What did you expect me to say?
sabre150a at 2007-7-13 1:39:05 > top of Java-index,Desktop,Core GUI APIs...