如何在JTable中设置CellEditor?

时间:2018-07-16 10:55:42

标签: java swing jtable tablecelleditor

我想为 <div formArrayName="items" *ngFor="let item of importExp.controls['items'].controls; let i = index;"> <div [formGroupName]="i"> <div *ngIf="this.selectedApproach == this.database" class="form-group"> <label class="form-label" for="dbServerID">Exposure Server</label> <div> <select formControlName="dbServer" class="form-control custom-select" id="dbServerId" (ngModelChange)="onSelectServer($event)"> <option *ngFor="let ds of databaseServers" [ngValue]="ds"> {{ds.databaseServerName}} </option> </select> <span class="fa fa-search-plus" aria-hidden="true"></span> <!-- <app-control-message ErrorMsg="Database Name" [control]="dbServer"></app-control-message> --> </div> 中的一个单元格设置<div *ngFor="let item of importExp.controls.items.controls; let i=index"> CellEditor。它是一个单元格,而不是全部列,我在JDK中找不到名为JComboBox的类。

此代码无法满足我的要求。

JTable

1 个答案:

答案 0 :(得分:2)

很简单。您只需要覆盖getCellRenderer中的方法JTable。这是一个例子。

import java.awt.Component;
import java.awt.font.TextAttribute;
import java.util.Collections;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

@SuppressWarnings("unchecked")
public class TableRendererTest {

    public static void main(String[] args) {
        JFrame frm = new JFrame("Renderer test");
        DefaultTableModel model = new DefaultTableModel(new String[] {"First", "Second", "Third" }, 3);
        model.setValueAt("First row cell", 0, 1);
        model.setValueAt("Striked out string", 1, 1);
        model.setValueAt("Last row cell", 2, 1);

        TableCellRenderer strikeOutRenderer = new StrikeOutRenderer();
        // table with strike-out renderer (last cell is not stroked out)
        // create a custom table with possibility to change renderer for a single cell.
        JTable tbl = new JTable(model) {
            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                if (row == 1 && column == 1) {
                    return strikeOutRenderer;
                }
                return super.getCellRenderer(row, column);
            }
        };
        frm.add(new JScrollPane(tbl));

        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    private static class StrikeOutRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component res = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            res.setFont(res.getFont().deriveFont(Collections.singletonMap(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON)));
            return res;
        }
    }
}

这是完整解决方案的代码,因此您可以在任何地方使用该表,并可以为任何单元格定义渲染器。

// defined in JCellStyleTable.java
public class JCellStyleTable extends JTable {


    private final Map<Cell, TableCellRenderer> renderersMap = new HashMap<>();

    private final Map<Cell, TableCellEditor> editorsMap = new HashMap<>();

    public JCellStyleTable() {
        super();
    }

    public JCellStyleTable(TableModel dm) {
        super(dm);
    }

    public void putRenderer(Cell cell, TableCellRenderer renderer) {
        renderersMap.put(Objects.requireNonNull(cell), renderer);
    }

    public TableCellRenderer getRenderer(Cell cell) {
        return renderersMap.get(Objects.requireNonNull(cell));
    }

    public void putEditor(Cell cell, TableCellEditor editor) {
        editorsMap.put(Objects.requireNonNull(cell), editor);
    }

    public TableCellEditor getEditor(Cell cell) {
        return editorsMap.get(Objects.requireNonNull(cell));
    }

    @Override
    public TableCellRenderer getCellRenderer(int row, int column) {
        TableCellRenderer result = renderersMap.get(new Cell(row, column));
        return result == null ? super.getCellRenderer(row, column) : result;
    }

    @Override
    public TableCellEditor getCellEditor(int row, int column) {
        return editorsMap.get(new Cell(row, column)); // when null - no editing
    }
}

// defined in CellBasedTableModel.java
public class CellBasedTableModel extends DefaultTableModel {
    private Collection<Cell> editableCells = new HashSet<>();

    public CellBasedTableModel() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return editableCells.contains(new Cell(row, column));
    }

    public void addEditableCell(Cell cell) {
        editableCells.add(cell);
    }

    public void removeEditableCell(Cell cell) {
        editableCells.remove(cell);
    }
}

// defined in Cell.java
public class Cell {
    private final int row, col;

    public Cell(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Cell) {
            Cell another = (Cell) obj;
            return another.col == col && another.row == row;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(new int[] {row, col});
    }
}