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]
# 1
as you are using the default editor for the table, just use the getValueAt() in the table, which will return a boolean. ( this will be based on the current state of the checkbox)
k_d_ja at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hi, yes. I use getValueAt. But How can I delete a row when there is more than one selected checkbox ?
MrSmyllea at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...
# 3
How can I remove all the selected (using the checkbox) rows at the same time?
MrSmyllea at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...
# 4
How can I remove all the selected (using the checkbox) rows at the same time?
MrSmyllea at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...
# 5
how can you get a little more patience. You expect an answer within 30 seconds? This isn't a chat room.You loop thru the rows and find out which are checked and remove the data for that row from your model.
bsampieria at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...
# 6

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!!!

MrSmyllea at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...
# 7
I resolved the problem!!!It was very easy!!!
MrSmyllea at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...
# 8
Hi , I am facing same problem .So Please tell me how u resolved this problem. This will really help me too.ThanksNitin
Nitin_Aga at 2007-7-16 13:49:18 > top of Java-index,Desktop,Core GUI APIs...