给jtable添加单选框的代码
时间: 2023-06-05 08:06:31 浏览: 141
以下是给JTable添加单选框的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class JTableWithRadioButtonExample {
private JFrame frame;
private JTable table;
private MyTableModel model;
private MyCellRenderer renderer;
private List<Boolean> checkedList;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTableWithRadioButtonExample window = new JTableWithRadioButtonExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JTableWithRadioButtonExample() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("Check");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < checkedList.size(); i++) {
System.out.println("Row " + i + ": " + checkedList.get(i));
}
}
});
panel.add(btnNewButton);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
model = new MyTableModel();
model.addColumn("Name");
model.addColumn("Select");
renderer = new MyCellRenderer();
table = new JTable(model) {
public TableCellRenderer getCellRenderer(int row, int column) {
if (column == 1) {
return renderer;
} else {
return super.getCellRenderer(row, column);
}
}
public Class getColumnClass(int column) {
if (column == 1) {
return JCheckBox.class;
} else {
return super.getColumnClass(column);
}
}
};
scrollPane.setViewportView(table);
checkedList = new ArrayList<Boolean>();
model.addRow(new Object[] {"John", new Boolean(false)});
checkedList.add(new Boolean(false));
model.addRow(new Object[] {"Jane", new Boolean(false)});
checkedList.add(new Boolean(false));
model.addRow(new Object[] {"Jim", new Boolean(false)});
checkedList.add(new Boolean(false));
}
class MyTableModel extends DefaultTableModel {
public boolean isCellEditable(int row, int col) {
if (col == 1) {
return true;
} else {
return false;
}
}
}
class MyCellRenderer implements TableCellRenderer {
private JCheckBox box;
public MyCellRenderer() {
box = new JCheckBox();
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
box.setSelected((Boolean)value);
return box;
}
}
}
```
这段代码会创建一个含有单选框的JTable,并且每个单选框对应一个布尔值,当单选框被选中时,布尔值为true,否则为false。用户可以通过点击按钮来打印所有行的布尔值,以检查哪些单选框被选中了。
阅读全文