Using Setvalueat to Recreate Mutually Exclusive Check Boxes

Using setValueAt to recreate mutually exclusive check boxes

Don't add your own MouseListener. Instead override setValueAt() in the TableModel to see the new value set by the default editor for Boolean.class.

Addendum: Here's an sscce. For expedience, it simply clears all entries in CHECK_COL, sets the new value and conditions the button accordingly.

CheckOne image

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
* @see http://stackoverflow.com/questions/7920068
* @see http://stackoverflow.com/questions/4526779
*/
public class CheckOne extends JPanel {

private static final int CHECK_COL = 1;
private static final Object[][] DATA = {
{"One", Boolean.FALSE}, {"Two", Boolean.FALSE},
{"Three", Boolean.FALSE}, {"Four", Boolean.FALSE},
{"Five", Boolean.FALSE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.FALSE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.FALSE}, {"Ten", Boolean.FALSE}};
private static final String[] COLUMNS = {"Number", "CheckBox"};
private DataModel dataModel = new DataModel(DATA, COLUMNS);
private JTable table = new JTable(dataModel);
private ControlPanel cp = new ControlPanel();

public CheckOne() {
super(new BorderLayout());
this.add(new JScrollPane(table));
this.add(cp, BorderLayout.SOUTH);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setPreferredScrollableViewportSize(new Dimension(250, 175));
}

private class DataModel extends DefaultTableModel {

public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}

@Override
public void setValueAt(Object aValue, int row, int col) {
if (col == CHECK_COL) {
for (int r = 0; r < getRowCount(); r++) {
super.setValueAt(false, r, CHECK_COL);
}
}
super.setValueAt(aValue, row, col);
cp.button.setEnabled(any());
}

private boolean any() {
boolean result = false;
for (int r = 0; r < getRowCount(); r++) {
Boolean b = (Boolean) getValueAt(r, CHECK_COL);
result |= b;
}
return result;
}

@Override
public Class<?> getColumnClass(int col) {
if (col == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(col);
}

@Override
public boolean isCellEditable(int row, int col) {
return col == CHECK_COL;
}
}

private class ControlPanel extends JPanel {

JButton button = new JButton("Button");

public ControlPanel() {
button.setEnabled(false);
this.add(new JLabel("Selection:"));
this.add(button);
}
}

private static void createAndShowUI() {
JFrame frame = new JFrame("CheckOne");
frame.add(new CheckOne());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowUI();
}
});
}
}

How to Add Listener to List of Checkboxes in JavaSwing

As suggested in a comment by @MadProgrammer and shown here, here and here, you can use JTable for the view and store Boolean values in the corresponding TableModel.

image

Unchecked checkbox in JTable gives NullPointerException

The default table cell editor may initially accept null, but your loop does not. As shown here and here, the model values rendered as unchecked should be set to Boolean.FALSE or false explicitly when you fetch the table's data.

How to use check box listener with a dynamic JTable?

The default cell editor for an editable column of type Boolean.class is a JCheckBox. Only the editor needs to listen to the checkbox, and only while the checkbox is being edited. After the editor concludes, examine the TableModel for changes. If another component needs to know about such changes, it should register as a TableModelListener, illustrated here. Related examples may be found here and here.

Creating table model from resultset with checkbox

Finally i solve the issue. Let share my code;

Table Model Class;

class MyTableModel extends AbstractTableModel {

private String[] columnNames = {"Mach",
"ID",
"Status",
"IsActive"};

private Vector<Vector<Object>> data = new Vector<Vector<Object>>();

public MyTableModel() {try {
fetchDB();
} catch (Exception ex) {
Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void fetchDB ()throws Exception{

ResultSet rs;
Statement stmt;
ResultSetMetaData rsmtd;

String query = "select Mach,ID,Status,IsActive from Configs\n" +
"order by Machine,ID";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.168.100.100;" + "databaseName=DBST;" + "user=" + "user1" + ";" + "password=" + "user1pass" + ";";
Connection con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
rs = stmt.executeQuery(query);
rsmtd = rs.getMetaData();

int columnCount = rsmtd.getColumnCount();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);

}

}

catch(SQLException se){
se.printStackTrace();
}
catch (ClassNotFoundException ex) {
Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
}
}

@Override
public int getColumnCount() {
return columnNames.length;
}

@Override
public int getRowCount() {
return data.size();
}

@Override
public String getColumnName(int col) {
return columnNames[col];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

return data.elementAt(rowIndex).elementAt(columnIndex);
}

@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

}

Main Gui;

jTable1 = new javax.swing.JTable (new MyTableModel());

Set JTable Cell Disable or Enable based on condition?

Based on this example, the code below conditions the isCellEditable() implementation based on the model's state for that row. Once checked, a row cannot be unchecked. You can alter the appearance of such rows as desired using a suitable renderer, as shown here.

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
* @see https://stackoverflow.com/a/31082475/230513
* @see https://stackoverflow.com/questions/7920068
* @see https://stackoverflow.com/questions/4526779
*/
public class CheckOnce extends JPanel {

private static final int CHECK_COL = 1;
private static final Object[][] DATA = {
{"One", Boolean.FALSE}, {"Two", Boolean.FALSE},
{"Three", Boolean.FALSE}, {"Four", Boolean.FALSE},
{"Five", Boolean.FALSE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.FALSE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.FALSE}, {"Ten", Boolean.FALSE}};
private static final String[] COLUMNS = {"Number", "CheckBox"};
private DataModel dataModel = new DataModel(DATA, COLUMNS);
private JTable table = new JTable(dataModel);

public CheckOnce() {
super(new BorderLayout());
this.add(new JScrollPane(table));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setPreferredScrollableViewportSize(
new Dimension(250, 10 * table.getRowHeight()));
}

private class DataModel extends DefaultTableModel {

public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}

@Override
public void setValueAt(Object aValue, int row, int col) {
super.setValueAt(aValue, row, col);
}

@Override
public Class<?> getColumnClass(int col) {
if (col == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(col);
}

@Override
public boolean isCellEditable(int row, int col) {
Object o = getValueAt(row, col);
boolean b = o instanceof Boolean && (Boolean) o;
return col == CHECK_COL && !b;
}
}

private static void createAndShowUI() {
JFrame frame = new JFrame("CheckOne");
frame.add(new CheckOnce());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowUI();
}
});
}
}

Multiple row selection in JTable

Using @Hovercraft's example and @camickr's advice, the example below shows a suitable user interface. Although it uses buttons, the SelectionAction would also be suitable for a menu or popup.

Check A Bunch

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.DefaultListSelectionModel;
import javax.swing.table.DefaultTableModel;

/** @see http://stackoverflow.com/questions/4526779 */
public class CheckABunch extends JPanel {

private static final int CHECK_COL = 1;
private static final Object[][] DATA = {
{"One", Boolean.TRUE}, {"Two", Boolean.FALSE},
{"Three", Boolean.TRUE}, {"Four", Boolean.FALSE},
{"Five", Boolean.TRUE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.TRUE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.TRUE}, {"Ten", Boolean.FALSE}};
private static final String[] COLUMNS = {"Number", "CheckBox"};
private DataModel dataModel = new DataModel(DATA, COLUMNS);
private JTable table = new JTable(dataModel);
private DefaultListSelectionModel selectionModel;

public CheckABunch() {
super(new BorderLayout());
this.add(new JScrollPane(table));
this.add(new ControlPanel(), BorderLayout.SOUTH);
table.setPreferredScrollableViewportSize(new Dimension(250, 175));
selectionModel = (DefaultListSelectionModel) table.getSelectionModel();
}

private class DataModel extends DefaultTableModel {

public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}

@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(columnIndex);
}

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

private class ControlPanel extends JPanel {

public ControlPanel() {
this.add(new JLabel("Selection:"));
this.add(new JButton(new SelectionAction("Clear", false)));
this.add(new JButton(new SelectionAction("Check", true)));
}
}

private class SelectionAction extends AbstractAction {

boolean value;

public SelectionAction(String name, boolean value) {
super(name);
this.value = value;
}

@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < dataModel.getRowCount(); i++) {
if (selectionModel.isSelectedIndex(i)) {
dataModel.setValueAt(value, i, CHECK_COL);
}
}
}
}

private static void createAndShowUI() {
JFrame frame = new JFrame("CheckABunch");
frame.add(new CheckABunch());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowUI();
}
});
}
}


Related Topics



Leave a reply



Submit