473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem in selecting cell in JTable

37
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 ***//
}
Oct 17 '07 #1
0 1872

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: gwerk | last post by:
Does anyone know how I can use a MouseListener to select a specific row in a JTable? Also, is there a way to have a hidden field or key for each row? Thanks! Gwerk.
1
by: asd | last post by:
I need to make the cells in the 1st column look like the column header. I tried the following code but it didn't change anything: private void rendererTest() { TableColumn column =...
1
by: asd | last post by:
When I click on a cell, I'd like the whole row to be highlighted, INCLUDING the selected cell. I've tried using the following code to no avail: setCellSelectionEnabled(false);...
0
by: nuria | last post by:
hi, I have an editable JTable, when you are editing it and press F8 shows a dialog and then go again to the JTable window but the focus goes to a JtextField component, go with the tab for the...
2
by: rajendra Kumar k | last post by:
Hai i want to select (by a single mouse click)an entire Row of a JTable and the contents of the row should be displayed in corresponding JTextAreas i.e no. of coloumns = no. of Jtextareas i can...
1
by: vijaykumarsharma | last post by:
hi to all. i written a action performed method to export table data but it is not working fine.It is creating a excel file but there is no data in it. I am sending the code here. please do favour...
1
by: thesti | last post by:
hi, can we change the color of the border of a cell / every cell in JTable? thx
1
by: Gimzo | last post by:
Hi, I'm trying to create a button that opens a new window for setting up a whole crapload of constants for an analysis program. For this, I have a JButton. The actionPerformed-method activates...
6
by: raggwd | last post by:
Hi, I have a JTable and implemented a model. I implemented the setValueAt(...) method to reflect the user input in my model. However, the setValueAt(...) method is not reliable enough, because it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.