JTable table = new JTable(data, columnNames); String columnName = table.getColumnName(3); System.out.println("Name of column at index 3: " + columnName);
DefaultTableModel model = (DefaultTableModel) table.getModel(); int colIndex = model.findColumn("ID"); String columnName = table.getColumnName(colIndex); System.out.println("Name of column with header \"ID\": " + columnName);This example demonstrates how to get the name of a column with a specific header text. First, the table model is retrieved from the JTable object, and then the index of the column with header "ID" is found using findColumn(). Finally, the name of the column at that index is retrieved using getColumnName() and printed to the console. Package/library: java.swing.