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);

        //Add the scroll pane to this panel.

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"Series One",
                                        "Series Two",
                                        "Series Three",
                                        "Series Four",
                                        "Series Five"
        private Object[][] data = {
	    {new JRadioButton("One"), new JRadioButton("One"),
	    	new JRadioButton("One") , new JRadioButton("One"),new JRadioButton("One")},
	    {new JRadioButton("Two"),new JRadioButton("Two"),
	    		new JRadioButton("Two") ,new JRadioButton("Two"),new JRadioButton("Two")},
	    {new JRadioButton("Three"), new JRadioButton("Three"),
	    			new JRadioButton("Three") ,new JRadioButton("Three"), new JRadioButton("Three")},
	    {new JRadioButton("Four"), new JRadioButton("Four"),
	    new JRadioButton("Four"),new JRadioButton("Four"),null}}  ;

        public int getColumnCount() {
            return columnNames.length;

        public int getRowCount() {
            return data.length;

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

        public Object getValueAt(int row, int col) {
        if( data[row][col]!=null)
            return data[row][col];
        		return(Object)new JTextField();

         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
        public Class getColumnClass(int c) {
            return getValueAt(0, c)!=null?getValueAt(0, c).getClass():String.class;

         * Don't need to implement this method unless your table's
         * editable.
        public boolean isCellEditable(int row, int col) {
                return true;


         * Don't need to implement this method unless your table's
         * data can change.
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            {  data[row][col] = value;
               fireTableCellUpdated(row, col);
               System.out.println(row+"  "+col);

            if (DEBUG) {
                System.out.println("New value of data:");

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);

     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");

        //Create and set up the content pane.
        RadioButtonInJTable newContentPane = new RadioButtonInJTable();
        newContentPane.setOpaque(true); //content panes must be opaque

        //Display the window.

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
    	try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		} catch (Exception e) {
			System.out.println("NIMBUS IS NOT PRESENT");
		    // If Nimbus is not available, you can set the GUI to another look and feel.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

class RadioButtonRenderer implements TableCellRenderer {
	  public Component getTableCellRendererComponent(JTable table, Object value,
	                   boolean isSelected, boolean hasFocus, int row, int column) {
	    if (value==null) return null;
	    return (Component)value;

	class RadioButtonEditor extends    DefaultCellEditor
	                        implements ItemListener {
	  private JRadioButton button;

	  public RadioButtonEditor(JCheckBox checkBox) {

	  public Component getTableCellEditorComponent(JTable table, Object value,
	                   boolean isSelected, int row, int column) {
	    if (value==null) return null;
	    if(value instanceof JRadioButton)
	    { button = (JRadioButton)value;
	    return (Component)value;

	  public Object getCellEditorValue() {
	   if(button instanceof JRadioButton)
	    return button;

	  public void itemStateChanged(ItemEvent e) {
	class NoRadioButtonEditor extends DefaultCellEditor 
		private JTextField field;
		public NoRadioButtonEditor(JTextField arg0) {
		public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) 
		 if (value==null) return null; 
			return(Component)field ;

I am unable to understand the reason for chang of textbox to a radiobutton..

please suggest..

1) if is possible put JCheckBox or JComboBox to the TableCell,

2) if is possible put JPanel with JRadioButtons to the TableCell

3) somehow I don't want to look or test your code, because untill today I never see correct workaroung for JRadioButtons in JTable, correct on CellRenderer and CellEditor side too, just with dirty hacks and sensitive for concrete Java Look and Feel

Edited 5 Years Ago by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.