2011-05-20 62 views
-2

我有這樣的代碼,以顯示我的文件到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

只要你有一個錯誤消息發佈,它是從哪裏來的線。 – jzd 2011-05-20 19:09:23

+1

其實,如果你看一下代碼,一個方便的評論就會告訴我們。 – Buhb 2011-05-20 19:10:13

+0

@Buhb,是的,但這需要潛在的搜索答案並可能放棄。如果OP僅列出相關的細節,OP將有更好的答案機會。 – jzd 2011-05-20 19:12:20

回答

3

Java不知道你的列表包含String數組。

應聲明rosterList這樣的:

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

甚至更​​好:

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

好的謝謝!!!!我可以在這裏添加一個jbutton嗎? – 2011-05-20 19:46:15

+0

嘗試,如果遇到問題,請提出另一個問題。 – Buhb 2011-05-20 19:50:16

+0

我必須創建一個新的按鈕類或添加代碼createAndShowGUI()? – 2011-05-20 20:27:31