-1

很抱歉,这是我第一次在这里发帖,我真的非常非常不擅长 java。

我的JTable一个面板中有一个,我需要做的就是将内容保存到一个文件中。当将某个字符串输入到JTextfield中时,文件会加载并填充JTable.

我不知道从哪里开始,我在 youtube 上搜索了帮助,但我很不走运。请帮帮我?

4

1 回答 1

0
 public Object[][] getTableData (JTable table) {
       (DefaultTableModel) dtm = (DefaultTableModel) table.getModel();
           int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
           Object[][] tableData = new Object[nRow][nCol];
           for (int i = 0 ; i < nRow ; i++)
               for (int j = 0 ; j < nCol ; j++)
                   tableData[i][j] = dtm.getValueAt(i,j);
           return tableData;
       }

您可以将 Jtable 中的值存储为数组,

之后将其写入文件...

    public void writeToFile()
      {
      try{
      FileWriter fstream = new FileWriter("out.txt");
      BufferedWriter out = new BufferedWriter(fstream);
      for(int i=0;i<array.length;i++){
            out.write(array[i].getBytes());
      }
      out.close();
      }catch (Exception e){
      System.err.println("Error: " + e.getMessage());
      }
于 2012-12-13T06:25:58.893 回答