开发者

How do I get the CellRow when there is an ItemEvent in the JComboBox within the cell

I have a JTable with a column containing a JComboBox.

I have an ItemListener attached to the JComboBox which acts upon any changes.

However, ItemListener does not have a method for obtaining the Row that the changed ComboBox is wit开发者_Python百科hin.

I need to Row number in order to act upon another column in the same row when the ComboBox has a change.

Any help would be appreciated.


This is my Short and Concise code. What I am trying to accomplish, is obtaining the Table Row of the ComboBox when a the itemlistener picks up a change.

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


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.

How do I get the CellRow when there is an ItemEvent in the JComboBox within the cell

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜