1

I'm working on a project for work involving a JTable with a dynamic number of columns. Each column is basically a separate transaction but I do not know the number of transactions a file will have ahead of time.

Typically when I create a JTable I know how many columns I will have and I declare it like this:

String header[] = new String[]{

                "Tag","Transaction1"

        };

For this project however there can be any number of transactions each time the program is used so I would need to dynamically add columns based upon the length of a certain array before I even create my rows. (The first row is actually going to also be used as a header).

So I have an array with a given length, but I don't know how to use this value in a loop, at least not with creating an object like the code above shows.

For example let's say the user uploads a file that has 3 transactions.. I would need my String header[] to read:

String header[] = new String[]{

                "Tag","Transaction1","Transaction2","Transaction3"

        };

I'd considered possibly creating an array list and adding the transactions to this using a counter and a loop, then possibly extracting the values into the String[] header although I'm not sure if this is the best approach and even still how exactly to make it work.

jesric1029
  • 698
  • 3
  • 10
  • 33
  • 1
    http://stackoverflow.com/questions/7969023/from-arraylist-to-array – NAMS Mar 30 '16 at 15:31
  • That doesn't even begin to answer my question. That is simply about converting ArrayList to Array which isn't what I am asking.. I'm asking how to dynamically add columns or dynamically fill the header object. – jesric1029 Mar 30 '16 at 15:32
  • 1
    Either replace the arrays when you get new data or use lists. Either way it would probably be best to provide your own table model implementation. If you know the data before creating the arrays then just create it like `String header[] = new String[numTransactions + 1]` then use a loop and fill the elements as needed. – Thomas Mar 30 '16 at 15:33
  • 1
    You can dynamically add items to an ArrayList and convert it to an Array of the same size as described in the answer to that post. Or you can just use the ArrayList on its own. – NAMS Mar 30 '16 at 15:35

5 Answers5

3

I actually found the answer to this.. Apparently I need to scrap the entire array and add them like this..

DefaultTableModel tableModel = new DefaultTableModel();

for(String columnName : columnNames){
   tableModel.addColumn(columnName);
}

jTable.setModel(tableModel);
jesric1029
  • 698
  • 3
  • 10
  • 33
2

A good option for you would be to have an ArrayList to dynamically add or remove elements from the List.

Then, when necessary, you can turn that ArrayList into an array of Strings, like..

ArrayList<String> elements = new ArrayList<String>();
elements.add("Transaction 1");
elements.add("Transaction 2");
elements.add("Transaction 3");

Object[] elementArray = elements.toArray();
Cristian Meneses
  • 4,013
  • 17
  • 32
  • Ah now that is perfect! My parsed data is already stored in an array list so instead of me having to extract it using a loop I can literally just convert to an Object and then fill the table. That is perfect! Thank you, more efficient than the way the program is now! I'm not actually calling the columns "Transaction1" I did that here for simplicity I'm actually using the same values that are in the array list so this works. – jesric1029 Mar 30 '16 at 15:40
1

I thing you dont need to use an Array, use better an List, this because you can increase the size as much as you need, iterate it, parse it to string-Arrays etc.

Example

    List<String> transactions = new ArrayList<String>();
    transactions.add("Tag");

    // later
    transactions.add("Transaction1");
    transactions.add("Transaction2");

    // print it
    for (final String string : transactions) {
        System.out.println(string);
    }
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I tried your solution out, I was forced to add a cast to it as Vector. Not sure if it will work but I'll test your answer out and mark it as correct as it works and would be more efficient than my solution. – jesric1029 Mar 30 '16 at 15:36
1
    List<String> headerList = new ArrayList<>();
    headerList.add("Tag");
    for(int i=1; i <= transactions.length; i++){
        headerList.add("Transaction" + i);
    }
    String[] header = headerList.toArray(new String[headerList.size()]);
1

I believe what you're looking for is an ArrayList, not an Array nor List, as this allows for dynamic allocation.

The syntax would be:

List<String> header = new ArrayList<String>();
header.add("Tag");

That initializes it. Then, use length() from your File class to set a parameter for a loop, then dynamically add the result of string concatenation with the "Transaction" + your loop index to your ArrayList.

That'd look like:

for (int i = 1; i <= file.length(); i++){
    header.Add("Transaction" + i);
}

And, then you can convert it back to an array of strings with:

 String[] headerArray = header.toArray(new String[header.size()]);