Hello!
I encountered a problem using radioButton as cell editor/renderer.
The first column works like a radio button, where only one tableentry may
have that option checked. I used ClientContext.setModelUpdateMode(
table.getModel(),
UlcEventConstants.SYNCHRONOUS_MODE). However, at the same time ,
I select two tableentry (Wrong radio button behaviour: Only one tableentry
should be selectable).
Can you help me to solve problem use radio button as cell editor/renderer in
table?
May be the attached snipped will help you to show the problem. You have to
play with it and try to uncheck the radioButton of the first column.
It seems, the 'fireTableDataChanged()' doesn't make his job because of a
focus problem.
Thank you and best regards,
Nguyen.
/**
* ------------------ BEGIN: SNIPPED ------------------
*/
package com.canoo.snipped;
import java.awt.Component;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.ComboBoxEditor;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ApplicationContext;
import com.ulcjava.base.application.ClientContext;
import com.ulcjava.base.application.IEditorComponent;
import com.ulcjava.base.application.IRendererComponent;
import com.ulcjava.base.application.ULCAlert;
import com.ulcjava.base.application.ULCDialog;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCListSelectionModel;
import com.ulcjava.base.application.ULCMenuItem;
import com.ulcjava.base.application.ULCPopupMenu;
import com.ulcjava.base.application.ULCRadioButton;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTable;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.event.IListSelectionListener;
import com.ulcjava.base.application.event.IWindowListener;
import com.ulcjava.base.application.event.ListSelectionEvent;
import com.ulcjava.base.application.event.WindowEvent;
import com.ulcjava.base.application.table.AbstractTableModel;
import com.ulcjava.base.application.table.ITableCellEditor;
import com.ulcjava.base.application.table.ITableCellRenderer;
import com.ulcjava.base.application.table.ULCTableColumn;
import com.ulcjava.base.client.UIRadioButton;
import com.ulcjava.base.client.UITable;
import com.ulcjava.base.client.tabletree.TableTreeCellEditor;
import com.ulcjava.base.client.tabletree.TableTreeCellRenderer;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.base.server.ICellComponent;
import com.ulcjava.base.shared.UlcEventConstants;
public class ULCRadioButtonCellEditorSnippet extends AbstractApplication {
private ULCFrame frame;
public void start() {
this.frame = new ULCFrame("ULCTable Radio Button CellEditor Sample");
frame.setSize(100, 250);
ULCTable table = new ULCTable();
table.setModel(new DummyTableModel());
ClientContext.setModelUpdateMode(table.getModel(),
UlcEventConstants.SYNCHRONOUS_MODE);
ULCPopupMenu pop = new ULCPopupMenu("Test");
pop.add(new ULCMenuItem("ABC"));
table.setComponentPopupMenu(pop);
table.getSelectionModel().addListSelectionListener(new
IListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
System.out.println("Selection changed - "
+ "Row: " +
((ULCListSelectionModel)event.getSource()).getMinSelectionIndex());
}
});
for (Enumeration columns = table.getColumnModel().getColumns();
columns.hasMoreElements() ;) {
final ULCTableColumn column = (ULCTableColumn) columns.nextElement();
column.setCellRenderer(new ITableCellRenderer() {
public IRendererComponent getTableCellRendererComponent(ULCTable
arg0, Object arg1, boolean arg2, boolean arg3, int arg4) {
return new ULCRadioButtonRenderer();
}
});
column.setCellEditor(new ITableCellEditor() {
public IEditorComponent getTableCellEditorComponent(ULCTable arg0,
Object arg1, int arg2) {
return new ULCRadioButtonEditor();
}
});
}
frame.add(new ULCScrollPane(table));
frame.setVisible(true);
frame.setDefaultCloseOperation(ULCDialog.DISPOSE_ON_CLOSE);
frame.addWindowListener(new IWindowListener() {
public void windowClosing(WindowEvent arg0) {
ApplicationContext.terminate();
}
});
}
public static void main(String[] args) {
DevelopmentRunner.setApplicationClass(ULCRadioButtonCellEditorSnippet.class
);
DevelopmentRunner.main(args);
}
private class DummyTableModel extends AbstractTableModel {
private int currentRow = 0;
public int getRowCount() {
return 10;
}
public int getColumnCount() {
return 1;
}
public Object getValueAt(int row, int column) {
switch (column) {
case 0:
return (row == currentRow) ? Boolean.TRUE : Boolean.FALSE;
default:
return (Math.random() < 0.5) ? Boolean.TRUE : Boolean.FALSE;
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public void setValueAt(Object value, int row, int column) {
if (column == 0) {
if (row == currentRow) {
final ULCAlert alert = new ULCAlert(frame, "Warning", "Not
uncheck!", "OK");
alert.show();
fireTableCellUpdated(row, column);
} else {
this.currentRow = row;
fireTableDataChanged();
}
}
}
}
public static class ULCRadioButtonEditor extends ULCRadioButton
implements
com.ulcjava.base.application.IEditorComponent {
private List actionCopyList = new ArrayList();
public boolean areAttributesEqual(ICellComponent component) {
if (!(component instanceof ULCRadioButtonEditor)) {
return false;
}
ULCRadioButtonEditor other = (ULCRadioButtonEditor)component;
return equals(getBackground(), other.getBackground())
&& equals(getFont(), other.getFont())
&& equals(getForeground(), other.getForeground())
&& (isSelected() == other.isSelected())
&& equals(getToolTipText(), other.getToolTipText()) ;
}
public int attributesHashCode() {
int result = 17;
result = 37 * result + (getBackground() == null ? 0 :
getBackground().hashCode());
result = 37 * result + (getFont() == null ? 0 :
getFont().hashCode());
result = 37 * result + (getForeground() == null ? 0 :
getForeground().hashCode());
result = 37 * result + (getToolTipText() == null ? 0 :
getToolTipText().hashCode());
return result;
}
public void copyAttributes(ICellComponent source) {
ULCRadioButtonEditor sourceRadioButton = (ULCRadioButtonEditor)source;
setBackground(sourceRadioButton.getBackground());
setFont(sourceRadioButton.getFont());
setForeground(sourceRadioButton.getForeground());
}
protected String typeString() {
return UIRadioButtonEditor.class.getName();
}
}
public static class UIRadioButtonEditor extends UIRadioButton
implements com.ulcjava.base.client.IEditorComponent {
private TableCellEditor fTableCellEditor;
protected void postInitializeState() {
super.postInitializeState();
fTableCellEditor = new RadioButtonTableCellEditor();
}
public ComboBoxEditor getComboBoxEditor() {
throw new UnsupportedOperationException();
}
public TableCellEditor getTableCellEditor() {
return fTableCellEditor;
}
public TableTreeCellEditor getTableTreeCellEditor() {
throw new UnsupportedOperationException();
}
public TreeCellEditor getTreeCellEditor() {
throw new UnsupportedOperationException();
}
private class RadioButtonTableCellEditor extends AbstractCellEditor
implements TableCellEditor {
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
getBasicRadioButton().setSelected(((Boolean)value).booleanValue());
return getBasicRadioButton();
}
public Object getCellEditorValue() {
return new Boolean(getBasicRadioButton().isSelected());
}
}
}
public static class ULCRadioButtonRenderer extends ULCRadioButton
implements com.ulcjava.base.application.IRendererComponent {
public ULCRadioButtonRenderer() {
super();
}
/* (non-Javadoc)
* @see com.ulcjava.base.server.ICellComponent#areAttributesEqual(
com.ulcjava.base.server.ICellComponent)
*/
public boolean areAttributesEqual(ICellComponent component) {
if (!(component instanceof ULCRadioButtonRenderer)) {
return false;
}
ULCRadioButtonRenderer other =
(ULCRadioButtonRenderer)component;
return equals(getBackground(), other.getBackground()) &&
equals(getFont(), other.getFont())&&
equals(getForeground(), other.getForeground()) &&
equals(getToolTipText(), other.getToolTipText())&&
equals(isSelected(), other.isSelected()) &&
equals(isEnabled(), other.isEnabled());
}
/* (non-Javadoc)
* @see com.ulcjava.base.server.ICellComponent#attributesHashCode()
*/
public int attributesHashCode() {
int result = 17;
result = 37 * result + (getBackground() == null ? 0 :
getBackground().hashCode());
result = 37 * result + (getFont() == null ? 0 :
getFont().hashCode());
result = 37 * result + (getForeground() == null ? 0 :
getForeground().hashCode());
result = 37 * result + (getToolTipText() == null ? 0 :
getToolTipText().hashCode());
result = 37 * result + (isSelected()? 1 : 0);
result = 37 * result + (isEnabled()? 1 : 0);
return result;
//return super.attributesHashCode();
}
/* (non-Javadoc)
* @see com.ulcjava.base.server.ICellComponent#copyAttributes(
com.ulcjava.base.server.ICellComponent)
*/
public void copyAttributes(ICellComponent source) {
ULCRadioButtonRenderer sourceRadioButton =
(ULCRadioButtonRenderer)source;
setBackground(sourceRadioButton.getBackground());
setFont(sourceRadioButton.getFont());
setForeground(sourceRadioButton.getForeground());
setToolTipText(sourceRadioButton.getToolTipText());
setSelected(sourceRadioButton.isSelected());
setEnabled(sourceRadioButton.isEnabled());
}
protected String typeString() { return
UIRadioButtonRenderer.class.getName(); }
}
public static class UIRadioButtonRenderer extends UIRadioButton
implements com.ulcjava.base.client.IRendererComponent {
private TableCellRenderer fTableCellRenderer;
/* (non-Javadoc)
* @see com.ulcjava.base.client.UITextField#postInitializeState()
*/
protected void postInitializeState() {
super.postInitializeState();
fTableCellRenderer = new RadioButtonTableCellRenderer();
}
/* (non-Javadoc)
* @see com.ulcjava.base.client.IRendererComponent#getTableCellRenderer()
*/
public TableCellRenderer getTableCellRenderer() {
return fTableCellRenderer;
}
/* (non-Javadoc)
* @see com.ulcjava.base.client.IRendererComponent#getTreeCellRenderer()
*/
public TreeCellRenderer getTreeCellRenderer() {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see com.ulcjava.base.client.IRendererComponent#getListCellRenderer()
*/
public ListCellRenderer getListCellRenderer() {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see
com.ulcjava.base.client.IRendererComponent#getTableTreeCellRenderer()
*/
public TableTreeCellRenderer getTableTreeCellRenderer() {
throw new UnsupportedOperationException();
}
private class RadioButtonTableCellRenderer implements TableCellRenderer {
/* (non-Javadoc)
* @see
javax.swing.table.TableCellRenderer#getTableCellRendererComponent(
javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Boolean select = (value instanceof Boolean) ?
(Boolean)value : null;
JRadioButton result = (JRadioButton)getBasicComponent();
if (select!= null) {
result.setSelected(select);
}
return result;
}
}
}
}