Spreadsheet Focused Cell tracker

180 views
Skip to first unread message

Joe Van Steen

unread,
Jun 9, 2016, 7:22:10 PM6/9/16
to ControlsFX
Hi,
I am trying to build an application perspective which includes a form of spreadsheet modeling.The ControlsFX SpreadsheetView seems ideal except for some problems I am running into. One problem is the ability to track and document in a status bar (similar to the Excel Formula Bar) the identity of the current cell and secondary information about that cell.

In Excel, the Formula Bar reports the cell address of the current active cell which has focus. As the users moves through the spreadsheet, whether by mouse or keyboard activity that cell address reported in the Formula Bar changes to track the active cell. In addition, cells in the spreadsheet display the result of any formula calculations while the Formula Bar shows the formula content of the active cell which created that result.

This is similar to what I want to achieve. My problem is that I do not see any way of actively tracking the currently active cell on a passive basis. I am looking for something along the lines of a FocusedCellAddressProperty that I can attach a PropertyChangeListener to, but there doesn't seem to be such a property. If my code has control for some reason, for example if a CellType convertValue function is called, I can determine the currently active cell at that point. However, this requires my code to have already been invoked for some other reason. It is not a passive change listener.

Is there a way to passively track the focused cell, or is this a change request?

Thanks!

samir.ha...@gmail.com

unread,
Jun 10, 2016, 8:40:40 AM6/10/16
to ControlsFX
Hi Joe,

In my application, I am showing the range selection a bit like Excel. In order to do so, I've attached a Listener to the selectedCells of the SelectionModel of the SpreadsheetView :

spreadsheetView.getSelectionModel().getSelectedCells().addListener(selectedCellsListener);

My listener looks like that :  (I also compute a sum) :
/**
     * This InvalidationListener is used to calculate in real-time the sum of
     * the selected numbers (if any). It also changes the selected cell indices.
     */

   
private final InvalidationListener selectedCellsListener = new InvalidationListener() {

       
@Override
       
public void invalidated(Observable observable) {
           
               
//TotalSum
               
double value = 0;
               
for (TablePosition position : spreadsheetView.getSelectionModel().getSelectedCells()) {
                   
if (position != null && position.getRow() != -1 && position.getColumn() != -1) {
                       
SpreadsheetCell cell = spreadsheetView.getGrid().getRows().get(position.getRow()).get(position.getColumn());
                       
if (ShuttleUtils.canAdjustData(cell.getCellType()) && cell.getItem() != null && !Double.isNaN((double) cell.getItem())) {
                            value
+= (Double) cell.getItem();
                       
}
                   
}
               
}
                totalSumProperty
.setValue(value);
         

       
}
   
};

I'm also computing the range with a efficient algorithm (similar to the one in RectangleSelection, -> GridRange). If you are interested I can help you with that. Regarding your issue, you could get the focusedCell in the selectionModel when this listener is triggered.
If this is not sufficient, we can try to add a Property around the focusedCell in order to be able to track it. But in my memory, I didn't do that because it was quite difficult and time-consuming.

If you need more help around the SpreadsheetView, don't hesitate.

Regards,
Sam'

Joe Van Steen

unread,
Jun 10, 2016, 10:12:07 PM6/10/16
to ControlsFX
Sam,
Thanks! That did the job for me.

Joe
Reply all
Reply to author
Forward
0 new messages