【问题标题】:JTable and array value problemJTable和数组值问题
【发布时间】:2011-05-20 19:03:49
【问题描述】:

我有这段代码可以将我的文件显示到JTable,但我有一个错误

array required, but java.lang.Object found

这是我的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.*;
import java.util.*;


public class TableDemo extends JPanel {
    private boolean DEBUG = false;
    static ArrayList rosterList = new ArrayList();   // added static infront becuase got non static referencing error

    public TableDemo() {
        super(new GridLayout(1,0));

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "Κωδικός", "Ποσότητα", "Τιμή", "Περιγραφή", "Μέγεθος", "Ράτσα"};


        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rosterList.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

     public Object getValueAt(int row, int col)
        {
             return rosterList.get(row)[col];  //array required,but java.lang.Object found

        }


    }


    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                creatArr();
                createAndShowGUI();

            }
        });
    }


private static void creatArr()
  {
     BufferedReader br = null;

     try
    {
      br = new BufferedReader(new FileReader("Dogss.txt"));
      String line = br.readLine();

      while (line != null )
      {
        String [] rowfields = line.split("#");
        rosterList.add(rowfields);
        line = br.readLine();
       }

    }
    catch (FileNotFoundException e)
    {
      // can be thrown when creating the FileReader/BufferedReader
      // deal with the exception
      e.printStackTrace();
    }
    catch (IOException e)
    {
      // can be thrown by br.readLine()
      // deal with the exception
      e.printStackTrace();
    }


}




}

【问题讨论】:

  • 每当您有错误消息时,发布它来自的行。
  • 实际上,如果你看一下代码,一个方便的注释就可以告诉我们。
  • @Buhb,是的,但这需要潜在的搜索答案并可能放弃。如果 OP 只列出相关的详细信息,他们将有更好的回答机会。
  • @jzd,他实际上在错误返回 rosterList.get(row)[col]; //需要数组,但是找到了 java.lang.Object 和@user563883,你不能在返回中调用它。你要么调用 rosterList.get(row) ,然后你就用它做你想做的事,或者你把你的 List 重新声明为@Buhb 回答

标签: java swing arraylist jtable


【解决方案1】:

Java 不知道您的列表包含字符串数组。

你应该像这样声明 rosterList:

static ArrayList<String[]> rosterList = new ArrayList<String[]>();

甚至更好:

static List<String[]> rosterList = new ArrayList<String[]>();

【讨论】:

  • 试试看,如果遇到问题,再问一个问题。
  • 我必须创建一个新的按钮类或添加代码到 createAndShowGUI() ?
猜你喜欢
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
相关资源
最近更新 更多