0

I am trying to input an array of number into a JTable but having problems. Namely the error 'java.lang.double[][] cannot be converted to javax.swing.TableModel ' I am assuming the issue is to do with a JTable not being able to read a double but some clarification or a fix would very helpful thanks.

// Data to be displayed in the JTable, 
  double[][]  mile = { 
  {1}, 
  {2},
  {3 }, 
  {4 },
  {5 },
  {6 },
  {7 },
  {8 },
  {9 },
  {10 },
  {11 },
  {12 },
  {13 },
  {14 },
  {15 },
  {16 },
  {17 },
  {18 },
  {19 },
  {20 },
  }; 


  double[][]  kilo = { 
  { 1.609 }, 
  { 3.218 },
  { 4.827 }, 
  { 6.436 },
  { 8.045 },
  { 9.654  },
  { 11.263 },
  { 12.872 },
  { 14.481 },
  { 16.09 },
  { 17.699 },
  { 19.308 },
  { 20.917 },
  { 22.526 },
  { 24.135 },
  { 25.825 },
  { 27.434 },
  { 29.043 },
  { 30.654 },
  { 32.261 },
  }; 


  // Column Names for the table.
  String[] titles = { "Miles", "Kilometers" }; 

  // Create the table which is going to display the information.
  JTable table = new JTable(mile, kilo, titles);
  JScrollPane scrollPane = new JScrollPane(table);
  scrollPane.setBounds(20,20,650,250);
  table.setFillsViewportHeight(true);
  infoPanel.add(scrollPane);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
OwenR98
  • 28
  • 5
  • [`JTable` has no such constructor](https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html). – BeUndead Mar 02 '20 at 12:12
  • 1) `scrollPane.setBounds(20,20,650,250);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 02 '20 at 12:13
  • Without knowing how you want it to look, it's going to be difficult to answer. _But_ at a guess you want to _combine_ the two arrays: `{1, 1.609}, {2, 3.218}, ...` to get what you want, since those become the rows of your table. – BeUndead Mar 02 '20 at 12:13
  • @BeUndead Yes I do, but for the moment my issue is that the table will not accept the double and throws the error code. I'm guessing your first response was in answer to that? – OwenR98 Mar 02 '20 at 12:15
  • Yes. `JTable`'s constructor (link above) takes _one_ array (of arrays) which becomes the data to the table. You're calling it with 2, which there's no constructor for (hence the compiler error). – BeUndead Mar 02 '20 at 12:17
  • I dont tink your array initialization is proper look at https://stackoverflow.com/questions/18578864/double-array-initialization-in-java/47036616 – Dimaka Mar 02 '20 at 12:19

1 Answers1

0

As mentioned in the comments, the JTable does not provide the constructor you want. For my part i would use the following constructor: public JTable(final Object[][] rowData, final Object[] columnNames)

But for this you have to adjust your data initialization.
The code would look like this:

    // Data to be displayed in the JTable,
    double[] mile = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    double[] kilo = { 1.609, 3.218, 4.827, 6.436, 8.045, 9.654, 11.263, 12.872, 14.481, 16.09, 17.699, 19.308, 20.917, 22.526, 24.135, 25.825, 27.434, 29.043, 30.654, 32.261 };

    Double[][] tableData = new Double[mile.length][2];

    for (int i = 0; i < mile.length; i++) {
        tableData[i][0] = mile[i];
        tableData[i][1] = kilo[i];
    }


    // Column Names for the table.
    String[] titles = {"Miles", "Kilometers"};

    // Create the table which is going to display the information.
    JTable table = new JTable(tableData, titles);
    table.setFillsViewportHeight(true);
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(20, 20, 650, 250);
    infoPanel.add(scrollPane);
tgallei
  • 827
  • 3
  • 13
  • 22
  • Thanks, that solves the issue, the for loop you have used there, is that to populate the table? – OwenR98 Mar 02 '20 at 12:37
  • Note: `mile[i] = i + 1` and kilo[i] = 1.609 * mile[i]` – user85421 Mar 02 '20 at 12:40
  • @OwenR98 Yes, the for loop converts the data into the format required by the JTable. More information about the JTable and its TableModel can be found [here](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – tgallei Mar 02 '20 at 12:41