【问题标题】:JComboBox popup dynamic populationJComboBox 弹出动态填充
【发布时间】:2014-02-17 19:16:51
【问题描述】:

烧了谷歌搜索节点两天两夜,我必须谦虚地问:

场景:

  • 我有一个JTable 和一个JComboBox 作为单元格编辑器。
  • 我希望在单击单元格时填充JComboBox 弹出列表(来自ArrayList)。

我在 2 天的时间里尝试了很多事情(在不同程度上起作用),但我仍然很难过。我试过MouseListener 和听众。我也试过SwingUtilities.invokeLater(...)

我只需要扩展我的JComboBox,比如DynPopComboBox,这样当单击该框时,它会调用一个方法来填充它的弹出列表,然后再显示它。理想情况下,我想从代码中其他地方的ArrayList 或另一个对象中的不同JTable 获取弹出内容。

有人可以指点我的代码示例吗?我无计可施,没时间解决。

谢谢。

【问题讨论】:

  • 你能展示你做了什么,为什么它不起作用?就目前而言,我们没有太多工作要做。
  • 谢谢一大堆peeskillet!看起来这将完成这项工作。我添加了一行“cbox.showPopup();”这样当填充完成时,您不必再次单击即可获得弹出窗口。

标签: java swing dynamic jcombobox population


【解决方案1】:

覆盖JTablegetCellEditor() 方法以返回具有适当编辑器的编辑器。比如:

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 JPanel
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public TableComboBoxByRow()
    {
        setLayout( new BorderLayout() );

        // Create the editors to be used for each row

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

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

        String[] items3 = { "Apple", "Orange", "Banana" };
        JComboBox<String> comboBox3 = new JComboBox<String>( 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 );
        add( scrollPane );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Table Combo Box by Row");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableComboBoxByRow() );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2014-10-03
    • 2014-01-24
    • 1970-01-01
    相关资源
    最近更新 更多