给jtable添加单选框的代码
时间: 2023-06-05 07:06:33 浏览: 102
以下是给 JTable 添加单选框的示例代码:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestJTable extends JFrame {
private JTable table;
private JRadioButton radioButton1;
private JRadioButton radioButton2;
private ButtonGroup buttonGroup;
public TestJTable() {
setTitle("JTable with Radio Buttons");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);
// Create the table model
String[] columnNames = {"Name", "Age", "Gender"};
Object[][] data = {
{"John", new Integer(30), new Boolean(false)},
{"Mary", new Integer(25), new Boolean(true)},
{"Bob", new Integer(40), new Boolean(false)},
{"Dan", new Integer(20), new Boolean(true)}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
// Create the table with the model
table = new JTable(model);
table.setAutoCreateRowSorter(true);
// Set the checkbox column editor and renderer
TableColumn column = table.getColumnModel().getColumn(2);
column.setCellEditor(new RadioButtonEditor());
column.setCellRenderer(new RadioButtonRenderer());
// Add the table to a scroll pane
JScrollPane scrollPane = new JScrollPane(table);
// Add the radio buttons to a button group
radioButton1 = new JRadioButton("Male");
radioButton2 = new JRadioButton("Female");
buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
// Add the radio buttons to the content pane
JPanel radioPanel = new JPanel();
radioPanel.add(radioButton1);
radioPanel.add(radioButton2);
add(radioPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
// The RadioButtonEditor class
class RadioButtonEditor extends DefaultCellEditor implements ActionListener {
private JRadioButton button;
public RadioButtonEditor() {
super(new JCheckBox());
button = new JRadioButton();
button.addActionListener(this);
button.setHorizontalAlignment(JRadioButton.CENTER);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value == null) {
button.setSelected(false);
} else {
button.setSelected((Boolean) value);
}
return button;
}
public Object getCellEditorValue() {
return button.isSelected();
}
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
}
// The RadioButtonRenderer class
class RadioButtonRenderer implements TableCellRenderer {
private JRadioButton button;
public RadioButtonRenderer() {
button = new JRadioButton();
button.setHorizontalAlignment(JRadioButton.CENTER);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) {
button.setSelected(false);
} else {
button.setSelected((Boolean) value);
}
return button;
}
}
public static void main(String args[]) {
TestJTable frame = new TestJTable();
frame.setVisible(true);
}
}
```
该示例中创建了一个 JTable,其中包含一个带有单选框的 Gender 列。RadioButtonEditor 和 RadioButtonRenderer 类用于设置单选框的编辑器和渲染器。RadioButtonEditor 用于设置单元格编辑器,RadioButtonRenderer 用于设置单元格呈现器。该示例还包含使用按钮组的单选按钮。
阅读全文