【问题标题】:Trying to input a double[][] array into a JTable尝试将 double[][] 数组输入 JTable
【发布时间】:2023-03-17 19:55:01
【问题描述】:

我正在尝试将一个数字数组输入到 JTable 中,但遇到了问题。即错误'java.lang.double[][] 无法转换为javax.swing.TableModel' 我假设问题与JTable 无法读取双精度但一些澄清或修复将非常有帮助谢谢。

// 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);

【问题讨论】:

  • 1) scrollPane.setBounds(20,20,650,250); Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2)为了尽快获得更好的帮助,edit添加minimal reproducible exampleShort, Self Contained, Correct Example
  • 在不知道您希望它的外观的情况下,很难回答。 但是猜测你想组合这两个数组:{1, 1.609}, {2, 3.218}, ... 得到你想要的,因为它们成为你表格的行。
  • @BeUndead 是的,我愿意,但目前我的问题是该表不接受双精度并抛出错误代码。我猜你的第一反应是回答这个问题?
  • 是的。 JTable 的构造函数(上面的链接)采用 one 数组(数组),它成为表中的数据。你用 2 调用它,它没有构造函数(因此编译器错误)。

标签: java swing compiler-errors jtable


【解决方案1】:

如 cmets 中所述,JTable 不提供您想要的构造函数。 就我而言,我将使用以下构造函数: public JTable(final Object[][] rowData, final Object[] columnNames)

但为此,您必须调整数据初始化。
代码如下所示:

    // 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);

【讨论】:

  • 谢谢,这解决了问题,你在那里使用的 for 循环是用来填充表格的吗?
  • 注:mile[i] = i + 1 和kilo[i] = 1.609 * mile[i]`
  • @OwenR98 是的,for循环将数据转换成JTable要求的格式。有关 JTable 及其 TableModel 的更多信息可以找到here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2013-03-01
  • 2021-08-29
相关资源
最近更新 更多