简体   繁体   中英

JTable ComboBox

For some reason this was removed. This question is CLOSED and i won't be reading it as i have moved onwards and implemented more methods working. So no need putting snarky comments here, i won't read them.

Ok, so this might be quite the simple question, but here goes. I am setting up a JTable for editing and i want column 1 to have a comboBox editor. I looked up how to do it and followed the instructions which lead me to the code under, however it doesn't seem to actually update into the display. What am I misssing here? Thank you in advance.

//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;
}
}

it doesn't seem to actually update into the display.

The value you select from the combo box is not being saved in the TableModel.

Your TableModel needs to implement the setValueAt(...) method.

See the section from the Swing tutorial on Create a Table Model for a simple implementation. It also shows you what fireXXX(...) method to invoke so the table is notified of the change in data.

You're missing a bit from your model including setValueAt(...) , isCellEditable(...) , and you forgot to call fireTableRowsInserted(...) inside of your append method.

eg,

    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);
    }

You will want to re-read the JTable tutorial because you're skipping a lot of key concepts.

And I forgot -- you also need to override public boolean isCellEditable(int row, int col) and have it return true, at least for the JComboBox column, else you'll never see combo boxes since it is an editor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM