Hi to all'
I am using Java swings.
I written a code to display a table. In the table i used Boolean.class type column.
when i click the checkbox it value to be change from false to true.
please help me . iam sending my code here
CODE:
package classes;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.DefaultListModel;
import classes.*;
public class Test extends JFrame/* implements ActionListener*/ {
public static void main(String[] args){
form();
}
public static void form() {
JFrame frame = new JFrame("Patient table");
frame.getContentPane().add(new JScrollPane(new JTable(new MyTableModel())));
Container cont=frame.getContentPane();
JPanel p= new JPanel();
p.setLayout( new FlowLayout( FlowLayout.CENTER ) );
JButton saveb= new JButton("Save");
JButton deleb= new JButton("Delete");
p.add(saveb);
p.add(deleb);
cont.add(p, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
final MyTableModel tb= new MyTableModel();
deleb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
for (int r=0;r<tb.i ;r++ )
{
Boolean s= (Boolean)tb.getValueAt(r, 0);
System.out.println(s);
/*if((boolean)tb.getValueAt(r,0))
{
try
{
Connection con = null;
Statement stmt=null;
ResultSet rs= null;
ConnectionBean cb = new ConnectionBean();
con=cb.getConnection();
String id=(String)tb.getValueAt(r,1);
stmt=con.createStatement();
int n= stmt.executeUpdate("delete from patient_list where patient_id = "+ id);
if(n>0)
System.out.println("row deleted");
// frame.setVisible(false);
form();
}
catch(Exception e)
{
}*/
}
}
//}
});
}//end of form()
}
class MyTableModel extends AbstractTableModel/*implements TableCellEditor */{
int i;
private String columnNames[] = new String[] { "Select" ,"ID","Name", "Age","Sex", "Address","App Type"};
private Class columnClasses[] = new Class[]{Boolean.class, String.class, String.class, String.class, String.class, String.class, String.class};
private Object data[][];
JComponent component = new JTextField();
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public Class getColumnClass(int columnIndex) {
return columnClasses[columnIndex];
}
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
/*if (columnIndex!=0)
return true;
else
return false;
for(int j=0;j<i;j++)
{
if(isCellSelected(rowIndex, columnIndex))
{
System.out.println("Hellooooo");
setValueAt(new Boolean(true), rowIndex, columnIndex);
}
}*/
return (columnIndex==0);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}
/*//*************************
private class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
protected JCheckBox checkBox;
public CheckBoxCellEditor() {
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CEN TER);
checkBox.setBackground( Color.white);
}
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {
checkBox.setSelected(((Boolean) value).booleanValue());
Component c = table.getDefaultRenderer(String.class).getTableCel lRendererComponent(table, value, isSelected, false, row, column);
if (c != null) {
checkBox.setBackground(c.getBackground());
}
return checkBox;
}
public Object getCellEditorValue() {
return Boolean.valueOf(checkBox.isSelected());
}
}*/
MyTableModel()
{
try
{
Connection con = null;
Statement stmt=null;
ResultSet rs= null;
ConnectionBean cb = new ConnectionBean();
con=cb.getConnection();
stmt= con.createStatement(ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE);
rs= stmt.executeQuery("Select * from patient_list");
i=0;
while(rs.next())
{
i++;
}
//JCheckBox c[]= new JCheckBox[i];
data= new Object[i][7];
rs.beforeFirst();
int r=0;
while(rs.next())
{
data[r][0]=new Boolean(false);;
data[r][1]=rs.getString(1);
data[r][2]= rs.getString(2);
data[r][3]=rs.getString(3);
data[r][4]=rs.getString(4);
data[r][5]=rs.getString(5);
data[r][6]= rs.getString(6);
r++;
}
}
catch (Exception e)
{
System.out.println("Error Occurred: "+e.getMessage());
}
/*JFrame f= new JFrame();
Container cont= f.getContentPane();
JPanel p= new JPanel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton saveb= new JButton("Save");
JButton deleb= new JButton("Delete");
p.add(saveb);
p.add(deleb);
cont.add(p, BorderLayout.SOUTH);
f.setSize(300, 200);
//f.setVisible(true);*/
}
///new end ***//
}