문제

I have created a table where in a cell of each row a combo box is displayed. I have used the following two classes as cell editor and cell renderer respectively. Somehow when the table is displayed, every combo box in a cell doesn't open when it's clicked. Can anyone give me a hint? Thanks in advance

public class CellEditor extends DefaultCellEditor{

private static final long serialVersionUID = 1L;

public CellEditor(String[] items) {
    super(new JComboBox(items));
}
}

public class ComboBoxRenderer extends JComboBox implements TableCellRenderer {
/****/
private static final long serialVersionUID = 1L;

public ComboBoxRenderer(String[] items) {
    super(items);
}

public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {

    if (isSelected) {
        this.setForeground(table.getSelectionForeground());
        super.setBackground(table.getSelectionBackground());
    } else {
        this.setForeground(table.getForeground());
        this.setBackground(table.getBackground());
    }        
    this.setSelectedItem(value);// Select the current value      
    return this;
}
}
도움이 되었습니까?

해결책

please reads JTable's tutorial, there are Editors and Renderers and Using a Combo Box as an Editor, some examples on this Forum (inc AutoCompleted JComboBox in the JTable) or here or here

but basically is your question about, (check if you set that)

public boolean isCellEditable(int row, int col) {
    if (col == someInt) {
        return true;
    } else if (col == TableColumnsStartsWithZero) {
        return true;
    } else {
        return false;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top