java中在jtable中加入了复选框,如何重置复选框
答案:3 悬赏:60 手机版
解决时间 2021-11-19 13:04
- 提问者网友:佞臣
- 2021-11-18 13:16
java中在jtable中加入了复选框,如何重置复选框
最佳答案
- 五星知识达人网友:患得患失的劫
- 2021-11-18 14:20
TableColumn aColumn = jTable1.getColumnModel().getColumn(0);
aColumn.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
aColumn.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
aColumn.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
aColumn.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
全部回答
- 1楼网友:轻雾山林
- 2021-11-18 14:52
document.getElementsByName("复选框的名字")追问不是,java中在jtable中加入了复选框,如何重置复选框,请看清楚一些
- 2楼网友:痴妹与他
- 2021-11-18 14:28
方法1.
实现
TableCellRenderer
TableCellEditor
Object[] row = .....
row[x] = new JCheckbox()
model.addRow(row);
方法2
继承DefaultTableModel
public class CheckTableModle extends DefaultTableModel {
重写
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
Object[] row = .....
row[x] = false
model.addRow(row);
上面两种方法都可以追问可以写详细点吗追答好的,下午给一个demo
刚才也有些看走眼了,以为是你要放入复选框
现在看来你已经功能放入了,但不能控制,
也看了你的补充,是用第一种方法实现的。可以参考一下下边的代码。
for (int row = 0; row < table.getRowCount(); row++) {
JCheckBox check = ((JCheckBox) table.getValueAt(row, column));
}
--------------------------------------------------------------------------------------------------
public class Table extends JFrame {
private JTable table;
private DefaultTableModel model;
private JCheckBox chckbxAll = null;
public Table() {
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 41, 418, 215);
getContentPane().add(scrollPane);
model = new DefaultTableModel(new String[] { "1", "2", "3" }, 0);
table = new JTable(model);
scrollPane.setViewportView(table);
table.getColumnModel().getColumn(0).setCellEditor(new CellEditor());
table.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
chckbxAll = new JCheckBox("all");
chckbxAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < table.getRowCount(); row++) {
JCheckBox check = (JCheckBox) table.getValueAt(row, 0);
check.setSelected(chckbxAll.isSelected());
}
table.updateUI();
}
});
chckbxAll.setBounds(12, 14, 103, 21);
getContentPane().add(chckbxAll);
for (int i = 0; i < 3; i++) {
Object[] row = { new JCheckBox(), "b", "c" };
model.addRow(row);
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(460, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - getWidth()) / 2,
(screenSize.height - getHeight()) / 2);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Table();
}
}
字不够了,
TableCellEditor
TableCellRenderer
的实现就不贴了追问还是实现不了点取消按钮将所有复选框都重置为未选状态还是实现不了点取消按钮将所有复选框都重置为未选状态追答for (int row = 0; row < table.getRowCount(); row++) {
JCheckBox check = (JCheckBox) table.getValueAt(row, 0);
check.setSelected(chckbxAll.isSelected());
}
就是这一块啊,
你把CheckBox放以了cell里,再从指写的行和列中取出来设置一下就可以了呀
你出的对象是什么?追问解决了,谢谢,就差写一个repaint函数了
实现
TableCellRenderer
TableCellEditor
Object[] row = .....
row[x] = new JCheckbox()
model.addRow(row);
方法2
继承DefaultTableModel
public class CheckTableModle extends DefaultTableModel {
重写
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
Object[] row = .....
row[x] = false
model.addRow(row);
上面两种方法都可以追问可以写详细点吗追答好的,下午给一个demo
刚才也有些看走眼了,以为是你要放入复选框
现在看来你已经功能放入了,但不能控制,
也看了你的补充,是用第一种方法实现的。可以参考一下下边的代码。
for (int row = 0; row < table.getRowCount(); row++) {
JCheckBox check = ((JCheckBox) table.getValueAt(row, column));
}
--------------------------------------------------------------------------------------------------
public class Table extends JFrame {
private JTable table;
private DefaultTableModel model;
private JCheckBox chckbxAll = null;
public Table() {
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 41, 418, 215);
getContentPane().add(scrollPane);
model = new DefaultTableModel(new String[] { "1", "2", "3" }, 0);
table = new JTable(model);
scrollPane.setViewportView(table);
table.getColumnModel().getColumn(0).setCellEditor(new CellEditor());
table.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
chckbxAll = new JCheckBox("all");
chckbxAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < table.getRowCount(); row++) {
JCheckBox check = (JCheckBox) table.getValueAt(row, 0);
check.setSelected(chckbxAll.isSelected());
}
table.updateUI();
}
});
chckbxAll.setBounds(12, 14, 103, 21);
getContentPane().add(chckbxAll);
for (int i = 0; i < 3; i++) {
Object[] row = { new JCheckBox(), "b", "c" };
model.addRow(row);
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(460, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - getWidth()) / 2,
(screenSize.height - getHeight()) / 2);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Table();
}
}
字不够了,
TableCellEditor
TableCellRenderer
的实现就不贴了追问还是实现不了点取消按钮将所有复选框都重置为未选状态还是实现不了点取消按钮将所有复选框都重置为未选状态追答for (int row = 0; row < table.getRowCount(); row++) {
JCheckBox check = (JCheckBox) table.getValueAt(row, 0);
check.setSelected(chckbxAll.isSelected());
}
就是这一块啊,
你把CheckBox放以了cell里,再从指写的行和列中取出来设置一下就可以了呀
你出的对象是什么?追问解决了,谢谢,就差写一个repaint函数了
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯