JTable单元格中的JComboBox [英] JComboBox in a JTable cell

查看:113
本文介绍了JTable单元格中的JComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用模型创建的JTable,它基于一个对象矩阵。
对于每一行,我想使用JComboBox在特定列(第5行)中放入一些信息。
我尝试过以下方法:

I have a JTable created using a model, which is based on a matrix of objects. For each row, I want to put in a specific column (the 5th) some information using a JComboBox. I have tried the following:

for(int i=0; i < n ; i++) {  
    .....  
    data[i][5] = new JComboBox(aux); // aux is a Vector of elements I wanna insert  
}  
table.setModel(new MyTableModel()); // MyTableModel() already takes into consideration the data[][] object  

问题在于数据[i] [5] =新的JComboBox(aux);不会在JTable的特定单元格中创建JComboBox对象,而是将代码粘贴到该行中。
我该怎么做才能解决这个问题?

The problem is that data[i][5] = new JComboBox(aux); does not create a JComboBox object in that specific cell of the JTable, but pastes a code into the row. What can I do to solve this problem?

谢谢。

推荐答案

一种方法是覆盖getCellEditor()方法以返回适当的编辑器。这是一个让你入门的例子:

One way is to override the getCellEditor() method to return an appropriate editor. Here is an example to get you started:

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
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);
            }
        };
        System.out.println(table.getCellEditor());

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

编辑:更新代码以使用trashgod的建议。

code updated to use trashgod's suggestion.

这篇关于JTable单元格中的JComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆