Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JTable中设置默认启用的单选按钮_Java_Swing_Jtable_Jradiobutton_Buttongroup - Fatal编程技术网

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

Java 在JTable中设置默认启用的单选按钮,java,swing,jtable,jradiobutton,buttongroup,Java,Swing,Jtable,Jradiobutton,Buttongroup,我使用guide使JTable能够处理单选按钮。工作正常,但我需要启用默认启用的按钮 可以有n行。我尝试通过默认的表模型、Object[][]、表来启用它,并尝试在将按钮添加到Object[][]之前启用该按钮。我不知道如何(如果可能的话)使用buttongroup来完成它 要找到默认启用的按钮,我必须将按钮文本与字符串进行比较(这部分有效)。是否尝试重新绘制()JTable 复选框是否总是未选中,或者是否存在未选中的情况 满是鳗鱼的气垫船是对的:你能发一堆代码吗?你试过重新绘制你的JTable

我使用guide使
JTable
能够处理单选按钮。工作正常,但我需要启用默认启用的按钮

可以有n行。我尝试通过默认的表模型、
Object[][]
、表来启用它,并尝试在将按钮添加到
Object[][]
之前启用该按钮。我不知道如何(如果可能的话)使用buttongroup来完成它

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

是否尝试重新绘制()JTable

复选框是否总是未选中,或者是否存在未选中的情况

满是鳗鱼的气垫船是对的:你能发一堆代码吗?

你试过重新绘制你的JTable吗

复选框是否总是未选中,或者是否存在未选中的情况


满是鳗鱼的气垫船是对的:你能发一堆代码吗?

我不确定我对这个问题的解释是否正确。您可以使用
JRadioButton
构造函数设置选择,例如,代码段(基于OP代码示例)将设置所选按钮“B”:

您还可以按如下方式更改选择:

((JRadioButton) dm.getValueAt(0, 1)).setSelected(true);
您还可以使用
按钮组.setSelected()
方法

编辑:从模型中删除组件

模型应该包含数据而不是组件。将组件存储在模型中违背了渲染器和编辑器的思想。有关更多详细信息,请参阅和。查看模型中模拟按钮组行为的以下示例:

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());
        }
    }   
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.border.*;
导入javax.swing.plaf.*;
导入javax.swing.table.*;
公共类按钮组模拟测试{
私有静态void createAndShowGUI(){
DefaultTableModel=新的DefaultTableModel(新对象[][]{
{“组1”,Boolean.FALSE},{“组2”,Boolean.FALSE},
{“组3”,Boolean.FALSE},
新对象[]{“名称”,“状态”}){
私有静态最终长serialVersionUID=1L;
@凌驾
公共类getColumnClass(int-col){
如果(列==1)
返回Boolean.class;
返回super.getColumnClass(col);
}
@凌驾
public void setValueAt(对象值、整行、整列){
super.setValueAt(值、行、列);
if(col==1&&value.equals(Boolean.TRUE))
取消选择值(行、列);
}
私有值(int selectedRow、int col){
对于(int row=0;rowimport 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());
        }
    }   
}