简体   繁体   English

自定义表模型中的JComboBox

[英]JComboBox in Custom table model

I have got question about adding JComboBox , to Column in Custom table model which extends ObjectTableModel (Table is OmniJTable ). 我对在自定义表模型中的Column中添加JComboBox提出了疑问,该模型扩展了ObjectTableModel(表为OmniJTable )。 I work on it 2 days and cannot solve this problem. 我工作了2天,无法解决此问题。

One thing I solved is displaying JComboBox in Column, but right now I have got problem with selecting anything from it (seems it's not editable, and anything like "setEditable()" not working). 我解决的一件事是在Column中显示JComboBox ,但是现在我从中选择任何内容都遇到了问题(似乎它不可编辑,并且诸如“ setEditable()”之类的东西都无法正常工作)。

Here is code which one I add jComboBox to my OmniJTable with ObjectTableModel. 这是我使用ObjectTableModel将jComboBox添加到OmniJTable代码。

class CheckBoxCellRenderer extends JComboBox implements TableCellRenderer {
    JComboBox combo;
    public CheckBoxCellRenderer(JComboBox comboBox) {
        this.combo = new JComboBox();
        for (int i=0; i<comboBox.getItemCount(); i++){
            combo.addItem(comboBox.getItemAt(i));
        }
    }
    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        combo.setSelectedItem(value);
        return combo;
    }
} 
private void addComboBoxToStatusColumn(JTable table)
{
    final int statusColumnIndex = bazaTelefonowOmniJTable.getColumnModel().getColumnIndex("Status");

    TableColumn tmpColum = bazaTelefonowOmniJTable.getColumnModel().getColumn(statusColumnIndex);
    final JComboBox comboBox = new JComboBox();
    comboBox.setEditable(true);
    comboBox.setEnabled(true);
    loadRecordStatusFromDictionary(comboBox);


    DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
    tmpColum.setCellEditor(defaultCellEditor);
    tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
    bazaTelefonowOmniJTable.setEditable(true);
    //table.repaint();
} 

As i said, this one adding jComboBox to Column, but i don't know how to make this one to allow me to choose items in jComboBox. 就像我说的那样,这个将jComboBox添加到Column中,但是我不知道如何制作此模型以允许我在jComboBox中选择项目。

PS: Sry for my english, it's not my primary language. PS:对不起,我的英语不是我的主要语言。

The simplest thing is not to add a CellRenderer . 最简单的事情是不添加CellRenderer In that case, the Table renders it as a Label and when clicked the combo box is shown. 在这种情况下,表格将其呈现为标签,并在单击时显示组合框。 Here is an example: 这是一个例子:

package snippet;

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class JTableTest extends JFrame {

    public JTableTest() {
        super(JTableTest.class.getName());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
    }

    private void initComponents() {
        JTable table = new JTable(new Object[][] { { "1", "One" }, { "2", "Two" } }, new Object[] { "Column One", "Status" });
        addComboBoxToStatusColumn(table);
        add(new JScrollPane(table));
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {
                new JTableTest().setVisible(true);
            }
        });
    }

    private void addComboBoxToStatusColumn(JTable table) {
        final int statusColumnIndex = table.getColumnModel().getColumnIndex("Status");

        TableColumn tmpColum = table.getColumnModel().getColumn(statusColumnIndex);
        final JComboBox comboBox = new JComboBox();
        loadRecordStatusFromDictionary(comboBox);

        DefaultCellEditor defaultCellEditor = new DefaultCellEditor(comboBox);
        tmpColum.setCellEditor(defaultCellEditor);
    }

    private void loadRecordStatusFromDictionary(JComboBox comboBox) {
        comboBox.addItem("Two");
        comboBox.addItem("Four");
        comboBox.addItem("Six");
    }
}

You also need to override the isCellEditable method from your model. 您还需要从模型中覆盖isCellEditable方法。

 model = DaneTableModel(some arg) {
     public boolean isCellEditable(int row, int col) {
        if(col == STATUS_COLUMN) return true ;
        return false;
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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