JTable和数组值问题

时间:2011-05-20 19:03:49

标签: java swing arraylist jtable

我有这段代码将我的文件显示到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();
    }


}




}

1 个答案:

答案 0 :(得分:3)

Java不知道您的列表包含String数组。

你应该像这样声明rosterList:

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

甚至更好:

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