Retrieving the CellRow of an ItemEvent in a Table ComboBox
When working with a JTable containing columns with Combo Boxes, it can be necessary to obtain the row of the ComboBox that triggered an ItemEvent. However, the ItemListener lacks a method for this.
How to Determine the CellRow
To retrieve the desired row, consider the following options:
Using the TableCellEditor
In "Using a Combo Box as an Editor," the TableCellEditor's getTableCellEditorComponent() method includes the row as a parameter. This method can be used to access the row.
Synchronizing Dependent Columns
Utilizing the getValueAt() Method
By overriding the model's getValueAt() method, you can establish a relationship between the ComboBox column and other columns within the same row. Modified code:
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; 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() {
The above is the detailed content of How to Retrieve the Row Index of a Table ComboBox Triggering an ItemEvent?. For more information, please follow other related articles on the PHP Chinese website!