witam
mam umieszczone w tabeli różne typy pól jradiobutton i jcheckbox. Problem pojawia się w momencie zaznaczania jradiobutton można zaznaczyć kilka zamiast tylko jednego(są dodane do ButtonGroup), w chwili gdy rozciągnę ramkę lub kliknę na inne pole(różne niż radiobutton ), zostaje zaznaczony radiobutton, który zaznaczono jako ostatni.Brakuje jakby odświezania? [???] Oto kod:
package testy;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
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;
}
button = (JRadioButton) value;
button.addItemListener(this);
return (Component) value;
}
public Object getCellEditorValue() {
button.removeItemListener(this);
return button;
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
}
public class JRBTable extends JFrame {
public JRBTable() {
AbstractTableModel dm = new AbstractTableModel() {
String title[] = {"OSOBA", "RESP", "CHECK"};
public Object[][] cells = {
{1, new JRadioButton(), false},
{2, new JRadioButton(), false},
{3, new JRadioButton(), false}};
public Class<?> getColumnClass(int c) {
return cells[0][c].getClass();
}
public String getColumnName(int c) {
return title[c];
}
public int getRowCount() {
return cells.length;
}
public int getColumnCount() {
return cells[0].length;
}
public Object getValueAt(int r, int c) {
return cells[r][c];
}
@Override
public void setValueAt(Object obj, int r, int c) {
cells[r][c] = obj;
}
@Override
public boolean isCellEditable(int r, int c) {
return c == RESP || c == CHECK;
}
public int RESP = 1, CHECK = 2;
};
JTable table = new JTable(dm) {
public void tableChanged(TableModelEvent e) {
super.tableChanged(e);
repaint();
}
};
ButtonGroup group1 = new ButtonGroup();
group1.add((JRadioButton) dm.getValueAt(0, 1));
group1.add((JRadioButton) dm.getValueAt(1, 1));
group1.add((JRadioButton) dm.getValueAt(2, 1));
table.getColumn("RESP").setCellRenderer(new RadioButtonRenderer());
table.getColumn("RESP").setCellEditor(
new RadioButtonEditor(new JCheckBox()));
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
setSize(200, 140);
setVisible(true);
}
public static void main(String[] args) {
JRBTable frame = new JRBTable();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Jakieś pomysły?