0

I have a process:

  • get widths of column table

  • refresh

  • set widths of column table

Here are my methods

This gets the column width table

public ArrayList<Integer> getJTableColumnsWidth() {
  ArrayList<Integer> list = new ArrayList<>();
    for (int i = 0; i < tableR.getColumnModel().getColumnCount(); i++) 
    {
        TableColumn column = tableR.getColumnModel().getColumn(i);
        System.out.print(column.getPreferredWidth());
        list.add(column.getPreferredWidth());

    }
 return list;    
} 

There are 18 columns in my table.

I then want to pass each number of my arraylist into a method like below..

setTableWidths(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18)

where each number is just each number in the arraylist and contains a width.

I can call each number of the arraylist by calling

getJTableColumnsWidth()

but then how is this passed directly to setTableWidths(x1,x2,x3,x4,x5....)?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ingram
  • 654
  • 2
  • 7
  • 29
  • 1
    You mean like `list = getJTableColumnsWidth();` then `setTableWidths(list.get(0), list.get(1), ...)`? – OneCricketeer Jan 14 '16 at 21:03
  • @ cricket_007 is there a more elegant way of doing this since if i had 100 columns this could get a little tedious.. – Ingram Jan 16 '16 at 21:07
  • I would just give the list itself as a parameter. You could then do a for-loop over the list within the method to set all the column widths – OneCricketeer Jan 17 '16 at 02:30

1 Answers1

1

You have an ArrayList<Integer>, and want to pass its contents as arguments to a method.

If the method is defined to accept a fixed number of arguments, then you can pass those arguments one-by-one.

setTableWidths( list.get(0), list.get(1), list.get(2) ... // etc.

Alternatively, you could define a setTableWidths() method that accepts a List<Integer> or array.

If the method supports a variable number of arguments, then you can convert the list into an array, and pass the array to the method:

setTableWidths( list.toArray( new Integer[0] ));
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • No, you can pass a single array containing all the arguments. If the method is declared `setTableWidths( Integer... args )`, then args is of type Integer[]. More detail on this capability is in the prior question: ["Can I pass an array as arguments to a method with variable arguments in Java?"](http://stackoverflow.com/questions/2925153/can-i-pass-an-array-as-arguments-to-a-method-with-variable-arguments-in-java) – Andy Thomas Jan 14 '16 at 21:08
  • I don't think OP has a method with variable arguments – OneCricketeer Jan 14 '16 at 21:12
  • 1
    @cricket_007 - Good point, that was unclear. I've covered both cases. – Andy Thomas Jan 14 '16 at 21:18
  • @Andy Thomas is there a more elegant way of doing this since if i had a table with 100 columns, this could be a bit painful...? – Ingram Jan 16 '16 at 21:04
  • @MrAssistance - You could pass an array or a list. Or you could write a loop that gets and sets one width at a time. Or sometimes a UI framework will let you define a method that provides the width of an indexed column when requested> – Andy Thomas Jan 17 '16 at 16:47