JTable ComboBox

时间:2015-09-27 16:32:02

标签: java swing jtable jcombobox abstracttablemodel

出于某种原因,这被删除了。 这个问题是关闭的,我不会读它,因为我已经开始并实施了更多的方法。所以不需要在这里发表讽刺的评论,我不会读它们。

好的,所以这可能是一个非常简单的问题,但是这里有。我正在设置JTable进行编辑,我希望第1列有一个comboBox编辑器。我查找了如何操作,并按照指示我的代码,但它似乎并没有实际更新到显示器。我在这里写什么?提前谢谢。

//MainWindow class
public class MainWindow extends JFrame{
GridBagConstraints gbc;
JTable gridDisplay;
private AbstractTableModel tableModel;
JLabel statusBar;

MainWindow()
{
    super("LayoutEditor");
    setLayout(new BorderLayout());
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    gridDisplay = new JTable();
    tableModel = new MyTableModel();
    gridDisplay.setAutoCreateRowSorter(true);
    gridDisplay.setColumnSelectionAllowed(true);
    gridDisplay.setCellSelectionEnabled(true);
    gridDisplay.setModel(tableModel);


    TableColumn tc = gridDisplay.getColumnModel().getColumn(0);
    JComboBox<String> cb = new JComboBox<String>();
    cb.addItem("JLabel");
    cb.addItem("JButton");
    cb.addItem("JTextField");
    cb.addItem("JTextArea");
    cb.addItem("JCheckBox");
    cb.addItem("JList");
    cb.addItem("JComboBox");
    cb.addItem("JSpinnerList");
    cb.addItem("JSpinnerNumber");
    cb.setSelectedIndex(0);
    tc.setCellEditor(new DefaultCellEditor(cb));
    MyDataModel temp= new MyDataModel();
    MyTableModel table = (MyTableModel)gridDisplay.getModel();
    table.append(temp);




    JScrollPane gridScroll = new JScrollPane(gridDisplay);
    mainPanel.add(toolBox, BorderLayout.NORTH);
    mainPanel.add(gridScroll, BorderLayout.CENTER);

    add(mb, BorderLayout.NORTH);
    add(mainPanel, BorderLayout.CENTER);
    setSize(1280,720);
    setVisible(true);
    }
]

//myTableModel class
public class MyTableModel extends AbstractTableModel {
ArrayList<MyDataModel> data;
String[] names;

MyTableModel()
{

    names = new String[]{"Type","Variable name","Text","Row","Column","Rows","Columns","Fill","Anchor"};
    data = new ArrayList<MyDataModel>();



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

@Override
public int getColumnCount() {
    return names.length;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    switch (columnIndex)
    {
        case 0:
            data.get(rowIndex).setType(aValue.toString());
        case 1:
            data.get(rowIndex).setName(aValue.toString());
        case 2:
            data.get(rowIndex).setText(aValue.toString());
        case 3:
            data.get(rowIndex).setRow((int)aValue);
        case 4:
            data.get(rowIndex).setColumn((int)aValue);
        case 5:
            data.get(rowIndex).setRows((int)aValue);
        case 6:
            data.get(rowIndex).setColumns((int)aValue);
        case 7:
            data.get(rowIndex).setFill((int)aValue);
        case 8:
            data.get(rowIndex).setAnchor((int)aValue);
    }

    fireTableCellUpdated(rowIndex, columnIndex);
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    switch (columnIndex){
        case 0:
            return data.get(rowIndex).getType();
        case 1:
            return data.get(rowIndex).getName();
        case 2:
            return data.get(rowIndex).getText();
        case 3:
            return data.get(rowIndex).getRow();
        case 4:
            return data.get(rowIndex).getColumn();
        case 5:
            return data.get(rowIndex).getRows();
        case 6:
            return data.get(rowIndex).getColumns();
        case 7:
            return data.get(rowIndex).getFill();
        case 8:
            return data.get(rowIndex).getAnchor();
    }
return null;
}

public void append(MyDataModel item)
{
    data.add(item);
}

@Override
public String getColumnName(int column) {
    return names[column];
}
}


//MyDataModel Class
public class MyDataModel {
String type, name, text;
int row, column, rows, columns, fill, anchor;

MyDataModel()
{
    type = new String("");
    name = new String("");
    text = new String("");
    row = 0;
    column = 0;
    rows = 0;
    columns = 0;
    fill = 0;
    anchor = 0;
}

public MyDataModel(MyDataModel test) {
    type = test.getType();
    name = test.getName();
    text = test.getText();
    row = test.getRow();
    column = test.getColumn();
    rows = test.getRows();
    columns = test.getColumns();
    fill = test.getFill();
    anchor = test.getAnchor();
}

public int getAnchor() {
    return anchor;
}

public void setAnchor(int anchor) {
    this.anchor = anchor;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getText() {
    return text;
}

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

public int getRow() {
    return row;
}

public void setRow(int row) {
    this.row = row;
}

public int getColumn() {
    return column;
}

public void setColumn(int column) {
    this.column = column;
}

public int getRows() {
    return rows;
}

public void setRows(int rows) {
    this.rows = rows;
}

public int getColumns() {
    return columns;
}

public void setColumns(int columns) {
    this.columns = columns;
}

public int getFill() {
    return fill;
}

public void setFill(int fill) {
    this.fill = fill;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}
}

2 个答案:

答案 0 :(得分:3)

  

它似乎没有真正更新到显示器。

您从组合框中选择的值未保存在TableModel中。

您的TableModel需要实现setValueAt(...)方法。

有关简单的实现,请参阅Create a Table Model上的Swing教程中的部分。它还会向您显示要调用的fireXXX(...)方法,以便向表通知数据更改。

答案 1 :(得分:3)

您在模型中遗漏了一些内容,包括setValueAt(...)isCellEditable(...),而您忘记在附加方法中调用fireTableRowsInserted(...)

如,

    public void append(MyDataModel item) {
        data.add(item);

        // !! don't forget this!!
        int firstRow = getRowCount() - 1;
        int lastRow = firstRow;
        fireTableRowsInserted(firstRow, lastRow);
    }

    @Override
    public String getColumnName(int column) {
        return names[column];
    }

    @Override //!!
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 0; // allow editing of first column
    }

    @Override //!!
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            MyDataModel row = data.get(rowIndex);
            row.setType(aValue.toString());
            fireTableCellUpdated(rowIndex, columnIndex);
            return;
        }
        super.setValueAt(aValue, rowIndex, columnIndex);
    }

您需要重新阅读JTable tutorial,因为您正在跳过许多关键概念。

我忘了 - 你还需要覆盖public boolean isCellEditable(int row, int col)并让它返回true,至少对于JComboBox列,否则你永远不会看到组合框,因为它是一个编辑器。