使用TableModelListener设置JComboBox编辑器 [英] Setting a JComboBox editor using TableModelListener

查看:100
本文介绍了使用TableModelListener设置JComboBox编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用TableModelListener将第二列设置为JComboBox编辑器.还希望根据第一列中选​​择的ComboBox更改第二列中ComboBox的 Model .在这里,我实现了一个侦听第一列的侦听器.

I wanted to set my 2nd column as JComboBox editor using TableModelListener. Also wanted to change the Model of ComboBox in 2nd column based on selected ComboBox in 1st column. Here I implemented a Listener that's listen to 1st column.

private class TableScheduleListener implements TableModelListener
{
    //Listening for data changes.
    @Override
    public void tableChanged(TableModelEvent e) 
    {
        int row = e.getFirstRow();
        int column = e.getColumn();
    }

    if(column == 1)
    {
            for(int i = 0; i < createScheduleView.tblSchedule.getRowCount(); i++)
            {
                createScheduleView.tblSchedule.getCellEditor(i, 2);
            }

            int col = createScheduleView.tblSchedule.convertColumnIndexToModel(2);

            if(col == 2 && createScheduleView.tblSchedule.getRowCount() < 7)
            {
                DefaultComboBoxModel cbModel = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
                createScheduleView.cbBreakStartTime.setModel(cbModel);
            }
     }
}

但是我在考虑如何在第二列中将JComboBox设置为编辑器.设置后是模型吗?任何帮助将不胜感激.

But I'm thinking how can I set as JComboBox as editor in my 2nd column. After settings it's model? Any help would appreciated.

更新

我遵循了来自 https://tips4java的camickr技巧.wordpress.com/2009/06/28/combo-box-table-editor/ 但是想覆盖其他类中的TableCellEditor.

I followed camickr tips from https://tips4java.wordpress.com/2009/06/28/combo-box-table-editor/ But wanted to override TableCellEditor from different class.

private class TableScheduleEditor extends JTable
{
    //  Determine editor to be used by row
    @Override
    public TableCellEditor getCellEditor(int row, int column)
    {
        int modelColumn = convertColumnIndexToModel(column);

        if(modelColumn == 2 && row < 7)
        {
            DefaultComboBoxModel model = new DefaultComboBoxModel(createScheduleModel.setData().toArray());
            createScheduleView.getCbBreakStartTime().setModel(model);
            return new DefaultCellEditor(createScheduleView.getCbBreakStartTime());
        }

        else
            return super.getCellEditor(row, column);
    };
}

我正在考虑该类如何在我的JTable中注册? 我试图在我的构造器this.createScheduleView.tblSchedule.setCellEditor(new TableScheduleEditor());中注册它 但是它说不能扩展到TableCellEditor,因为它扩展了JTable.有什么技巧吗?

I'm thinking how this class can register in my JTable? I tried to register this in my constructor this.createScheduleView.tblSchedule.setCellEditor(new TableScheduleEditor()); But it says cannot be converted to TableCellEditor because it extends JTable. Any trick to do this?

推荐答案

还希望根据第一列中选​​择的组合框来更改第二列中的组合框的模型

Also wanted to change the Model of ComboBox in 2nd column based on selected ComboBox in 1st column

重写JTablegetCellEditor(...)方法以返回适当的编辑器.

Override the getCellEditor(...) method of the JTable to return the appropriate editor.

这是一个入门的基本示例:

Here is a basic example to get you started:

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

public class TableComboBoxByRow extends JPanel
{
    List<String[]> editorData = new ArrayList<String[]>(3);

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

        // Create the editorData to be used for each row

        editorData.add( new String[]{ "Red", "Blue", "Green" } );
        editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
        editorData.add( new String[]{ "Apple", "Orange", "Banana" } );

        //  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)
                {
                    JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
                    return new DefaultCellEditor( comboBox1 );
                }
                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();
            }
        });
    }
}

在上面的示例中,模型"基于正在编辑的行而更改.

In the above example the "model" changes based on the row being edited.

根据您的情况,您将需要修改逻辑以根据第一列中的值返回编辑器.

In your case you will need to modify the logic to return the editor based on the value in the first column.

这篇关于使用TableModelListener设置JComboBox编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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