Home > Java > javaTutorial > How to Retrieve the Row Index of a JComboBox Within a JTable Cell During an ItemEvent?

How to Retrieve the Row Index of a JComboBox Within a JTable Cell During an ItemEvent?

Mary-Kate Olsen
Release: 2024-12-09 12:58:11
Original
896 people have browsed it

How to Retrieve the Row Index of a JComboBox Within a JTable Cell During an ItemEvent?

How to Get the Cell Row of a JComboBox in a JTable Cell During ItemEvent

When using a JComboBox as a cell editor in a JTable, it can be challenging to obtain the row associated with the changed JComboBox during an ItemEvent. This information is crucial for accessing other columns in the same row when updating based on the JComboBox's change.

Solution:

The key to solving this issue is to understand that the overridden method of the TableCellEditor, getTableCellEditorComponent(), includes the row as a parameter. Below is a solution that leverages this:

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

public class JComboBoxCellRowAccess {

    public static void main(String[] args) {
        JTable table = createTable();
        JComboBox<String> comboBox = new JComboBox<>(new String[] { "Item1", "Item2", "Item3" });
        TableColumn column = table.getColumnModel().getColumn(1);
        column.setCellEditor(new DefaultCellEditor(comboBox));

        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                int row = getCellRow(comboBox);
                System.out.println("Row: " + row);
            }
        });
    }

    public static int getCellRow(JComboBox<?> comboBox) {
        TableCellEditor editor = comboBox.getCellEditor();
        if (editor != null) {
            Component component = editor.getTableCellEditorComponent(null, null, true, -1, -1);
            return ((JTable) component.getParent()).getSelectedRow();
        }
        return -1;
    }

    public static JTable createTable() {
        DefaultTableModel model = new DefaultTableModel(new Object[][] { { "Row 0" }, { "Row 1" }, { "Row 2" } }, new String[] { "Column 1" });
        return new JTable(model);
    }
}
Copy after login

With this solution, you can access the row of the JComboBox within the ItemListener, allowing you to update or retrieve information from other columns in the same row as needed.

The above is the detailed content of How to Retrieve the Row Index of a JComboBox Within a JTable Cell During an ItemEvent?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template