简体   繁体   English

如何将唯一的JComboBox添加到JTable(Java)中的列

[英]How to add unique JComboBoxes to a column in a JTable (Java)

I am trying to add unique JComboBoxes to a column in a JTable . 我试图将唯一的JComboBoxesJTable的列。 I know it is possible to add a JComboBox to an entire column using 我知道可以使用JComboBox到整个列

TableColumn col = table.getColumnModel().getColumn(columnNumber);
col.setCellEditor(new MyComboBoxEditor(values));

but I need each JComboBox to be different and have different Strings inside it. 但是我需要每个JComboBox都不同,并且里面有不同的字符串。 Any ideas? 有任何想法吗?

Override the getCellEditor(...) method. 覆盖getCellEditor(...)方法。 For example; 例如;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableComboBoxByRow extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public TableComboBoxByRow()
    {
        // Create the editors to be used for each row

        String[] items1 = { "Red", "Blue", "Green" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

        String[] items2 = { "Circle", "Square", "Triangle" };
        JComboBox comboBox2 = new JComboBox( items2 );
        DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
        editors.add( dce2 );

        String[] items3 = { "Apple", "Orange", "Banana" };
        JComboBox comboBox3 = new JComboBox( items3 );
        DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
        editors.add( dce3 );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                    return editors.get(row);
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableComboBoxByRow frame = new TableComboBoxByRow();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
}

Try this: 尝试这个:

...
final Object[] obj = {"test1", "test2", "test3"};
JTable jTable = new JTable();
TableColumn column = jTable.getColumnModel().getColumn(1);
column.setCellEditor(new AutoCompletionComboBoxEditor(obj));
...

public static class AutoCompletionComboBoxEditor extends AbstractCellEditor implements TableCellEditor {

        JComboBox cbx;

        public AutoCompletionComboBoxEditor(Object[] items) {
            cbx = new JComboBox(items);
        }

        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {
            return cbx;
        }

        public Object getCellEditorValue() {
            return cbx.getSelectedItem();
        }
    }

try tell me the effect :) 试试告诉我效果:)

-Saligh -Saligh

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

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