add jcheckBox to Jtable using GUI


after searching in google and solutionoferror and going over little solutions I am still stuck with this issue.
I have jtable and I am filling it from DB(derpy-JDBC).
i need add checkout box to my jtable using GUI..I hv update column type to boolean and add these lines
JCheckBox checkBox = new javax.swing.JCheckBox();
jTable1.getColumn("status").setCellEditor(new DefaultCellEditor(checkBox));

but it does not work perfectly.
this my try
public class showp1 extends javax.swing.JFrame implements ActionListener {
/** Creates new form showp1 */
public showp1() {
initComponents();
this.setLocationRelativeTo(null);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jTextField1.addActionListener(this);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: don't change this code snippet. The content of this method is
* generally regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object {
{null null null null}
{null null null null}
{null null null null}
{null null null null}
}
new String {
"id" "area" "location" "status"
}
) {
Class kinds = new Class {
java.language.Object.class java.language.Object.class java.language.Object.class java.language.Boolean.class
};
public Class getColumnClass(int columnIndex) {
return kinds columnIndex;
}
});
jScrollPane1.setViewportView(jTable1);
jButton1.setText("jButton1");
jButton2.setText("jButton2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1 javax.swing.GroupLayout.PREFERRED_SIZE 320 javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(73 73 73)
.addComponent(jButton2)
.addGap(166 166 166))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING layout.createSequentialGroup()
.addComponent(jScrollPane1 javax.swing.GroupLayout.PREFERRED_SIZE 375 javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
.addGap(10 10 10))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1 javax.swing.GroupLayout.DEFAULT_SIZE 23 Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1 javax.swing.GroupLayout.PREFERRED_SIZE 155 javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18 18 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(64 64 64))
);
pack();
}//

private void formWindowOpened(java.awt.event.WindowEvent evt) {
myDbConnection dbconnect;
// TODO add your handling code snippet here:
ArrayList list = new ArrayList();
try {
dbconnect = new myDbConnection();
ResultSet resultSet =null;
resultSet = dbconnect.excuteQuery("SELECT id area location status1 FROM pledges ");
while (resultSet.next()){
list.add(resultSet.getString(1));
list.add(resultSet.getString(2));
list.add(resultSet.getString(3));
list.add(resultSet.getString(4));
}
} catch (Exception e) {
System.out.println(e);
}
Object record;
int myListCount = list.size()/4;
record = new ObjectmyListCount4;
int count = 1;
// JCheckBox checkBox = new javax.swing.JCheckBox();
for (int ii = 1; ii<=myListCount;ii++) {
for(int i=1;i<=4;i++){
recordii-1i-1 = list.get(count-1);
count++;
}
}
//TableColumnModel columnModel = jTable1.getColumnModel();
//columnModel.getColumn(3).setCellEditor(new DefaultCellEditor(checkBox));
jTable1.setModel(new DefaultTableModel(record new String{"id" "area" "location" "status1"}) {
@Override
public Class getColumnClass(int columnIndex) {
if (getColumnName(columnIndex).equals("status1")) {
return Boolean.class;
}
return super.getColumnClass(columnIndex);
}
});
public static void main(String args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void execute() {
new showp1().setVisible(true);
}
});
} }

sorry for this awful code^^"
Thank You in advance



Get Source Code

  Views 52   Posted on : Friday, May 24, 2013 10:58:19 AM
Related Tags:




Replies on "add jcheckBox to Jtable using GUI" :

Nobody has replied on this post , be first to post answer.


Post Your Answer/Comment:

Please Login first to post Reply !


Note: Please Put Code into <code></code> block.




Also See: