将jList数据保存到txt文件中的方法? [英] Way to save jList data into a txt file?

查看:162
本文介绍了将jList数据保存到txt文件中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将jlist中的用户输入保存到txt文件中的方法.这是一个家庭作业项目.我已将程序设置为从文本字段获取用户输入,然后在他们点击添加按钮时将其输入到jlist中. jlist上有一个defaultlistmodel,我想知道是否有一种方法可以创建一个保存按钮,并允许它将完整的jlist保存到.txt文件中.

I am looking for a way to save the users input from a jlist into a txt file. It's for a homework project. I have the program set up to take user input from a textfield, then it goes into the jlist once they hit the add button. The jlist has a defaultlistmodel on it, and I'm wondering if there is a way to create a save button and allow it to save the complete jlist into a .txt file.

另外,这是我的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JProgressBar;
import javax.swing.JLabel;


public class i extends JFrame {

private JPanel contentPane;
private JTextField jTextField;
DefaultListModel ListModel = new DefaultListModel();
ArrayList<String> arr = new ArrayList <String>();
String str;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                i frame = new i();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public i() {
    super("List");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    jTextField = new JTextField();
    jTextField.setBounds(15, 80, 168, 20);
    contentPane.add(jTextField);
    jTextField.setColumns(10);

    final JList jList = new JList(ListModel);
    jList.setBorder(new LineBorder(new Color(0, 0, 0)));
    jList.setBounds(289, 54, 135, 197);
    contentPane.add(jList);


    JButton jButton = new JButton("Add");
    jButton.setBounds(190, 79, 89, 23);
    contentPane.add(jButton);
    jButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            String str=jTextField.getText();
            ListModel.addElement(str);
            jTextField.setText("");
            arr.add(str);
            System.out.println(arr);
        }
    });



    JButton delete = new JButton("DELETE");
    delete.setBounds(190, 162, 89, 23);
    contentPane.add(delete);
    delete.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent k){
            ListModel.removeElement(jList.getSelectedValue());

        }
    });

    JButton btnUp = new JButton("UP");
    btnUp.setBounds(190, 125, 89, 23);
    contentPane.add(btnUp);
    btnUp.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            int index = jList.getSelectedIndex();
            if(index == -1){
                JOptionPane.showMessageDialog(null, "Select something to move.");
            } else if(index > 0) {
                String temp = (String)ListModel.remove(index);
                ListModel.add(index - 1, temp);
                jList.setSelectedIndex(index -1);
            }
        }
    });

    JButton btnDown = new JButton("DOWN");
    btnDown.setBounds(190, 196, 89, 23);
    contentPane.add(btnDown);
    btnDown.addActionListener( new ActionListener(){ 
        public void actionPerformed( ActionEvent e ){ 
            int index = jList.getSelectedIndex(); 
            if( index == -1 ) 
                JOptionPane.showMessageDialog(null, "Select something to move."); 
            else if( index < ListModel.size() - 1 ){ 
                String temp = (String)ListModel.remove( index ); 
                ListModel.add( index + 1, temp ); 
                jList.setSelectedIndex( index + 1 ); 
            } 
        } 
    }); 

    JLabel lblItems = new JLabel("Items:");
    lblItems.setBounds(290, 37, 46, 14);
    contentPane.add(lblItems);

    JButton btnPrint = new JButton("PRINT");
    btnPrint.setBounds(190, 228, 89, 23);
    contentPane.add(btnPrint);
    btnPrint.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){

        }
    });
}
}

推荐答案

您需要使用File类在其中保存列表

you need use File class to save your list in that

File file = new File("c:/test/myfile.txt");

现在已创建并打开文件! 并将一些输出写入文件

now file's created and opened! and for write some output to file

使用格式化程序类:

public class CreateFile
{
   private Formatter x;
    void openFile()
    {
    try
    {
        x = new Formatter("c:/myfile.txt");
        System.out.println("you created a file");
    }
    catch(Exception e)
    {
        System.err.println("error in opening file");
    }
    }
    void addFile()
    {
        x.format("%s %s %s\n","20","Start","Today");
    }
    void closeFile()
    {
        x.close();
    }
}

如您所见

,addFile方法接受3个传递的字符串以写入文件 我通过了"20",开始",今天" 您可以使用大于/小于%s的值,并传递用户输入或String列表来写入文件. 祝你好运

as you see,addFile method accept 3 String passed to write in the file i passed "20" , "Start" , "Today" you can use more/less than %s , and pass user input or a list in String to write in the file. good luck

这篇关于将jList数据保存到txt文件中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆