开发者

JComboBox not opening in a jTable

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜