简体   繁体   English

创建一个.txt文件,将其从JList中填充

[英]Create a .txt file populating it from a JList

Is it possible to write on a .txt file the content of a JList? 是否可以在.txt文件中写入JList的内容? If it's possible, can you give me a sample? 如果可以的话,您可以给我样品吗? Thanks 谢谢

A JList is not a data structure, but a displaying component. JList不是数据结构,而是显示组件。

You should have the contents in a ListModel, and if the elements of this model are simple Strings (or something easily convertible to Strings, you could of course write it in a text file. 您应该将内容包含在ListModel中,并且如果该模型的元素是简单的Strings(或易于转换为Strings的内容),则当然可以将其写入文本文件中。

public static void exportList(ListModel model, File f) throws IOException {
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
    try {
        int len = model.getSize();
        for (int i = 0; i < len; i++) {
            pw.println(model.getElementAt(i).toString());
        }
    } finally {
        pw.close();
    }
}

A list has a model and the model has data. 列表有一个模型,而模型有数据。 You just have to write that data to a file: 您只需要将该数据写入文件即可:

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
class ListDemo {
    public static void main( String ... args ) throws FileNotFoundException {
        // The data
        final Object [] data = {"A","B","C"};


        // Put it in the frame 
        JFrame frame = new JFrame();
        frame.add( new JScrollPane( new JList( data )));
        // write to a file  
        final PrintStream out = new PrintStream(new FileOutputStream("datos.txt"));
        frame.add( new JButton("Print"){{
            addActionListener( new ActionListener() {
                public void actionPerformed( ActionEvent e ) {
                    for( Object d : data ) { 
                            out.println( d );
                    }
                }
            });
        }}, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible( true );
    }
}

This is only a sample. 这只是一个示例。 You have to create a list model of our own and populate it with your own data. 您必须创建我们自己的列表模型,并使用您自己的数据填充它。

Also I'm not closing the file here. 我也没有在这里关闭文件。

To know more about JList read here: 要了解有关JList的更多信息,请单击此处:

http://download.oracle.com/javase/tutorial/uiswing/components/list.html http://download.oracle.com/javase/tutorial/uiswing/components/list.html

To know more about stream here: 要在此处了解有关流的更多信息:

http://download.oracle.com/javase/tutorial/essential/io/charstreams.html http://download.oracle.com/javase/tutorial/essential/io/charstreams.html

Is this a homework question ? 这是一个作业问题吗?
anyway yes it is possible from the the JList you can use the following method although i am sure its not the best way to do it it should work 无论如何,可以从JList中使用以下方法,尽管我确信它不是执行它的最佳方法,但它应该可以工作
where list is a JList list是一个JList

list.setSelectedIndex(int index); list.setSelectedIndex(int index); // sets one selection //设置一个选择
list.getSelectedValue(); list.getSelectedValue(); //returns Object //返回对象

or 要么

list.setSelectedIndices(int [] indices); list.setSelectedIndices(int [] index); // sets multiple selections //设置多个选择
list.getSelectedValues(); list.getSelectedValues(); //returns all selected values in an Object [] //返回对象[]中的所有选定值

for writing/reading/deleting/created read this 用于编写/阅读/删除/创建的阅读

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM