JTable and JCheckBox
How do I get the value of the checkboxes?
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
class Testingextends JFrame
{
String colNames[] ={"CheckBox","String","String"};
Object[][] data ={};
DefaultTableModel dtm;
public Testing()
{
setLocation(400,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dtm =new DefaultTableModel(data,colNames);
JTable table =new JTable(dtm);
JScrollPane sp =new JScrollPane(table);
TableColumn tc = table.getColumnModel().getColumn(0);
tc.setCellEditor(table.getDefaultEditor(Boolean.class));
tc.setCellRenderer(table.getDefaultRenderer(Boolean.class));
getContentPane().add(sp);
for(int x = 0; x < 5; x++)
{
dtm.addRow(new Object[]{new Boolean(false),"Row "+(x+1)+" Col 2","Row "+(x+1)+" Col 3"});
}
pack();
}
publicstaticvoid main (String[] args){new Testing().setVisible(true);}
}
This a simple example, I'd like to get the value of the checkbox, I wanna if it is selected or no?
[2294 byte] By [
MrSmyllea] at [2007-10-2 6:41:02]

Sorry, because I wanted to watch the posts!!!I Can you try this code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableTest extends JFrame implements ActionListener
{
JButton btnAdd;
BorderLayout layout;
DefaultTableModel model;
JTable table;JButton btexcluir;
public static void main(String[] args)
{
TableTest app = new TableTest();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public TableTest()
{
super("Table Example");
btnAdd = new JButton("Add");
btnAdd.addActionListener(this);
model = new DefaultTableModel()
{
public Class getColumnClass(int col)
{
switch (col)
{
case 1 :
return Boolean.class;
default :
return Object.class;
}
}
};
table = new JTable(model);
table.setPreferredSize(new Dimension(250,200));
// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");
JCheckBox cbox = new JCheckBox();
// Append a row
JPanel painel = new JPanel();
model.addRow(new Object[] { "v1",new Boolean(false)});
model.addRow(new Object[] { "v3", new Boolean(false)});
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.createVerticalScrollBar();
btexcluir= new JButton("Excluir");
btexcluir.addActionListener(this);
painel.add(btexcluir);
painel.add(btnAdd);
getContentPane().add(scrollPane, BorderLayout.NORTH);
getContentPane().add(painel, BorderLayout.SOUTH);
setSize(600, 600);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == btnAdd)
model.addRow(new Object[] { "Karl", new Boolean(false)});
if (e.getSource()==btexcluir){
for (int i=0; i <=model.getRowCount(); i++){
if (model.getValueAt(i,1)==Boolean.FALSE)
JOptionPane.showMessageDialog(null, "No lines to remove!!");
else if(model.getValueAt(i,1)==Boolean.TRUE)
model.removeRow(i);
}
}}
}
It's almost the way i want, but I'd like to remove the lines at the same time that I click the button to remove!!!