2 Replies Latest reply: Dec 12, 2012 1:54 PM by eno g. - oracle RSS

    populating a tableview from multiple data sources (text files)

    eno g. - oracle

      I would like to open multiple text delimited files and display them in a single tableview. I need to be able to track each entry in the table to its file source (e.g. when I select a row I want to know which file and line number it came from).

      Is it possible to create an ObservableMap which would have the filename as a the key and its data as value, and populate the TableView from each of the data in the hashmap?

        • 1. Re: populating a tableview from multiple data sources (text files)
          It's possible, I'm sure...

          But why not just make the File and line number properties of the data type which the table is displaying?
          final TableView<MyDataType> table ;
          final Label sourceLabel = new Label();
          sourceLabel.textProperty().bind(new StringBinding() {
            { super.bind(table.getSelectionModel().selectedItemProperty()); }
            public String computeValue() {
              MyDataType selectedItem = table.getSelectionModel().getSelectedItem();
              if (selectedItem==null) {
                return "" ;
              } else {
                return String.format("%s:%d", selectedItem.getSourceFile(), selectedItem.getSourceFileLineNumber());
          public class MyDataType {
            private File sourceFile ;
            private int sourceFileLineNumber ;
            // ... all the other stuff you need for your table
            // get/set methods for the two fields.
          Obviously replace MyDataType with whatever class you're using to represent the data in your table.

          If you can't alter that class (eg you're using a third party library), you can always create a simple wrapper for it.
          • 2. Re: populating a tableview from multiple data sources (text files)
            eno g. - oracle
            Thanks for the input! Yes that'd be one way to do it... I'm trying to keep as small of a footprint as possible as I expect to be dealing with somewhat large files. Maybe one more text field with the absolute path of the source file won't be that much, but I'm already thinking on how to discard data from the file itself to keep the smallest footprint possible. Also, I was thinking that (depending on requirements) it may be easier to deal with each file's content in one shot if they were separated into their own objects as opposed to iterating through the whole data set to pick at it line by line.

            After having a couple of beers last night I realized I made a fool of myself with the above comment. I'd be passing a reference to the File paths and not copy it to each instance of the table row. Thanks James.

            Edited by: Eno G. on Dec 12, 2012 11:51 AM