java - 使用 Java Swing 根据另一个 JComboBox 中的选择更改单个单元格中 JComboBox 的内容

标签 java swing jtable jcombobox tablecelleditor

这是我第一次在这里发布问题,所以请告诉我我是否不遵守规则或是否缺少某些内容。

我的问题如下。我有一个带有 JTable 的窗口。在这个表中我有不同的数据。两列使用 JComboBoxes 从一组数据中选择值。 第二个 ComboBox 中的数据应基于第一个 ComboBox 的选择,但仅限于当前选定的行。

不幸的是,它没有按照预期的方式工作,并且它在第二个组合框中对该列中的所有组合框进行了更改。

为了简化问题,我从 Oracle Java 教程中获取了演示代码: [http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox][1] 并稍微调整了代码以完成我需要它做的事情。

我复制了我的整个小程序,以便您可以复制它并在您的计算机上运行它(如果您愿意/需要)。

package demoTraining;

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * TableRenderDemo.java requires no other files.
 */

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * TableRenderDemo is just like TableDemo, except that it explicitly initializes
 * column sizes and it uses a combo box as an editor for the Sport column.
 */
public class TableRenderDemo extends JPanel implements ItemListener {
    private boolean DEBUG = true;

    JTable table;

    public TableRenderDemo() {
        super(new GridLayout(1, 0));

        table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        // Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        // Set up column sizes.
        initColumnSizes(table);

        // Fiddle with the Sport column's cell editors/renderers.
        setUpSportColumn(table, table.getColumnModel().getColumn(2));
        setUpCategoryColumn(table, table.getColumnModel().getColumn(5));

        // Add the scroll pane to this panel.
        add(scrollPane);
    }

    /*
     * This method picks good column sizes. If all column heads are wider than
     * the column's cells' contents, then you can just use
     * column.sizeWidthToFit().
     */
    private void initColumnSizes(JTable table) {
        MyTableModel model = (MyTableModel) table.getModel();
        TableColumn column = null;
        Component comp = null;
        int headerWidth = 0;
        int cellWidth = 0;
        Object[] longValues = model.longValues;
        TableCellRenderer headerRenderer = table.getTableHeader()
                .getDefaultRenderer();

        for (int i = 0; i < 6; i++) {
            column = table.getColumnModel().getColumn(i);

            comp = headerRenderer.getTableCellRendererComponent(null,
                    column.getHeaderValue(), false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;

            comp = table.getDefaultRenderer(model.getColumnClass(i))
                    .getTableCellRendererComponent(table, longValues[i], false,
                            false, 0, i);
            cellWidth = comp.getPreferredSize().width;

            if (DEBUG) {
                System.out.println("Initializing width of column " + i + ". "
                        + "headerWidth = " + headerWidth + "; cellWidth = "
                        + cellWidth);
            }

            column.setPreferredWidth(Math.max(headerWidth, cellWidth));
        }
    }

    public void setUpSportColumn(JTable table, TableColumn sportColumn) {
        // Set up the editor for the sport cells.
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("Snowboarding");
        comboBox.addItem("Rowing");
        comboBox.addItem("Knitting");
        comboBox.addItem("Speed reading");
        comboBox.addItem("Pool");
        comboBox.addItem("None of the above");
        comboBox.addItemListener(this);
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

        // Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
    }

    private void setUpCategoryColumn(JTable table, TableColumn categoryColumn) {
        JComboBox categoryComboBox = new JComboBox();
        categoryComboBox.addItem("Category1");
        categoryComboBox.addItem("Category2");
        categoryComboBox.addItem("Category3");
        categoryColumn.setCellEditor(new DefaultCellEditor(categoryComboBox));

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        categoryColumn.setCellRenderer(renderer);

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
     * 
     * Catches the itemStateChange on the sport ComboBox and changes the values
     * of the Category ComboBox.
     */
    public void itemStateChanged(ItemEvent e) {
        // Only does something on itemEven "SELECTED"
        if (e.getStateChange() == ItemEvent.SELECTED) {

            // Only does something if a row is selected as it needs the row
            // number to make changes.
            if (table.getSelectedRow() != -1) {

                String currentSportSelection = e.getItem().toString();
                System.out.println("1. Current selection: "
                        + currentSportSelection);

                // If the selection in the sports combobox is
                // "None of the above" this change happens.
                if (currentSportSelection == "None of the above") {

                    // Some Sysos to help with debugging.
                    System.out.println("2. Selected Row: "
                            + table.getSelectedRow());
                    System.out.println("2.1. First Row Category: "
                            + table.getModel().getValueAt(0, 5));
                    System.out.println("2.2. Second Row Category: "
                            + table.getModel().getValueAt(1, 5));
                    System.out.println("3. Starting change of category column");

                    // Getting the tableCellEditor of the category cell in the
                    // selected row.
                    TableCellEditor categoryCellEditor = table.getCellEditor(
                            table.getSelectedRow(), 5);

                    // Getting the CellEditorComponent (ComboBox) of the
                    // category cell in the current row.
                    JComboBox currentCategoryComboBox = (JComboBox) categoryCellEditor
                            .getTableCellEditorComponent(table, null, true,
                                    table.getSelectedRow(), 5);

                    // Refill the category combobox.
                    currentCategoryComboBox.removeAllItems();
                    currentCategoryComboBox.addItem("SuperCategory1");
                    currentCategoryComboBox.addItem("SuperCategory2");
                    currentCategoryComboBox.addItem("SuperCategory3");
                    currentCategoryComboBox.addItem("SuperCategory4");

                    // Set the new value in the cell.
                    table.getModel().setValueAt(
                            currentCategoryComboBox.getItemAt(0),
                            table.getSelectedRow(), 5);

                    // Some more Sysos
                    System.out.println("4. Selected Row: "
                            + table.getSelectedRow());

                    System.out.println("5. Category of selected row: "
                            + table.getValueAt(table.getSelectedRow(), 5));

                    System.out.println("6. Categorylist top item: "
                            + currentCategoryComboBox.getItemAt(0));

                    System.out.println("7. Item deselected!");
                    table.getSelectionModel().clearSelection();

                    // Else clause does the same as if clause. 
                } else /*
                         * if (currentSportSelection == "Rowing" ||
                         * currentSportSelection == "Knitting" ||
                         * currentSportSelection == "Speed reading" ||
                         * currentSportSelection == "Pool" ||
                         * currentSportSelection == "None of the above")
                         */{
                    System.out.println("8. Selected Row: "
                            + table.getSelectedRow());
                    System.out.println("8.1. First Row Category: "
                            + table.getModel().getValueAt(0, 5));
                    System.out.println("8.2. Second Row Category: "
                            + table.getModel().getValueAt(1, 5));

                    TableCellEditor categoryCellEditor = table.getCellEditor(
                            table.getSelectedRow(), 5);

                    JComboBox categoryComboBox = (JComboBox) categoryCellEditor
                            .getTableCellEditorComponent(table, null, true,
                                    table.getSelectedRow(), 5);

                    categoryComboBox.removeAllItems();
                    categoryComboBox.addItem("Category1");
                    categoryComboBox.addItem("Category2");
                    categoryComboBox.addItem("Category3");

                    table.getModel().setValueAt(categoryComboBox.getItemAt(0),
                            table.getSelectedRow(), 5);

                    // Some Sysos
                    System.out.println("9. Selected Row: "
                            + table.getSelectedRow());

                    System.out.println("10. Category of selected row: "
                            + table.getValueAt(table.getSelectedRow(), 5));

                    System.out.println("11. Categorylist top item: "
                            + categoryComboBox.getItemAt(0));

                    System.out.println("12. Item deselected!");
                    table.getSelectionModel().clearSelection();
                }

            }

        }

    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "First Name", "Last Name", "Sport",
                "# of Years", "Vegetarian", "Category" };
        private Object[][] data = {
                { "Kathy", "Smith", "Snowboarding", new Integer(5),
                        new Boolean(false), "Category1" },
                { "John", "Doe", "Rowing", new Integer(3), new Boolean(true),
                        "Category2" }, };

        public final Object[] longValues = { "Jane", "Kathy",
                "None of the above", new Integer(20), Boolean.TRUE, "Category3" };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/ editor for
         * each cell. If we didn't implement this method, then the last column
         * would contain text ("true"/"false"), rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's editable.
         */
        public boolean isCellEditable(int row, int col) {
            // Note that the data/cell address is constant,
            // no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's data can
         * change.
         */
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                        + " to " + value + " (an instance of "
                        + value.getClass() + ")");
            }

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i = 0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j = 0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("TableRenderDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
        TableRenderDemo newContentPane = new TableRenderDemo();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

我在其他一些问题中读到我需要使用“getTableCellRendererComponent()”,但我不能 100% 确定我正确使用了它。

此外,如果您需要更多信息,请随时询问。

预先感谢您针对此问题提供的任何帮助。

利奥·K.

最佳答案

您需要更改表中每一行的编辑器。

以下示例展示了如何为每行显示不同的编辑器:

import java.awt.*;
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<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();
            }
        });
    }
}

您显然需要自定义逻辑,因为您的编辑器将基于另一列(而不是行)中的值,但代码应该可以帮助您开始。

关于java - 使用 Java Swing 根据另一个 JComboBox 中的选择更改单个单元格中 JComboBox 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27987864/

相关文章:

java - JFrame 在 Ubuntu 中的错误位置(Unity?)

java - 将 JPanel 设置为另一个类的新 JPanel

java - 如何让JTable像windows一样调整大小?

java - 客户端套接字上的 setSoTimeout 不会影响套接字

java - 使用Iterator模式对n叉树进行前序/后序迭代遍历

java - 如何调用多个OSGi模块

java - 从 panel2 中的按钮添加到 panel1 中的文本字段

java - 如何在一页中打印多个 Jtable

java - 将 MongoDB 集合中的数据检索到 Swing JTable 中

java - 再次在任何@PostConstruct 之前注入(inject)所有@Resource