java 在 JTable 中设置默认启用的单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12078648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting a default enabled radiobutton in a JTable
提问by Phox
I used thisguide in order to make JTable
that would handle radio buttons. Works fine except i need to enable a default enabled button.
我使用本指南来JTable
处理单选按钮。工作正常,除非我需要启用默认启用的按钮。
there can be n rows. I've tried to enable it through the default table model, the Object[][]
, the table, and i tried enabling the button before adding it to the Object[][]
. I couldn't figure out how(if it is possible) to do it with the buttongroup.
可以有 n 行。我尝试通过默认表模型启用它Object[][]
,该表,并且我尝试在将其添加到Object[][]
. 我不知道如何(如果可能的话)用按钮组来做到这一点。
to find the default enabled button i have to compare the button text to a string(this part works).
要找到默认启用的按钮,我必须将按钮文本与字符串进行比较(这部分有效)。
回答by tenorsax
Not sure that I am interpreting the question correctly. You can use JRadioButton
constructor to set selection, for example the snippet (based on OP code sample) will set selected button "B":
不确定我是否正确解释了这个问题。您可以使用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" });
You can also change selection like this:
您还可以像这样更改选择:
((JRadioButton) dm.getValueAt(0, 1)).setSelected(true);
You can also use ButtonGroup.setSelected()
method.
您也可以使用ButtonGroup.setSelected()
方法。
EDIT: eliminate components from model
编辑:从模型中消除组件
The model should contain data rather than components. Storing components in the model defeats the idea of renderers and editors. For more details see Editors and Renderersand Swing Models and Renderers. Check out the following example the mimics button group behavior in the model:
模型应该包含数据而不是组件。在模型中存储组件违背了渲染器和编辑器的想法。有关更多详细信息,请参阅编辑器和渲染器以及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());
}
}
}
回答by Benj
Did you try to repaint() your JTable ?
您是否尝试重新绘制()您的 JTable ?
Is the checkbox always unchecked or is there some case in which it is not ?
该复选框是否始终未选中,还是在某些情况下未选中?
Hovercraft Full Of Eels is right : could you post a bunch of your code ?
充满鳗鱼的气垫船是对的:你能发布一堆你的代码吗?