6

我有一个包含 JComboBox 的列的 JTable。

我有一个附加到 JComboBox 的 ItemListener,它对任何更改都起作用。

但是,ItemListener 没有获取更改后的 ComboBox 所在行的方法。

当 ComboBox 发生更改时,我需要行号才能对同一行中的另一列进行操作。

任何帮助,将不胜感激。


这是我的简明代码。我想要完成的是在 itemlistener 获取更改时获取 ComboBox 的表格行。

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Example extends JFrame {

    private static final long serialVersionUID = 1L;
    public static int maxX, maxY;
    public static final String[] columnHeads = {"Col 1", "Col 2", "Col 3"};

    public static void main(String args[]) throws IOException {
        Example example = new Example();
    }

    public Example() {
        //Create Table Model
        DefaultTableModel model = new DefaultTableModel();

        for (int index = 0; index < columnHeads.length; index++) {
            model.addColumn(columnHeads[index]);
        }

        //Create Table
        JTable table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);

        //List for ComboBox
        String[] list = {"Item1", "Item2", "Item3"};

        //Create ComboBox
        JComboBox itemTypes = attachComboBoxRenderer(table, 2, list);

        //Attach Item Listener
        itemTypes.addItemListener(new ComboBoxListener());
        ((DefaultTableModel) table.getModel()).insertRow(
            table.getRowCount(), new Object[]{"C1", "C2", ""});
        this.setTitle("Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container container = getContentPane();

        //MAIN Panel
        final JPanel main = new JPanel();
        main.setLayout(new GridBagLayout());
        main.add(scrollPane);

        container.add(main);
        this.pack();
        this.setVisible(true);
    }

    public static JComboBox attachComboBoxRenderer(
        JTable table, int column, Object[] values) {
        JComboBox combo = new JComboBox(values);
        TableColumn col = table.getColumnModel().getColumn(column);
        col.setCellRenderer(new ComboBoxRenderer(values));
        col.setCellEditor(new DefaultCellEditor(combo));
        return combo;
    }
}

class ComboBoxListener implements ItemListener {

    private static final int SELECTED = 1;

    @Override
    public void itemStateChanged(ItemEvent e) {
        // Get the affected item
        Object item = e.getItem();
        if (item.toString() != null
            && !"".equals(item.toString())
            && e.getStateChange() == SELECTED) {
            System.out.println(item.toString() + " selected");
            //How do I get Row in the Table of the ComboBox that was changed?
        }
    }
}

class ComboBoxRenderer extends JComboBox implements TableCellRenderer {

    private static final long serialVersionUID = 1L;

    public ComboBoxRenderer(Object[] items) {
        super(items);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        // Select the current value
        setSelectedItem(value);
        return this;
    }
}
4

1 回答 1

10

It sounds like you are Using a Combo Box as an Editor. If so, the TableCellEditor method, getTableCellEditorComponent(), includes the row as a parameter. There's a related example here.

Addendum: To change a value in the same row you've edited, just have the model return the correct value for the "other column" based on the related values in that row. Alternatively, update the related value in your model's setValueAt() method before firing the update, as shown in the example.

Addendum: Based on your example, the code below overrides the model's getValueAt() method to keep the dependent column synchronized with the item column.

enter image description here

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/** @see http://stackoverflow.com/questions/7350445 */
public class DependentColumn extends JFrame {

    private static final int DEPENDENT_COL = 1;
    private static final int ITEM_COL = 2;
    private static final String[] columnNames = {"Col 1", "Col 2", "Col 3"};

    public static void main(String args[]) throws IOException {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DependentColumn dc = new DependentColumn();
            }
        });
    }

    public DependentColumn() {
        this.setTitle("Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create Model & Table
        DefaultTableModel model = new DefaultTableModel(columnNames, 0) {

            @Override
            public Object getValueAt(int row, int col) {
                if (col == DEPENDENT_COL) {
                    return "C2:" + this.getValueAt(row, ITEM_COL);
                } else {
                    return super.getValueAt(row, col);
                }
            }
        };
        for (int i = 0; i < 16; i++) {
            model.addRow(new Object[]{"C1", "C2", "Item1"});
        }
        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(new Dimension(320, 120));

        //Create ComboBox
        String[] items = {"Item1", "Item2", "Item3"};
        JComboBox combo = new JComboBox(items);
        TableColumn col = table.getColumnModel().getColumn(ITEM_COL);
        col.setCellEditor(new DefaultCellEditor(combo));
        combo.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    System.out.println(e.getItem() + " selected");
                }
            }
        });

        this.add(new JScrollPane(table));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}
于 2011-09-09T02:22:13.203 回答