Hi..
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));
table.setFillsViewportHeight(true);
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))
{
System.out.println(mod.getValueAt(i,j));
group1.add((JRadioButton)mod.getValueAt(i,j));
((JRadioButton)mod.getValueAt(i,j)).setFont(new Font("Dialog", Font.PLAIN, 12));
}
}
}
System.out.println("---------------------------------");
int j=0;
for (int i=0; i<5; i++)
{
System.out.println(i);
if(mod.getValueAt(j,i).getClass()!=JTextField.class){
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.
add(scrollPane);
}
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];
else
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() + ")");
}
if((value!=null)&&(value.getClass()==JRadioButton.class))
{ data[row][col] = value;
fireTableCellUpdated(row, col);
System.out.println(row+" "+col);
DEBUG=true;
}
;
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
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]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* 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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
RadioButtonInJTable newContentPane = new RadioButtonInJTable();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
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())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} 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() {
createAndShowGUI();
}
});
}
}
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) {
super(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;
button.addItemListener(this);}
return (Component)value;
}
public Object getCellEditorValue() {
if(button instanceof JRadioButton)
button.removeItemListener(this);
return button;
}
public void itemStateChanged(ItemEvent e) {
System.out.println(e.getSource());
super.fireEditingStopped();
}
}
class NoRadioButtonEditor extends DefaultCellEditor
{
private JTextField field;
public NoRadioButtonEditor(JTextField arg0) {
super(arg0);
}
public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column)
{
if (value==null) return null;
field=(JTextField)value;
field.setEnabled(false);
field.setVisible(false);
return(Component)field ;
}
}
I am unable to understand the reason for chang of textbox to a radiobutton..
please suggest..