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

