PANYAM KRISHNA MURTHY (3/27/01 9:54 AM):
>hi here it is .,
>
>
>http://codeguru.earthweb.com/java/articles/665.shtml
>
>
>It has multiple radio buttons in a button group for a cell. I have used
> Cell
>Renderer and Cell Editor for the table.
>
>To make it simple I have it based on the one already in Codeguru.com
>that explains how to use radio buttons as cell editor and renderer.
>
<snip>
... code removed ...
</snip>
The example forces people to use custom-renderer för every solution involving
JRadioButtons in tables. Also, it has to have an additional renderer to solve the
problems with more than 1 column with JRadioButtons. As advantage it is possible
to use more than one JRadioButton/cell.
I would like to show an alternative which utilises more of the strenghts of
swings table (IMHO).
In the examples down the UI-Delegate is further enhanced to provide the outlook
that is needed, making it easier to mix controlls of different kinds and it is
able to respond imidiatly as soon as a single cell changes into an JRadioButton.
The solution should easily be incorporated on a lower level for a project which
would easily extend the possibilities for programmers to choose when to show
things as JRadioButtons. It would also make it much easier to adapt and change the
behaviour in the future, without having to redo all the JRadioButtons.
With a little home made TableModel, you could easily have an query based upon
rows / columns or link any buttons that is nessecary together, without having
to change or implement any CellEditor/CellRenderer.
This is/was a rather quick hack, but I would like some input about my solution.
Is there any drawbacks with doing it this way? As far as I can see, there would
only be advantages, but I am a bit biased.
Yours sincerely
Peter Norell
Sema, Sweden
Code follows ----------------------------
--- Cell Renderer / Editor ---
// Class implements both renderer and editor to save some writing space. There is no
problems to seperate
// these and it should be done in a real solution.
public class JRadioButtonCellRenderer extends JRadioButton
implements TableCellRenderer, TableCellEditor
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
if (isSelected)
{
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
else
{
setForeground(table.getForeground() );
setBackground(table.getBackground() );
}
// If the model is our higly expanded Model
// we rip the text and make sure it shows the correct text.
if( value instanceof ExpandedToggleButtonModel )
{
ExpandedToggleButtonModel dm = (ExpandedToggleButtonModel)value;
setText( dm.getText() );
}
if( value instanceof ButtonModel )
{
setModel((ButtonModel)value);
}
return this;
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
{
if (isSelected)
{
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
else
{
setForeground(table.getForeground() );
setBackground(table.getBackground() );
}
// If the model is our higly expanded Model
// we rip the text and make sure it shows the correct text.
if( value instanceof ExpandedToggleButtonModel )
{
ExpandedToggleButtonModel dm = (ExpandedToggleButtonModel)value;
setText( dm.getText() );
}
if( value instanceof ButtonModel )
{
setModel((ButtonModel)value);
}
return this;
}
public Object getCellEditorValue()
{
return getModel();
}
public boolean shouldSelectCell(java.util.EventObject eo)
{
return true;
}
public boolean isCellEditable(EventObject anEvent)
{
return true;
}
public void cancelCellEditing()
{
fireEditingCanceled();
}
public boolean stopCellEditing()
{
fireEditingStopped();
return true;
}
protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
// implements javax.swing.CellEditor
public void addCellEditorListener(CellEditorListener l)
{
listenerList.add(CellEditorListener.class, l);
}
// implements javax.swing.CellEditor
public void removeCellEditorListener(CellEditorListener l)
{
listenerList.remove(CellEditorListener.class, l);
}
protected void fireEditingStopped()
{
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length-2; i>=0; i-=2)
{
if (listeners[i]==CellEditorListener.class)
{
// Lazily create the event:
if (changeEvent == null)
{
changeEvent = new ChangeEvent(this);
}
((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled()
{
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length-2; i>=0; i-=2)
{
if (listeners[i]==CellEditorListener.class)
{
// Lazily create the event:
if (changeEvent == null)
{
changeEvent = new ChangeEvent(this);
}
((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
}
}
}
}
--- ExpandedToggleButtonModel ---
// Used since the original ButtonModel has all the state-information for a button
_except_
// the text. This is primitively changed by making an ExpandedToggleButtonModel.
public class ExpandedToggleButtonModel extends JToggleButton.ToggleButtonModel
{
private String text;
public ExpandedToggleButtonModel(String text)
{
this.text = text;
}
public String getText()
{
return text;
}
}
--- Example usage class ---
// This is just a class to show an example on how to
// use the setup
public class ButtonGroupTableExample extends JFrame
{
public ButtonGroupTableExample()
{
// To be able to use it fully, it would preferably
// use an TableModel that returns an correct class
DefaultTableModel dm = new DefaultTableModel() {
public Class getColumnClass(int columnIndex)
{
return columnIndex > 2 ? Object.class:ExpandedToggleButtonModel.class;
}
};
int ROWS = 10;
// In example cols >= 3 or it will bug!
int COLS = 5;
Object[][] items = new Object[ROWS][COLS];
Object[] header = new Object[COLS];
for( int i = 0; i < COLS; i++)
{
header[i] = "Header "+i;
}
for( int i = 0; i < ROWS; i++)
{
items[i][0] = "Some text";
items[i][1] = "Another text";
// ButtonGroup for each row to handle
// the buttons that belong together
ButtonGroup bg = new ButtonGroup();
for(int j = 2; j < COLS; j++)
{
ButtonModel bm = new ExpandedToggleButtonModel("radio "+ (i+j) );
// Here we link the buttons to the button group
// Note: There would not be much of a problem to link in columns
// rather than rows. It will have to make sure that the correct
// column is repainted when changed.
bm.setGroup( bg );
items[i][j] = bm;
}
}
dm.setDataVector(items, header);
JTable table = new JTable(dm);
// Here we set the default editor and renderer
// to the JRadioButtonCellRenderer.
table.setDefaultEditor( DaftModel.class , new JRadioButtonCellRenderer() );
table.setDefaultRenderer( DaftModel.class , new JRadioButtonCellRenderer() );
// Editor and renderer should not be the same object since it
// easily causes problems for rendering while editing.
table.setRowHeight(30);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 400);
setVisible(true);
}
}
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing