java 根据数据类型具有不同类型单元格的 Jtable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16970824/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:34:31  来源:igfitidea点击:

Jtable with different types of cells depending on data type

javaswingjtabletablecellrendererabstracttablemodel

提问by MxLDevs

How can I implement a JTable with different types of cell editors depending on the type of input a particular row is displaying?

如何根据特定行显示的输入类型使用不同类型的单元格编辑器实现 JTable?

For example

例如

  • some rows could be checkboxes (for boolean types)
  • some rows could be comboboxes (if I want to provide a fixed set of options to choose from)
  • some rows could be text fields (if I allow arbitrary data).
  • 有些行可能是复选框(对于布尔类型)
  • 有些行可能是组合框(如果我想提供一组固定的选项可供选择)
  • 有些行可能是文本字段(如果我允许任意数据)。

Currently I have implemented the AbstractTableModel, which takes a set of custom field objects from my object and adds rows to the table. I would like to further customize my table by setting specific types of cells. I can determine which cell type to use based on the type of field that row contains.

目前我已经实现了AbstractTableModel,它从我的对象中获取一组自定义字段对象并将行添加到表中。我想通过设置特定类型的单元格来进一步自定义我的表格。我可以根据行包含的字段类型确定要使用的单元格类型。

The table model is dynamically created at run-time.

表模型是在运行时动态创建的。

回答by mKorbel

  • some rows could be checkboxes (for boolean types)
  • some rows could be comboboxes (if I want to provide a fixed set of options to choose from)
  • some rows could be text fields (if I allow arbitrary data).
  • 有些行可能是复选框(对于布尔类型)
  • 有些行可能是组合框(如果我想提供一组固定的选项可供选择)
  • 有些行可能是文本字段(如果我允许任意数据)。

for example

例如

enter image description here

在此处输入图片说明

import java.awt.EventQueue;
import java.util.Date;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class EachRowRendererEditor {

    private JFrame frame = new JFrame("EachRowRendererEditor");
    private  String[] columnNames = {"Type", "Value"};
    private     Object[][] data = {
            {"String", "I'm a string"},
            {"Date", new Date()},
            {"Integer", new Integer(123)},
            {"Double", new Double(123.45)},
            {"Boolean", Boolean.TRUE}};
   private  JScrollPane scrollPane;
   private  JTable table;

    public EachRowRendererEditor() {
        table = new JTable(data, columnNames) {

            private static final long serialVersionUID = 1L;
            private Class editingClass;

            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);
                if (modelColumn == 1) {
                    Class rowClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultRenderer(rowClass);
                } else {
                    return super.getCellRenderer(row, column);
                }
            }

            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);
                if (modelColumn == 1) {
                    editingClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultEditor(editingClass);
                } else {
                    return super.getCellEditor(row, column);
                }
            }
            //  This method is also invoked by the editor when the value in the editor
            //  component is saved in the TableModel. The class was saved when the
            //  editor was invoked so the proper class can be created.

            @Override
            public Class getColumnClass(int column) {
                return editingClass != null ? editingClass : super.getColumnClass(column);
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        scrollPane = new JScrollPane(table);
        frame.add(scrollPane);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                EachRowRendererEditor eeee = new EachRowRendererEditor();
            }
        });
    }
}

回答by TT.

Create a custom class implementing javax.swing.table.TableCellRenderer, which displays the values using the control you want to display with depending on the data type. Use instances of this class as cell renderer (TableColumn.setCellRenderer)

创建一个实现 javax.swing.table.TableCellRenderer 的自定义类,它根据数据类型使用要显示的控件显示值。使用此类的实例作为单元格渲染器 (TableColumn.setCellRenderer)