Home > Java > javaTutorial > How to Determine the JTable Row Containing a JComboBox That Triggered an ItemEvent?

How to Determine the JTable Row Containing a JComboBox That Triggered an ItemEvent?

Patricia Arquette
Release: 2024-12-17 16:04:14
Original
649 people have browsed it

How to Determine the JTable Row Containing a JComboBox That Triggered an ItemEvent?

Determining the Row Associated with a JComboBox Cell in a JTable

Initial Question:

You aim to ascertain the row number within a JTable that contains a JComboBox within a specific cell when an ItemEvent is triggered in the JComboBox. This row number is crucial for subsequent actions involving another cell in the same row after the JComboBox modification.

Response:

It appears that you're utilizing a JComboBox as an editor within the JTable. In such scenarios, the getTableCellEditorComponent() method of the TableCellEditor class provides access to the row as one of its parameters. Refer to the following resources for further insights:

  • Java Tutorial: Using a Combo Box as an Editor
  • Example: Customizing the ComboBox Editor

Addendum for Value Synchronization:

To synchronize the value of a dependent column with that of the JComboBox column, override the getValueAt() method of the table model. This allows you to return the updated value based on the altered content of the JComboBox column.

Additional Example:

The below code showcases this approach, keeping a dependent column synchronized with the JComboBox column using the overridden getValueAt() method:

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Example {

  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) {
    // Create table model
    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);
        }
      }
    };

    // Add rows to table model
    for (int i = 0; i < 16; i++) {
      model.addRow(new Object[] {"C1", "C2", "Item1"});
    }

    // Create table and customize JComboBox column
    JTable table = new JTable(model);
    TableColumn col = table.getColumnModel().getColumn(ITEM_COL);
    String[] items = {"Item1", "Item2", "Item3"};
    JComboBox combo = new JComboBox(items);
    col.setCellEditor(new DefaultCellEditor(combo));

    // Create the frame and add the table
    JFrame frame = new JFrame();
    frame.add(new JScrollPane(table));
    frame.pack();
    frame.setVisible(true);
  }
}
Copy after login

The above is the detailed content of How to Determine the JTable Row Containing a JComboBox That Triggered 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