在Jtable单元格中的复选框旁边插入文本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Jtable单元格中的复选框旁边插入文本相关的知识,希望对你有一定的参考价值。

我已经习惯了这一段时间了,我能找到的所有解决方案和工作示例包括使用内置布尔TableCellRenderers呈现的复选框,即一个单元格只能有一个复选框。我没有找到任何工作示例,其中在该SAME单元​​格中的复选框旁边会有一些文本,或者更具体地说是允许复选框和文本的JCheckBox。

答案

当您的数据/要求不符合默认的可用功能时,您需要考虑提供定制的解决方案

我建议你先看看Using custom renderers

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public class Data {
        private String text;
        private boolean selected;

        public Data(String text, boolean selected) {
            this.text = text;
            this.selected = selected;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean state) {
            this.selected = state;
        }

    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            DefaultTableModel model = new DefaultTableModel(new String[] {"Something"}, 0) {
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return Data.class;
                }
            };
            model.addRow(new Object[]{new Data("Hello", true)});
            model.addRow(new Object[]{new Data("Goodbye", false)});
            model.addRow(new Object[]{new Data("Do you want to play a game?", true)});
            model.addRow(new Object[]{new Data("Yesturday", false)});
            model.addRow(new Object[]{new Data("Tomorrow", true)});
            model.addRow(new Object[]{new Data("Java", false)});
            model.addRow(new Object[]{new Data("Swift", false)});
            model.addRow(new Object[]{new Data("Helicopter", true)});


            JTable table = new JTable(model);
            table.setDefaultRenderer(Data.class, new DataTableCellRenderer());

            add(new JScrollPane(table));
        }

    }

    public class DataTableCellRenderer extends JCheckBox implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (isSelected) {
                setBackground(table.getSelectionBackground());
                setForeground(table.getSelectionForeground());
            } else {
                setBackground(table.getBackground());
                setForeground(table.getForeground());
            }

            if (value instanceof Data) {
                Data data = (Data)value;
                setText(data.getText());
                setSelected(data.isSelected());
            }

            return this;
        }

    }

}

在这一点上,我想知道JTable是否是最佳解决方案,如果JList可能更好

当然,另一种选择,可能与JTable更好地工作,将设计一个自定义TableModel,可以将数据分解成单独的列...

Custom table model

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public class Data {

        private String text;
        private boolean selected;

        public Data(String text, boolean selected) {
            this.text = text;
            this.selected = selected;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean state) {
            this.selected = state;
        }

    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            DataTableModel model = new DataTableModel();
            model.add(new Data("Hello", true));
            model.add(new Data("Goodbye", false));
            model.add(new Data("Do you want to play a game?", true));
            model.add(new Data("Yesturday", false));
            model.add(new Data("Tomorrow", true));
            model.add(new Data("Java", false));
            model.add(new Data("Swift", false));
            model.add(new Data("Helicopter", true));

            JTable table = new JTable(model);

            add(new JScrollPane(table));
        }

    }

    public class DataTableModel extends AbstractTableModel {

        private List<Data> rows = new ArrayList<>(25);

        public void add(Data data) {
            int rowCount = getRowCount();
            rows.add(data);
            fireTableRowsInserted(rowCount, rowCount);
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case 0: return "";
                case 1: return "Option";
            }
            return null;
        }

        @Override
        public int getRowCount() {
            return rows.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            switch (columnIndex) {
                case 0: return Boolean.class;
                case 1: return String.class;
            }
            return Object.class;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Data row = rows.get(rowIndex);
            switch (columnIndex) {
                case 0: return r

以上是关于在Jtable单元格中的复选框旁边插入文本的主要内容,如果未能解决你的问题,请参考以下文章

如何在编辑时选择JTable单元格中的所有文本

您如何使复选框单元格中的文本能够被编辑?

如何将工具提示添加到 jtable 中的单元格?

JTable 单元格中的 JDateChooser 输入键并不总是有效

自定义 UITableViewCell 中的 UIButton 导致在多个单元格中选择按钮

在jTable(自动保存)中编辑后如何保存单元格中的数据?