我使用本指南是为了让它JTable
处理单选按钮。工作正常,除了我需要启用默认启用按钮。
可以有 n 行。我试图通过默认的表格模型启用它Object[][]
,表格,我尝试在将按钮添加到之前启用它Object[][]
。我不知道如何(如果可能的话)使用按钮组来做到这一点。
要找到默认启用的按钮,我必须将按钮文本与字符串进行比较(这部分有效)。
我使用本指南是为了让它JTable
处理单选按钮。工作正常,除了我需要启用默认启用按钮。
可以有 n 行。我试图通过默认的表格模型启用它Object[][]
,表格,我尝试在将按钮添加到之前启用它Object[][]
。我不知道如何(如果可能的话)使用按钮组来做到这一点。
要找到默认启用的按钮,我必须将按钮文本与字符串进行比较(这部分有效)。
不确定我是否正确解释了这个问题。您可以使用JRadioButton
构造函数来设置选择,例如片段(基于 OP 代码示例)将设置选定按钮“B”:
dm.setDataVector(new Object[][] { { "Group 1", new JRadioButton("A") },
{ "Group 1", new JRadioButton("B", true) },
{ "Group 1", new JRadioButton("C") },
{ "Group 2", new JRadioButton("a") },
{ "Group 2", new JRadioButton("b") } }, new Object[] {
"String", "JRadioButton" });
您还可以像这样更改选择:
((JRadioButton) dm.getValueAt(0, 1)).setSelected(true);
也可以使用ButtonGroup.setSelected()
方法。
编辑:从模型中消除组件
模型应该包含数据而不是组件。在模型中存储组件违背了渲染器和编辑器的想法。有关更多详细信息,请参阅编辑器和渲染器以及Swing 模型和渲染器。查看以下示例,该示例模拟了模型中的按钮组行为:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.table.*;
public class ButtonGroupMockupTest {
private static void createAndShowGUI() {
DefaultTableModel model = new DefaultTableModel(new Object[][] {
{ "Group 1", Boolean.FALSE }, { "Group 2", Boolean.FALSE },
{ "Group 3", Boolean.FALSE } },
new Object[] { "Name", "State" }) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int col) {
if (col == 1)
return Boolean.class;
return super.getColumnClass(col);
}
@Override
public void setValueAt(Object value, int row, int col) {
super.setValueAt(value, row, col);
if (col == 1 && value.equals(Boolean.TRUE))
deselectValues(row, col);
}
private void deselectValues(int selectedRow, int col) {
for (int row = 0; row < getRowCount(); row++) {
if (getValueAt(row, col).equals(Boolean.TRUE)
&& row != selectedRow) {
setValueAt(Boolean.FALSE, row, col);
fireTableCellUpdated(row, col);
}
}
}
};
JTable table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setDefaultRenderer(Boolean.class, new BooleanRadionRenderer());
table.setDefaultEditor(Boolean.class, new BooleanRadioEditor());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(table));
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static class BooleanRadionRenderer implements TableCellRenderer, UIResource {
JRadioButton radioButton;
Border emptyBorder;
public BooleanRadionRenderer() {
radioButton = new JRadioButton();
radioButton.setHorizontalAlignment(JRadioButton.CENTER);
radioButton.setBorderPainted(true);
emptyBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
if (isSelected) {
radioButton.setBackground(table.getSelectionBackground());
radioButton.setForeground(table.getSelectionForeground());
} else {
radioButton.setBackground(table.getBackground());
radioButton.setForeground(table.getForeground());
}
if (hasFocus)
radioButton.setBorder(UIManager
.getBorder("Table.focusCellHighlightBorder"));
else
radioButton.setBorder(emptyBorder);
radioButton.setSelected(((Boolean) value).booleanValue());
return radioButton;
}
}
static class BooleanRadioEditor extends AbstractCellEditor
implements TableCellEditor {
private static final long serialVersionUID = 1L;
private JRadioButton radioButton;
public BooleanRadioEditor() {
radioButton = new JRadioButton();
radioButton.setHorizontalAlignment(JRadioButton.CENTER);
radioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// prevent deselection to mimic button group
if (!radioButton.isSelected())
cancelCellEditing();
stopCellEditing();
}
});
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int col) {
radioButton.setSelected(((Boolean) value).booleanValue());
return radioButton;
}
@Override
public Object getCellEditorValue() {
return Boolean.valueOf(radioButton.isSelected());
}
}
}
您是否尝试重新绘制()您的 JTable ?
该复选框是始终未选中还是在某些情况下未选中?
Hovercraft Full Of Eels 是对的:你能发布一堆你的代码吗?