Променете съдържанието на JComboBox в една клетка въз основа на селекцията в друг JComboBox с помощта на Java Swing

Това е първият ми път, когато публикувам въпрос тук, така че просто ми кажете дали не съм спазил правило или ако нещо липсва.

Моят проблем е следния. Имам прозорец, който има JTable. В тази таблица имам различни данни. Две колони използват JComboBoxes за избор на стойност от набор от данни. Данните във второто комбинирано поле трябва да се базират на избора на първото комбинирано поле, но само за текущо избрания ред.

За съжаление не работи както е предназначено и прави промяната във второто комбинирано поле за всички комбинирани полета в тази колона.

За да опростя проблема, взех демо-кода от уроците за 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% сигурен, че го използвах правилно.

Освен това, ако имате нужда от повече информация, не се колебайте да попитате.

Благодаря предварително за всяка помощ с този проблем.

Лео К.


person Leo K.    schedule 16.01.2015    source източник


Отговори (1)


Трябва да смените редактора за всеки ред в таблицата.

Ето пример, който показва как да се показва различен редактор за всеки ред:

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

Очевидно ще трябва да персонализирате логиката, тъй като вашият редактор ще се основава на стойност в друга колона, а не на реда, но кодът трябва да ви помогне да започнете.

person camickr    schedule 16.01.2015