简体   繁体   English

JTable中具有多个具有不同值的JComboBox

[英]Having several JComboBoxes with different values in a JTable

I want to create a JTable where in a specific column named "Tools" a JComboBox should be accommodated. 我想创建一个JTable,其中应在名为“工具”的特定列中容纳JComboBox。 the values of the JComboBox should differ for each row. JComboBox的值对于每一行应该有所不同。

In the constructor I have the following piece of code: 在构造函数中,我具有以下代码:

public Table(List<Software> software_list) throws TransformerFactoryConfigurationError, TransformerException, Exception {

    //****** create an 2D array object containing the emulators list data to be sent to the table model ******
    software_list_to_array = new Object[software_list.size()][5];


    for(int x = 0; x < software_list_to_array.length; x++){

        software_list_to_array[x][0] = software_list.get(x).getNumber();

        software_list_to_array[x][1] = software_list.get(x).getAuthor();

        software_list_to_array[x][2] = software_list.get(x).getReleaseDate();

        software_list_to_array[x][3] = software_list.get(x).getName();


        app_str = new String[software_list.get(x).getApp().size()];
        System.out.println(app_str.length);

        for(int k = 0; k < app_str.length; k++){
            app_str[k] = software_list.get(x).getApp().get(k).getName()
            + " " + software_list.get(x).getApp().get(k).getVersion();

        }
        software_list_to_array[x][4] = app_str;

    }

    tableModel = new SftTableModel(software_list_to_array, software_list_to_array.length);

    this.setModel(tableModel);

    this.setSize(new Dimension(1000,500));

    this.setRenderingProperties(app_str);

}

I have created the following method: 我创建了以下方法:

public void setRenderingProperties(String[] str){

    this.setRowHeight(25);

    this.getTableHeader().setFont(new Font("Arial", Font.BOLD,12));
    this.getTableHeader().setAlignmentY(Component.RIGHT_ALIGNMENT);
    this.setAutoCreateRowSorter(true);
    this.setShowGrid(false);
    this.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    ...........

if(app_str.length > 1){

    this.getColumn("Tools").setCellRenderer(new ComboBoxRenderer());
    JComboBox comboBox = new JComboBox();
    for(int k=0;k<app_str.length;k++){
        comboBox.addItem(app_str[k]);
    }

    this.getColumn("Tools").setCellEditor(new DefaultCellEditor(comboBox));
}else{
    this.getColumn("Tools").setCellRenderer(new CellsRenderer());
}
    this.getColumn("Tools").setResizable(true);
    this.getColumn("Tools").setPreferredWidth(250);

}

The problem with this code is that in the Tools column all the comboboxes contain all the same value. 此代码的问题在于,在“工具”列中,所有组合框都包含所有相同的值。 Does anyone have suggestion for this issue? 有人对这个问题有建议吗?

You can override the getCellEditor(...) of the JTable to return a specific editor. 您可以重写JTable的getCellEditor(...)以返回特定的编辑器。 Something like: 就像是:

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 );
//      table.getColumnModel().getColumn(1).setCellRenderer(new ComboBoxRenderer() );
    }


/*
    class ComboBoxRenderer extends JComboBox implements TableCellRenderer
    {

        public ComboBoxRenderer()
        {
            setBorder(new EmptyBorder(0, 0, 0, 0));
        }

        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column)
        {
//          setFocusable(false);
            removeAllItems();
            addItem( value );
            return this;
        }
    }

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

由于此代码太旧了,但我不记得我在这里看到了另一个更好,更简单的用法示例

I think that the main problem is that your app_str String is only instantiated once. 我认为主要问题是您的app_str字符串仅被实例化一次。 Then at the end, everything is set to the last value of this instance. 然后,最后,一切都设置为该实例的最后一个值。

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

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