Jtable Showing Boolean as Checkbox Please help

Hi all. I have a java app that opens a database using jdbc. the final column(test) in the database contains a boolean value, currenty it displays true/false. but i want it as a check box

I cant seem to figure out how i could change this for this application.

here is the code, sorry it is so big.

import java.awt.event.ActionEvent;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.print.*;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Enumeration;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableColumn;

/**

*

* Author 0758531

*

*/

publicclass databaseclassextends JFrameimplements ActionListener, Printable

{

//static String fields;

//static String street, house, surname, occupation, servent, outvillage;

static BufferedReader kb =new BufferedReader(new InputStreamReader(System.in));

private Container c;

private JPanel main;

private JPanel jPanelCenter;

private JPanel jPanelSouth;

private JPanel jPanelEast;

private JPanel jPanelWest;

private JPanel jPanelNorth;

private JPanel jPanelWestCenter;

private JPanel jPanelWestWest;

private JPanel jPanelWestEast;

private JTable table;

private JComboBox JComboBoxQuery, JComboBoxSort;

private JTextField txtUser, txtMessage;

private JButton Search, Clear, Exit, Print;

private JLabel lblTop, lblTopb, lblQuery, lblbb, lblStats, lblPer, lblNumber;

Connection con;

String url ="jdbc:odbc:database";

Statement stmt;

static String field;

static String users;

static String order;

privatefloat count;

DefaultTableModel model =new DefaultTableModel(){

//

JFrame window =new JFrame("database");

publicstaticvoid main(String[]args)

{

databaseclass database=new databaseclass();

}

public databaseclass()

{

//layout

//there is more but nothing to do with table....

jPanelWestCenter=new JPanel();

jPanelWest.add(jPanelWestCenter,BorderLayout.CENTER);

jPanelWestCenter.setBackground(new Color(255,255,255));

jPanelWestCenter.setLayout(new BoxLayout(jPanelWestCenter,BoxLayout.Y_AXIS));

jPanelWestCenter.setPreferredSize(new Dimension (900,500));

model.addColumn("ID");

model.addColumn("Map");

model.addColumn("Company");

model.addColumn("Address 1");

model.addColumn("Address 2");

model.addColumn("City");

model.addColumn("County");

model.addColumn("Postcode");

model.addColumn("Telephone");

model.addColumn("Test" ,new Object[]{Boolean.TRUE});

table =new JTable(model);

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JScrollPane scrollpane =new JScrollPane(table);

jPanelWestCenter.add(scrollpane);

table.setPreferredScrollableViewportSize(new Dimension (500, 500));

//

for(int iCtr = 0; iCtr < sList.length; iCtr++ )

JComboBoxQuery.addItem( sList[iCtr] );

//more layout taken out here

window.setSize(1200,600);

window.setVisible(true);

//displays all data when program first runs

txtUser.setText("");

order ="order by ID";

field ="company <> ?";

main();

}

//--

//This runs the sql and sets up the table

publicvoid main()

{

// Explicitly set the Boolean cell renderer and editor on the

// TableColumn showing the first column in the table model

int mColIndex = 9;

TableColumn column;//Sets up columns

column = table.getColumnModel().getColumn(0);

column.setPreferredWidth(40);

column = table.getColumnModel().getColumn(1);

column.setPreferredWidth(60);

column = table.getColumnModel().getColumn(2);

column.setPreferredWidth(200);

column = table.getColumnModel().getColumn(3);

column.setPreferredWidth(120);

column = table.getColumnModel().getColumn(4);

column.setPreferredWidth(100);

column = table.getColumnModel().getColumn(5);

column.setPreferredWidth(100);

column = table.getColumnModel().getColumn(6);

column.setPreferredWidth(70);

column = table.getColumnModel().getColumn(7);

column.setPreferredWidth(70);

column = table.getColumnModel().getColumn(8);

column.setPreferredWidth(122);

column = table.getColumnModel().getColumn(9);

column.setPreferredWidth(30);

/*

column = table.getColumnModel().getColumn(10);

column.setPreferredWidth(117);

column = table.getColumnModel().getColumn(11);

column.setPreferredWidth(70);

column = table.getColumnModel().getColumn(12);

column.setPreferredWidth(90);

*/

//Main sql query

//field and order take data from action listner

String querySEARCH = ("SELECT ID, mapregion, " +

"company, address1, address2, city, county, " +

"postcode, telephone, test " +

"FROM Dealers2 WHERE "+field+" "+order+" ");

String SEARCH = txtUser.getText();

try

{

String driver ="sun.jdbc.odbc.JdbcOdbcDriver";

Class.forName( driver );

con = DriverManager.getConnection(url);

PreparedStatement search = con.prepareStatement(querySEARCH);

search.setString(1,SEARCH);

stmt = con.createStatement();

ResultSet rs = search.executeQuery();

while(rs.next())

{//adds data to columns

int ID = rs.getInt("ID");

int mapregion= rs.getInt("mapregion");

String company = rs.getString("company");

String address1 = rs.getString("address1");

String address2 = rs.getString("address2");

String city = rs.getString("city");

String county = rs.getString("county");

String postcode = rs.getString("postcode");

String telephone = rs.getString("telephone");

boolean test = rs.getBoolean("test");

//String sex = rs.getString("sex");

//int age = rs.getInt("age");

//String trade = rs.getString("trade");

//String cbirth = rs.getString("cbirth");

//String tbirth = rs.getString("tbirth");

txtUser.setText("");//clears user box

//Add Rows

model.addRow(new Object[]{""+ID,""+mapregion, company, address1, address2,

city, county, postcode, telephone,""+test});

count = table.getRowCount();

//lblNumber.setText(count+" of 441 Total Records");

//lblPer.setText(((count/436)*100)+" Percent of Total Records" );

}

stmt.close();

con.close();

}

catch(Exception e)

{

System.out.println( e);

}

}//end of main

//-

publicvoid actionPerformed(ActionEvent arg0)

{

//ok is used to check each input it is set

//to false to start with

boolean ok =false;

//Exit button

if(arg0.getSource() == Exit)

System.exit(0);

//Print button

if(arg0.getSource() == Print)

{

//Prints data from print job

PrinterJob pj=PrinterJob.getPrinterJob();

pj.setPrintable(purplelineclass.this);

pj.printDialog();

try

{

pj.print();

}

catch (Exception PrintException){}

}

if(arg0.getSource() == Clear)//clears the output

txtUser.setText("");//clears text box

model.setRowCount(0);

if(arg0.getSource() == Search)

{

Object codeq;

Object codes;

codeq = JComboBoxQuery.getSelectedItem();

codes = JComboBoxSort.getSelectedItem();

if(codeq =="Search Company:" && isLetter() ==false )

{

field ="company like '%'+ ?+'%'";//added to sql query

ok =true;

}

if(codeq =="Search Town:" && isLetter() ==false)

{

field ="city like '%'+ ?+'%' ";

ok =true;

}

if(codeq =="Search Post Code:"&& isLetter() ==false)

{

field ="postcode like '%'+ ?+'%'";

ok =true;

}

if(codeq =="Search County"&& isLetter() ==false)

{

field ="county like '%'+ ?+'%'";

ok =true;

}

if(codeq =="Search ID"&& isNumber() ==false)

{

field ="ID = ? ";

ok =true;

}

if(codeq =="Search Phone Number:"&& isNumber() ==false)

{

field ="telephone like '%'+ ?+'%' ";

ok =true;

}

if(codeq =="Show all records")

{

txtUser.setText("");

field ="company <> ? ";

ok =true;

}

if(codes =="Sort By ID")

{

order ="order by ID";

}

if(codes =="Sort By Company")

{

order ="order by company";

}

if(codes =="Sort By Map")

{

order ="order by mapregion";

}

if(ok)//only runs if ok is true

{

model.setRowCount(0);//clears rows

main();//finally runs main

}

else//for everything else assume an input error

{

txtMessage.setText("Input error");

txtUser.setText("");

}

}

}

}//End Brace leave alone

Any help would be extremly useful

[17178 byte] By [ard_ouisura] at [2007-11-27 10:26:01]
# 1

Your code is ugly. There is too much stuff all in one class.

Anyway. The default renderer for a boolean value is a checkbox so you are doing one of the following.

a) not properly telling the table that the column contains a boolean

or

b) you are overriding the default renderer with one of your that mucks this up.

cotton.ma at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 2

Oh and in future Swing related questions should be posted into the Swing forum.

cotton.ma at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 3

yes the code is a mess.

There should be no renderers overiding the deafult one so i need to tell the table that the column contains a boolean.

This is what i have been trying but im not clear about where this needs to be and what is the correct syntax for this is.

I have tried but im nto geting far

ard_ouisura at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 4

Is it necessary to separate sections with:

//-

It looks retarded.

CaptainMorgan08a at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 5

> Is it necessary to separate sections with:

> //-

> ]

> It looks retarded.

helpful.....

ard_ouisura at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 6

>/**

> *

> * Author 0758531

> *

> */

Proof for this thread?

http://forum.java.sun.com/thread.jspa?threadID=5194363&tstart=0

KelVarnsona at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 7

> helpful.....

I'm helping you make your code look better.

By the way, the GNU indentation style doesn't look great either.

CaptainMorgan08a at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 8

> > helpful.....

>

> I'm helping you make your code look better.

>

> By the way, the GNU indentation style doesn't look

> great either.

to be fair i do agree with you, this program was not mine, i was planning to modify it for a diffrent use, however as i cant seem to figure out half of the odd things in it i may start again.

ard_ouisura at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 9

If you do decide to start over, and are using this code as a starting-point, here's a few observations:

1) Next time please post just a small, compilable example that explains your problem, not your whole code

2) Don't compare strings using "=="

3) You shouldn't mix database and GUI code

4) You shouldn't have one catch-all ActionListener that listens for everything

5) Class name should be capitalized

6) You shouldn't call setVisible(true) until the frame is ready

7) The frame shouldn't be calling setVisible(true) on itself. This is typically the job of the frame creator.

8) Having a public method called "main" (I'm not referring to your program's entry point) might not be the best idea.

9) Unnecessary comments just create code-clutter ("//End Brace leave alone")

KelVarnsona at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 10

its cool i sorted it it works now, but i will probally still re do it.

Thankyou for the tips they are appreciated

ard_ouisura at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 11

> By the way, the GNU indentation style doesn't look

> great either.

Hey Captain, what is GNU indentation? Thanks -- /Pete

Addendum: never mind, Google and wikipedia to the rescue:

http://en.wikipedia.org/wiki/Indent_style#GNU_style

Message was edited by:

petes1234

petes1234a at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...
# 12

> > By the way, the GNU indentation style doesn't look

> > great either.

>

> Hey Captain, what is GNU indentation? Thanks --

> /Pete

It's ugly, that's all you need to know. ;-)

CaptainMorgan08a at 2007-7-28 17:37:04 > top of Java-index,Java Essentials,New To Java...