简体   繁体   English

如何使用扫描仪将文本文件读取到Swing控制组件中?

[英]How do you read a textfile using a scanner into a Swing control component?

In an array class I have the following code and would like to use the name, surname and score variables to be loaded into a jList. 在数组类中,我有以下代码,并希望使用名称,姓氏和分数变量加载到jList中。 How would I do so? 我该怎么办?

private static User uArr [] = new User [10];
private static int size = 0;
public UserArray () throws FileNotFoundException {

try
{
    Scanner fcFile = new Scanner (new File ("user.txt"));
    String line, name, surname;
    int score;

    while (fcFile.hasNext())
    {
        line = fcFile.nextLine();
        Scanner cfFile = new Scanner (line); 
        name = cfFile.next(); 
        surname = cfFile.next();
        score = cfFile.nextInt(); 
        cfFile.close();
        uArr[size] = new User (name,surname,score);
        size++;
    }

    fcFile.close();

}
catch (FileNotFoundException f)
{
    System.out.println("File Not Found - Check File Name And Path Again.");
}

would like to use the name, surname and score variables to be loaded into a jList 想要使用名称,姓氏和分数变量加载到jList中

A JList is used to store a single piece of data. JList用于存储单个数据。 Since you want to display an object containing 3 pieces of data then you should use a JTable. 由于要显示包含3个数据的对象,因此应使用JTable。 Read the section from the Swing tutorial on How to Use Tables for more information and examples to get you started. 阅读Swing教程中有关如何使用表的部分,以获取更多信息和示例以开始使用。

When using a JTable you could use the DefaultTableModel and store each piece of data separately into the TablemModel without using your User class. 使用JTable ,可以使用DefaultTableModel并将每个数据分别存储到TablemModel而无需使用User类。

Or, if you want to add the User class to the JTable then you will need to create a custom TableModel. 或者,如果要将User类添加到JTable则需要创建一个自定义TableModel。 Check out Row Table Model for a step-by-step example of creating a custom model for an object. 请查看行表模型 ,以获取为对象创建自定义模型的分步示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文本文件中读取所有字符到字符串(使用扫描程序) - How to read all the chars into a String from a textfile (using scanner) 您如何以与使用扫描仪阅读 .txt 文件相同的方式阅读 .PDF 文件。 这是在java for android - How do you read a .PDF file the same way you read .txt file using scanner. This is in java for android 您如何要求用户不使用扫描仪来填充阵列? - How do you ask a user to fill an array without using a scanner? Java-如何使用bufferedReader和Scanner将数据读入列表? - Java - How do I read data into a list using bufferedReader and Scanner? 你如何使用Java(Swing)获得标题栏的高度? - How Do You Get the Height of the Titlebar Using Java (Swing)? 如何使用 SWING/AWT 绘制 Java 中的 Mandelbrot 集? - How do you draw the Mandelbrot Set in Java using SWING/AWT? 如何使用带有空格的扫描仪类读取文件? - How do I read files using the scanner class with a space delimeter? 使用扫描仪/阅读器/ BufferedReader读取文本文件以读取.txt文件中的数字 - Reading a textfile using scanner/reader/bufferedreader to read numbers in .txt-file Swing:如何读取组件下方的图形信息? - Swing: How to read graphic information underneath a component? Java Swing:将UI组件与逻辑行为分开 - 您如何处理这个问题? - Java Swing: separating UI component from logic behaviour - how do you approach this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM