Estou fazendo algo assim:
package br.com.xpto.view;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
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 JRadioButtonTableExample extends JFrame {
public JRadioButtonTableExample(){
super( "Telefones Cliente" );
UIDefaults ui = UIManager.getLookAndFeel().getDefaults();
UIManager.put("RadioButton.focus", ui.getColor("control"));
DefaultTableModel dm = new DefaultTableModel();
List lista = new ArrayList();
lista.add("11 - 69431231");
lista.add("11 - 88888888");
lista.add("11 - 55555555");
lista.add("11 - 33333333");
lista.add("11 - 22222222");
dm.setDataVector(
new Object[][]{
//como carregar isso de uma lista
//for(int i=0;i<lista.size();i++){
{"Residencial",new JRadioButton(lista.get(0).toString())},
{"Celular",new JRadioButton(lista.get(1).toString())},
{"Comercial",new JRadioButton(lista.get(2).toString())},
{"Teste",new JRadioButton(lista.get(3).toString())},
{"Teste2",new JRadioButton(lista.get(4).toString())}},
new Object[]{"Tipo","Telefone"});
JTable table = new JTable(dm) {
public void tableChanged(TableModelEvent e) {
super.tableChanged(e);
repaint();
}
};
ButtonGroup group1 = new ButtonGroup();
for(int i=0;i<lista.size();i++){
group1.add((JRadioButton)dm.getValueAt(i,1));
}
table.getColumn("Telefone").setCellRenderer(new RadioButtonRenderer());
table.getColumn("Telefone").setCellEditor(new RadioButtonEditor(new JCheckBox()));
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 170 );
setVisible(true);
}
public static void main(String[] args) {
JRadioButtonTableExample frame = new JRadioButtonTableExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Eu não saberei a quantidade de números, por isso monto como uma lista:
mas não funciona.
dm.setDataVector(
new Object[][]{
//como carregar isso de uma lista
//for(int i=0;i<lista.size();i++){
{"Residencial",new JRadioButton(lista.get(0).toString())},
{"Celular",new JRadioButton(lista.get(1).toString())},
{"Comercial",new JRadioButton(lista.get(2).toString())},
{"Teste",new JRadioButton(lista.get(3).toString())},
{"Teste2",new JRadioButton(lista.get(4).toString())}},
Nesse trecho não consigo usar for ou ifs, qual a maneira elegante de resolver isso?