将数据从txt文件显示到JTable

时间:2011-03-16 14:42:32

标签: java swing file-io jtable jframe

我的txt文件包含:

约翰
1.0
新手
编辑
3.0
奇才
第一行是玩家的名字,第二行是得分,第三行是等级
我想在JTable中显示我的数据,这是我的代码:

package splashdemo;

import java.awt.BorderLayout;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTable;


public class Hscore extends javax.swing.JFrame {

   public static String hname[]= new String[50];
   public static double hscore[]= new double[50];
   public static String hlevel[]= new String[50];
   public static String data[][]= new String[50][3];
   public Hscore() {
        initComponents();
        String col[] = {"Name","Course","Grade"};
        JTable table = new JTable(data,col);
        jPanel1.add(table,BorderLayout.CENTER);
                JFrame frame= new JFrame();
        frame.add(jPanel1);
        frame.setSize(300,200);
        frame.setVisible(true);
    }
public void op(){

}
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jButton1.setText("jButton1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 370, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        
//=====Set HScore
public void setHScore(){

        File oFile=new File("C:\\Users\\sony\\Documents\\NetBeansProjects\\SplashDemos\\HScore.txt");
        FileInputStream fis=null;
        BufferedInputStream bis=null;
        DataInputStream dis=null;
                int iIndex=0;
                String name;

        try{
            fis=new FileInputStream(oFile);
            bis=new BufferedInputStream(fis);
            dis=new DataInputStream(bis);

        while(dis.available()!=0){
                        hname[iIndex]=dis.readLine();
            name=dis.readLine();
                        hscore[iIndex]=(int) Double.parseDouble(name);
                        hlevel[iIndex]=dis.readLine();
  

data [iIndex] [iIndex] = {{hname [iIndex],hscore [iIndex],hlevel [iIndex]}}; //此行是错误......

            iIndex++;
        }
                fis.close();
        bis.close();
        dis.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
//==============
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Hscore().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   

}

2 个答案:

答案 0 :(得分:1)

  

如何在jtable中显示我的数据..

你不明白什么?

您知道如何使用硬编码数据创建表格吗?首先使其工作,然后使用文件中的动态数据。

您知道如何从文件中读取数据吗?我建议你使用FileReader和BufferedReader。从I/O examples

开始

答案 1 :(得分:0)

将行更改为

data[iIndex]=new String[]{hname[iIndex], String.valueOf(hscore[iIndex]), hlevel[iIndex]};

使它工作,而不仅仅是代码中需要更改很多。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JTable;


public class Hscore extends javax.swing.JFrame {

    public static String hname[]= new String[50];
    public static double hscore[]= new double[50];
    public static String hlevel[]= new String[50];
    public static String data[][]= new String[50][3];
    public Hscore() {
        initComponents();
        setHScore();
        String col[] = {"Name","Course","Grade"};
        JTable table = new JTable(data,col);
        table.setPreferredSize(new Dimension(200,200));
        jPanel1.add(table,BorderLayout.CENTER);
        getContentPane().add(jPanel1);
        setSize(300,200);
        setVisible(true);
    }
    public void op(){

    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jButton1.setText("jButton1");

        /*javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                                  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addContainerGap()
                                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                          .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 370, Short.MAX_VALUE)
                                          .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addContainerGap())
        );*/

        pack();
    }// </editor-fold>                        
    //=====Set HScore
    public void setHScore(){

        File oFile=new File("C:\\Users\\sony\\Documents\\NetBeansProjects\\SplashDemos\\HScore.txt");
        FileInputStream fis=null;
        BufferedInputStream bis=null;
        DataInputStream dis=null;
        int iIndex=0;
        String name;

        try{
            fis=new FileInputStream(oFile);
            bis=new BufferedInputStream(fis);
            dis=new DataInputStream(bis);

            while(dis.available()!=0){
                hname[iIndex]=dis.readLine();
                name=dis.readLine();
                hscore[iIndex]=(int) Double.parseDouble(name);
                hlevel[iIndex]=dis.readLine();
                data[iIndex]=new String[]{hname[iIndex], String.valueOf(hscore[iIndex]), hlevel[iIndex]};

                iIndex++;
            }
            fis.close();
            bis.close();
            dis.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    //==============
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Hscore().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   

}