0

I'm writing a program to do some statistical analysis on data imported from a csv file or database. I can get the data loaded into a jTable and displayed but I'm struggling with the next step.

I would like to be able to click on a column heading and have the summary stats for the contents of the column displayed in a label in a panel to the side of the jTable (see image).

enter image description here

Can anyone suggest methods to look at or example code for similar projects? Any help would be appreciated.

EDIT: I'm doing this in netbeans. Usually in netbeans I just click on the component in design mode and add a listener by right clicking and then filling in the code on the source tab. However, I'm not sure how to add a listener to a table or header when it's not visible in the design tab.

screechOwl
  • 27,310
  • 61
  • 158
  • 267
  • What is the difficulty you are facing? Is it not being able to get the column contents? Or is it not being able to get the column being selected? – vaisakh Apr 07 '12 at 19:10
  • @vaisakh: A bit of both. I'm assuming I need a listener of some kind but I just have never done this before and usually use other people's code for similar projects (mainly because I find the javadocs to be unreadable/useless) but can't find anything similar to tweak. – screechOwl Apr 07 '12 at 19:14

3 Answers3

1
JTable table = ...
TableColumnModel columnModel = table.getColumnModel();
columnModel.add(new TableColumnModelListener() {
    // other methods
    public void columnSelectionChanged(ListSelectionEvent e) {
        // user selected or deselected a column, change summary as necessary
    }
}

TableColumnModel reference

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • +1 for the reasons outlined [here](http://stackoverflow.com/a/7146216/230513), although there's an example header component [nearby](http://stackoverflow.com/a/7137801/230513).. – trashgod Apr 07 '12 at 23:32
1

For the listener part I used the information here:

Listening for Clicks on a Column Header in a JTable Component

screechOwl
  • 27,310
  • 61
  • 158
  • 267
1

So, the problem has two parts:-

  1. The event handler to execute when the column is selected.

  2. The code to get the summary.

For the first one (event handler), you could refer to @Jeffrey's answer. For the summary part you could write a method like this:

/* Method to return values in a column of JTable as an array */

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 < rowCount; i++){
        data[i] = table.getModel().getValueAt(i, columnIndex);        
    }
    return(data);
}

Call this method from inside the event handler like this:

public void columnSelectionChanged(ListSelectionEvent e) {
    //assuming single column is selected
    Object[] data = columnToArray(table,table.getSelectedColumn());
   /* type cast if using specific data type. for eg:
    * Integer[] data = (Integer[]) columnToArray(table,table.getSelectedColumn());
    */
    // other functions to create the summary
}

The object array could be used to compute the summary you require, like finding the range, Standard deviation, etc. Those should be trivial. Remember to typecast the Object array in the calling method.

vaisakh
  • 1,041
  • 9
  • 19