How to Add Checkboxes to Jtable Swing

Add CheckBox to jTable?

you need to implement getColumnClass(int columnIndex) in your TableModel, and make sure it will return Boolean for the column you want to contain checkboxes.

Take a look at this guide, it's very useful:
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

adding check box in the jTable

You have to take a look to Concepts: Editors and Renderers section of How to Use Tables tutorial.

This JCheckBox you're looking for is the default renderer/editor for Boolean class. Having said this JTable makes use of TableModel.getColumnClass() to decide the proper renderer/editor. If you use DefaultTableModel the implementation of the aforementioned method always return Object.class so you would have to override it to return Boolean.class. For instance let's say the first column will contain booleans:

DefaultTableModel model = new DefaultTableModel() {
@Override
Class<?> getColumnClass(int columnIndex) {
return columnIndex == 0 ? Boolean.class : super.getColumnClass(columnIndex);
}
};

It is all well explained in the linked tutorial.

Addendum

Another approach is shown in this Q&A: Checkbox in only one JTable Cell. This is useful when a given column may contain different types of values (booleans, numbers, strings...) so overriding getColumnClass() is not feasible. Don't think it's your case but it might be helpful.


also i want to know that how do i be able to find out that which check
box is checked and how to use the variable to respond the request of
the multiple deletes

Just iterate over the rows asking for the column value (true/false). If it's "selected" (true) then delete it:

TableModel model = table.getModel();
for(int i = 0; i < model.getRowCount(); i++) {
if((Boolean)model.getValueAt(i, 0)) {
// delete the row
}
}

Off-topic

Database calls are time consuming tasks and may block the Event Dispatch Thread (a.k.a. EDT) causing the GUI become unresponsive. The EDT is a single and special thread where Swing components creation and update take place. To avoid block this thread consider use a SwingWorker to perform database calls in a background thread and update Swing components in the EDT. See more in Concurrency in Swing trail.

How to make JTable column contain checkboxes?

There's no need to create your own table renderer. Here's a simpler example. Just create a custom table model and for a given column return the class Boolean for:

public Class getColumnClass(int column)

If you want the column to be editable, return true for

public boolean isCellEditable(int row, int column)

JTable takes care of the rendering for you.

Another example is here.

How to get checkbox on all rows in for loop JTable?

Read the section from the Swing tutorial on How to Use Tables for the basics and working examples to get you started.

The keys are:

  1. You need to override the getColumnClass(...) method of your DefaultTableModel to return Boolean.class for the column with the combo boxes.

  2. Then in your loop where you set the data you will need to add data to your TableModel.

So the code would be something like:

table.setValueAt(i, i, 0);
table.setValueAt(Boolean.FALSE, I, 1); // add the check box

I want to do it without the Object[ ] method

Yes, well that is the better approach. You should never create a TableModel with a fixed size. Create the table with 0 rows and then just use the addRow(...) method to let the table dynamically grow as you add rows of data.

This is better than using setValueAt(...).

How to add JCheckBox in JTable?

Start with How to use tables.

Your table model needs several things.

  1. It needs to return Boolean.class from the getColumnClass method for the appropriate column. You will need to override this method.
  2. The method isCellEditable will need to return true for the table column you want to make editable (so the user can change the value of the column)
  3. You're table model will need to be capable of holding the value for the column
  4. Make sure you pass a valid value for the boolean column for the row, otherwise it will be null

Updated with simple example

Sample Image

import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TableTest {

public static void main(String[] args) {
new TableTest();
}

public TableTest() {
startUI();
}

public void startUI() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

MyTableModel model = new MyTableModel();
model.addRow(new Object[]{0, "Brian", false});
model.addRow(new Object[]{1, "Ned", false});
model.addRow(new Object[]{2, "John", false});
model.addRow(new Object[]{3, "Drogo", false});
JTable table = new JTable(model);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class MyTableModel extends DefaultTableModel {

public MyTableModel() {
super(new String[]{"ID", "Name", "Present"}, 0);
}

@Override
public Class<?> getColumnClass(int columnIndex) {
Class clazz = String.class;
switch (columnIndex) {
case 0:
clazz = Integer.class;
break;
case 2:
clazz = Boolean.class;
break;
}
return clazz;
}

@Override
public boolean isCellEditable(int row, int column) {
return column == 2;
}

@Override
public void setValueAt(Object aValue, int row, int column) {
if (aValue instanceof Boolean && column == 2) {
System.out.println(aValue);
Vector rowData = (Vector)getDataVector().get(row);
rowData.set(2, (boolean)aValue);
fireTableCellUpdated(row, column);
}
}

}

}

Ps- I would HIGHLY recommend you avoid form editors until you have a better understanding of how Swing works - IMHO



Related Topics



Leave a reply



Submit