I want a JTable
which consists of 2 columns(having RadioButton
) Active and Inactive such that if Active Column is Selected
then Inactive RadioButton gets Unselected
and Vice versa means that in a single row only 1 radiobutton can be selected out of 2 radiobuttons. the code for radiobutton is below . I am not able 2 add buttongroup for this 2 columns.
public class NewJFrame extends javax.swing.JFrame {
DefaultTableModel dt;
public JRadioButton radioButton=new JRadioButton();
public class RadioButtonCellEditorRenderer extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener {
public RadioButtonCellEditorRenderer() {
radioButton.addActionListener(this);
radioButton.setOpaque(false);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
radioButton.setSelected(Boolean.TRUE.equals(value));
return radioButton;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
radioButton.setSelected(Boolean.TRUE.equals(value));
return radioButton;
}
@Override
public void actionPerformed(ActionEvent e) {
stopCellEditing();
}
@Override
public Object getCellEditorValue() {
return radioButton.isSelected();
}
}
public NewJFrame() {
initComponents();
for(int i=0;i <10;i++)
{
//ButtonGroup bp[i]= new ButtonGroup();
dt.addRow(new Object[]{null,false,false});
}
// jTable1.setValueAt(false, 5, 1);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jScrollPane4 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
dt= (new javax.swing.table.DefaultTableModel(new Object[][]{{null,null,null}},new String [] {"Name","Select", "Unselect"})
{
Class[] types = new Class [] {java.lang.Integer.class,java.lang.String.class, java.lang.String.class};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int r,int c)
{
return true;
}
});
//JTextField textBox= new JTextField();
jTable1.setModel(dt);
TableColumn column = jTable1.getColumnModel().getColumn(1);
column.setCellEditor(new RadioButtonCellEditorRenderer());
column.setCellRenderer(new RadioButtonCellEditorRenderer());
TableColumn column1 = jTable1.getColumnModel().getColumn(2);
column1.setCellEditor(new RadioButtonCellEditorRenderer());
column1.setCellRenderer(new RadioButtonCellEditorRenderer());
jTable1.getTableHeader().setReorderingAllowed(false);
jScrollPane4.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(13, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(29, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
@Override
annotation. 2) Use static factories when available, e.g.getCellEditorValue()
canreturn Boolean.valueOf(btn.isSelected())
. 3) CalladdItemListener()
once in constructor, and simplify logic, e.g.btn.setSelected(((Boolean) value).booleanValue())
4) Omit redundant caste insetValueAt()
. – Templet