Java 如何将文件内容放入JList
如何将examplefile.dat的内容放入Jlist examplefile.dat 埃文:乔:兰斯:史蒂夫 以冒号分隔的名称将转换为数组,并将其放入JList或JComboBox中 这是我加载和打印文件的代码Java 如何将文件内容放入JList,java,swing,io,java.util.scanner,jlist,Java,Swing,Io,Java.util.scanner,Jlist,如何将examplefile.dat的内容放入Jlist examplefile.dat 埃文:乔:兰斯:史蒂夫 以冒号分隔的名称将转换为数组,并将其放入JList或JComboBox中 这是我加载和打印文件的代码 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.Scanner; import java.util.logging.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class FileScanner {
/**
* @param args the command line arguments
* @throws FileNotFoundException
* @throws IOException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
JPanel panel = new JPanel();
JFrame frame = new JFrame();
final JTextField input0 = new javax.swing.JTextField(20);
JButton load = new javax.swing.JButton("Load");
frame.add(panel);
frame.setSize(240,200);
panel.add(input0);
panel.add(load);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
load.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FileReader fin = null;
String filename = input0.getText();
try {
fin = new FileReader(filename);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileScanner.class.getName()).log(Level.SEVERE, null, ex);
}
Scanner src = new Scanner(fin);
src.useDelimiter(":");
while (src.hasNext()) {
String lol = src.nextLine();
System.out.println(lol);
}
}
});
}
}
好的,这是我的代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Jfetizanan
*/
public class IOtestForm extends javax.swing.JFrame {
/**
* Creates new form IOtest
*/
public IOtestForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
list = new javax.swing.JList();
input0 = new javax.swing.JTextField();
load = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(list);
DefaultListSelectionModel m = new DefaultListSelectionModel();
m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
m.setLeadAnchorNotificationEnabled(false);
list.setSelectionModel(m);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int selected[] = list.getSelectedIndices();
for (int i = 0; i < selected.length; i++) {
String element = (String) list.getModel().getElementAt(selected[i]);
input0.setText(element);
}}});
load.setText("Load");
load.addActionListener(new ActionListener() {
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
FileReader fin = null;
try {
fin = new FileReader("data.dat");
} catch (FileNotFoundException ex) {
Logger.getLogger(IOtestForm.class.getName()).log(Level.SEVERE, null, ex);
}
Scanner src = new Scanner(fin);
src.useDelimiter("\n");
DefaultListModel model = new DefaultListModel();
while (src.hasNext()) {
String lol = src.nextLine();
model.addElement(lol);
}
list.setModel(model);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addComponent(input0)
.addComponent(load, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addComponent(load)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(input0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(94, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(IOtestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new IOtestForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField input0;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList list;
private javax.swing.JButton load;
// End of variables declaration
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.util.Scanner;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.DefaultListModel;
导入javax.swing.DefaultListSelectionModel;
导入javax.swing.ListSelectionModel;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
/**
*
*@作者Jfetizanan
*/
公共类IOtestForm扩展了javax.swing.JFrame{
/**
*创建新表单IOtest
*/
公共服务电子表格(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jScrollPane1=newjavax.swing.JScrollPane();
list=newjavax.swing.JList();
input0=newjavax.swing.JTextField();
load=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(列表);
DefaultListSelectionModel m=新的DefaultListSelectionModel();
m、 设置选择模式(ListSelectionModel.单选);
m、 setLeadAnchorNotificationEnabled(假);
列表.设置选择模型(m);
addListSelectionListener(新的ListSelectionListener(){
@凌驾
public void值已更改(ListSelectionEvent e){
int selected[]=list.getSelectedIndices();
对于(int i=0;iJList list = new JList(); // You need to create the JList first
JScrollPane scrollPane = new JScrollPane(list);
// Add the scrollpane to the UI
...
// At some time in the future, load the results...
DefaultListModel model = new DefaultListModel();
while (src.hasNext()) {
String lol = src.nextLine();
model.addElement(lol);
}
list.setModel(model);