473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in selecting cell in JTable

37 New Member
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.tab le.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;
import java.sql.*;
import javax.swing.Def aultListModel;

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.getConten tPane().add(new JScrollPane(new JTable(new MyTableModel()) ));
Container cont=frame.getC ontentPane();
JPanel p= new JPanel();
p.setLayout( new FlowLayout( FlowLayout.CENT ER ) );
JButton saveb= new JButton("Save") ;
JButton deleb= new JButton("Delete ");
p.add(saveb);
p.add(deleb);
cont.add(p, BorderLayout.SO UTH);
frame.pack();
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
frame.setVisibl e(true);
final MyTableModel tb= new MyTableModel();
deleb.addAction Listener(new ActionListener( ) {
public void actionPerformed (ActionEvent ae) {

for (int r=0;r<tb.i ;r++ )
{

Boolean s= (Boolean)tb.get ValueAt(r, 0);
System.out.prin tln(s);
/*if((boolean)tb .getValueAt(r,0 ))
{
try
{

Connection con = null;
Statement stmt=null;
ResultSet rs= null;
ConnectionBean cb = new ConnectionBean( );
con=cb.getConne ction();
String id=(String)tb.g etValueAt(r,1);
stmt=con.create Statement();
int n= stmt.executeUpd ate("delete from patient_list where patient_id = "+ id);
if(n>0)
System.out.prin tln("row deleted");
// frame.setVisibl e(false);
form();

}
catch(Exception e)
{
}*/
}


}


//}
});

}//end of form()
}

class MyTableModel extends AbstractTableMo del/*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.len gth;
}

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(i nt 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(isCellSelect ed(rowIndex, columnIndex))
{
System.out.prin tln("Hellooooo" );
setValueAt(new Boolean(true), rowIndex, columnIndex);
}
}*/


return (columnIndex==0 );
}

public void setValueAt(Obje ct aValue, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = aValue;
fireTableCellUp dated(rowIndex, columnIndex);
}



/*//*************** **********
private class CheckBoxCellEdi tor extends AbstractCellEdi tor implements TableCellEditor {
protected JCheckBox checkBox;

public CheckBoxCellEdi tor() {
checkBox = new JCheckBox();
checkBox.setHor izontalAlignmen t(SwingConstant s.CENTER);
checkBox.setBac kground( Color.white);
}

public Component getTableCellEdi torComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {

checkBox.setSel ected(((Boolean ) value).booleanV alue());

Component c = table.getDefaul tRenderer(Strin g.class).getTab leCellRendererC omponent(table, value, isSelected, false, row, column);
if (c != null) {
checkBox.setBac kground(c.getBa ckground());
}

return checkBox;
}
public Object getCellEditorVa lue() {
return Boolean.valueOf (checkBox.isSel ected());
}
}*/


MyTableModel()
{
try
{
Connection con = null;
Statement stmt=null;
ResultSet rs= null;
ConnectionBean cb = new ConnectionBean( );
con=cb.getConne ction();
stmt= con.createState ment(ResultSet. TYPE_SCROLL_SEN SITIVE, ResultSet. CONCUR_UPDATABL E);
rs= stmt.executeQue ry("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.prin tln("Error Occurred: "+e.getMessage( ));
}
/*JFrame f= new JFrame();
Container cont= f.getContentPan e();
JPanel p= new JPanel();
p.setLayout(new FlowLayout(Flow Layout.CENTER)) ;
JButton saveb= new JButton("Save") ;
JButton deleb= new JButton("Delete ");
p.add(saveb);
p.add(deleb);
cont.add(p, BorderLayout.SO UTH);
f.setSize(300, 200);
//f.setVisible(tr ue);*/

}







///new end ***//
}
Oct 17 '07 #1
0 1895

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

Similar topics

1
5504
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
8976
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 = tblDecisions.getColumnModel().getColumn(0); TableCellRenderer renderer = column.getHeaderRenderer(); column.setCellRenderer(renderer); }
1
2135
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); setColumnSelectionEnabled(false); setRowSelectionEnabled(true); Help would be much appreciated.
0
3401
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 rest of the textfields and when have to get the focus again the JTable can´t get it !!! only if I go with the mouse!!!! I am trying all things, give the focus to other component before call the Jdialog, put visible(false) the JTable before call...
2
2386
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 select any single cell and the entire row should be selected and displayed Thanks in adv.. Rajendra Kumar.K
1
2027
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 for me.bye.Have a nice day. ////////////////////////////////////////////////////////////////////////////////////////////////////// export.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ...
1
12883
by: thesti | last post by:
hi, can we change the color of the border of a cell / every cell in JTable? thx
1
2550
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 another method in the master class, which creates a new JFrame. The JFrame contains a JTable which has it's own TableModel, also residing within the master class. The JTable reads it's data from an array, which is updated or not depending on the...
6
2884
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 only fires when the user actually LEAVES the cell. If he does some input and then hits a button, the model is still in the old state. How can I force each and every input the user makes to be reflected in my model? Do I have to add a listener that...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.