I have downloaded a code to add JRadioBuotton to JTable, modified it.Please see the code..What i wanted was to have blank cell...i was getting a NullPointerException for this so i tried adding a JTextBox for instead. When I click on only radiobuttons the code works fine.. if i click textbox and then a radiobutton, textbox is getting replaced by radioButton

package components;


  • TableDemo.java requires no other files.

import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


  • TableDemo is just like SimpleTableDemo, except that it
  • uses a custom TableModel.
    public class RadioButtonInJTable extends JPanel {
    private boolean DEBUG = false;

    public RadioButtonInJTable() {
    super(new GridLayout(1,0));
    MyTableModel mod=new MyTableModel();
    JTable table = new JTable(mod);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    ButtonGroup group1 = new ButtonGroup();
    for (int i=0; i < 4; i++)
    for (int j=0; j<5; j++)
    if ((mod.getValueAt(i,j)!=null)&&(mod.getValueAt(i,j).getClass()!=JTextField.class))

                ((JRadioButton)mod.getValueAt(i,j)).setFont(new Font("Dialog", Font.PLAIN, 12));
    int j=0;
    for (int i=0; i<5; i++)
        table.getColumnModel().getColumn(i).setCellRenderer(new RadioButtonRenderer());    
        table.getColumnModel().getColumn(i).setCellEditor(new RadioButtonEditor(new JCheckBox()));  
        else {
            table.getColumnModel().getColumn(i).setCellRenderer(new TableCellRenderer(){
                public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column) {
                                  if (value==null) return null;
                                        return (Component)value;
            table.getColumnModel().getColumn(i).setCellEditor(new NoRadioButtonEditor(new JTextField())); 
    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table); ...