在Kotlin中如何调用JTable.getColumn?调用抛参数异常求解决
Hey there! I’ve run into this exact pitfall before—let’s break down what’s happening and how to fix it quickly.
Why the error happens
The JTable.getColumn() method does not take a column index as its argument. Instead, it expects a column identifier (an object that uniquely identifies the column, which defaults to the column name if you didn’t set a custom one). When you pass 0 (an integer), the JTable tries to find a column with 0 as its identifier—and since that doesn’t exist, it throws the IllegalArgumentException: Identifier not found error.
Correct ways to get a column in Kotlin
Here are two reliable approaches depending on what you need:
1. Get column by index (what you probably intended)
To fetch a column using its position index, use the table’s ColumnModel instead:
val yourTable = JTable(...) // Get the first column (index 0) val targetColumn = yourTable.columnModel.getColumn(0)
Kotlin lets you use property access syntax for columnModel, so this works too:
val targetColumn = yourTable.columnModel[0]
2. Get column by identifier (if you need this specifically)
If you really want to use JTable.getColumn(), make sure you pass the correct identifier for the column. For example:
- If you’re using default column names:
// Assuming your first column is named "User ID" val targetColumn = yourTable.getColumn("User ID") - If you set a custom identifier when creating the column:
// Create a column with a custom identifier val customColumn = TableColumn(0).apply { identifier = "user_identifier" } yourTable.columnModel.addColumn(customColumn) // Fetch using the custom identifier val targetColumn = yourTable.getColumn("user_identifier")
Quick recap
- Use
table.columnModel.getColumn(index)when you know the column’s position. - Use
table.getColumn(identifier)only when you have the column’s unique identifier (not its index).
内容的提问来源于stack exchange,提问作者paj28

