How to extract named column from tablemodel in java swing
<p>I have a table that shows the results of user defined queries in a swing project. I want to allow the user to extract the data from a particular names column, if present. At the moment I can select the data from a column when I click the column but I don't know how to do the same thing from a button so that only a particular column's data (the column is called <code>HNum</code>) is obtainable. The code I have so far is as follows. If this is impossible I could always try to make sure that <code>HNum</code> is the first column but I need something cleaner I think.</p>

<pre><code>btnCompare.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Object[] data_L = columnToArray(table,table.getSelectedColumn());
    }
}


public Object[] columnToArray(JTable table, int columnIndex){
    // get the row count
    int rowCount = table.getModel().getRowCount();

    // declare the array
    Object [] data = new Object[rowCount];

    // fetch the data
    for(int i = 0; i &lt; rowCount; i++){
        data[i] = table.getModel().getValueAt(i, columnIndex);        
    }

    return(data);
}
</code></pre>