在JTable中设置默认启用的单选按钮

时间:2012-08-22 17:46:43

标签: java swing jtable jradiobutton buttongroup

我使用this指南来制作处理单选按钮的JTable。工作正常,但我需要启用默认启用按钮。

可以有n行。我尝试通过默认表模型Object[][],表格启用它,我尝试启用按钮,然后将其添加到Object[][]。我无法弄清楚如何(如果可能的话)用buttongroup来做。

找到默认启用按钮我必须将按钮文本与字符串进行比较(此部分有效)。

2 个答案:

答案 0 :(得分:5)

我不确定我是否正确地解释了这个问题。您可以使用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()方法。

编辑:从模型中删除组件

模型应包含数据而不是组件。在模型中存储组件会破坏渲染器和编辑器的想法。有关详细信息,请参阅Editors and RenderersSwing Models and Renderers。请查看以下示例模型中的模拟按钮组行为:

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());
        }
    }   
}

答案 1 :(得分:0)

您是否尝试重绘()您的JTable?

复选框是否始终未选中,或者是否存在某些情况?

Hovercraft Full Of Eels是对的:您可以发布一堆代码吗?