我无法让我的程序将数据从txt导入Jtable

时间:2017-01-22 17:54:56

标签: java arrays file arraylist jtable

我的程序能够读取我的.txt文件,但出于某种原因,它无法将该数据放在我的桌面上。 (如果您需要我的代码的任何其他部分,请告诉我)。我不确定问题究竟是什么,但我几乎已经尝试将其输入到表格中,似乎没有任何效果。我能够手动将数据输入到表中(使用创建按钮)但由于某种原因,我无法导入数据。

//initializing all of the buttons (and jtable, scroll pane)
this.jScrollPane1 = new JScrollPane();
this.pokeTable = new JTable();
this.programTitle = new JLabel();
this.searchText = new JTextField();
this.searchButton = new JButton();
this.createButton = new JButton();
this.deleteButton = new JButton();
this.displayInstructionsButton = new JButton();
this.exportPokedex = new JButton();

//sets close operation
setDefaultCloseOperation(3);

//makes my Jtable
  this.pokeTable.setModel(new DefaultTableModel(
  new Object[0][], 
  new String[] {"Name", "Region", "PokeNumber", "Type", "Habitat" }) //creating column headings
  {
    boolean[] canEdit = new boolean[5];

    public boolean isCellEditable(int rowIndex, int columnIndex) //makes all of the cells editable
    {
      return this.canEdit[columnIndex]; 
    }
  });


  IO.openInputFile("data/pokemon.txt");
  int j =0;
  while(IO.readLine()!=null){   
    j++;
  }
  IO.closeInputFile();

  IO.openInputFile("data/pokemon.txt");

  String[] pokemon2 = new String[5];

  for(int i = j; i!=0; i--)
  {

    String s = IO.readLine();
    pokemon2 = s.split(",");


    DefaultTableModel model = (DefaultTableModel)this.pokeTable.getModel();
    model.addRow(new Object[] { pokemon2[0], pokemon2[1], Integer.valueOf(pokemon2[2]), pokemon2[3], pokemon2[4] });
     new Pokemon (pokemon2[0], pokemon2[1], Integer.valueOf(pokemon2[2]), pokemon2[3], pokemon2[4]);
    for(int f = 0; f < pokemon2.length; f++){
        System.out.println(pokemon2[f]);
        System.out.println("");
    }
  }

  IO.closeInputFile();  

this.jScrollPane1.setViewportView(this.pokeTable); //makes table able to scroll
this.jScrollPane1.getViewport().setBackground(new Color (233, 116, 81)); //sets background of table (not cell colour)
//end of intializing JTable

//sets title of the program
this.programTitle.setText("Pokedex");

2 个答案:

答案 0 :(得分:0)

我对你的建议是分开不同的变量,你试图在一步中做太多,而不是

this.pokeTable.setModel(new DefaultTableModel(
new Object[0][],new String[] {"Name", "Region", "PokeNumber", "Type", "Habitat" })

创建单独的变量

String[] headers = {"Name", "Region", "PokeNumber", "Type", "Habitat" };
Object[][] arr = new Object[fileLength][headers.length];
DefaultTableModel model = new DefaultTableModel(arr, headers);
this.pokeTable.setModel(model);

通常,这可以排除许多小错误,然后继续编写检查语句以精确缩小问题发生的位置;当它工作时你可以将它缩小到一行,但是将其分解通常足以找到并修复错误。也许在尝试创建表之前先尝试填充数组。我有一些代码可以实现这个结果,但数据库是:

Object[][] data;

//Get data from table and convert to 2D array
data = new Object[len][collumnNames.length];
String sql = "SELECT * FROM SHM";
ResultSet rs = st.executeQuery(sql);
int x = 0;
while (rs.next()){
    for(int i = 0; i < collumnNames.length; i++){
       data[x][i] = new Double(rs.getDouble(collumnNames[i]));
    }                
    x++;
}
rs.close();
//Output data
JFrame frame = new JFrame("Table");
TableModel model = new DefaultTableModel(data, collumnNames);
JTable table = new JTable(model);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.setAlwaysOnTop(true);
frame.pack();
frame.setVisible(true);

尝试以这种格式实现它,将其拆分以确保您的工作,在将其放入表中之前从文件创建2Darray,而不是创建表并尝试以这种方式向其添加新行;如果您以后想要添加新行,请将其重新绘制出来。如果这仍然不起作用,我不确定对不起。

答案 1 :(得分:0)

DefaultTableModel model = (DefaultTableModel)this.pokeTable.getModel();尝试将此行放在for循环中,然后重试。