2012-08-14 60 views
0

我怎样才能把examplefile.dat的内容到的jList examplefile.dat我怎样才能把文件内容到一个JList

埃文:乔:矛:史蒂夫

的名字分开由冒号将被转换为阵列,并把它放在JList中或JComboBox中

这里是我的代码加载和打印文件

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 
} 

@MadProgrammer看看新的代码我张贴。它所做的是它加载该文件,并显示在一个JList,里面的一切,当我点击它..它显示一个文本行。但我希望Jlist只显示列表中的数字,以及当我点击它时。它显示这些字符在字符串的其余部分的其余部分在线路

这必须是data.dat文件

1洛尔

2洛尔

3洛尔

的内容

4洛尔

*不介意线之间的空间

回答

3
JList 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); 

也许你想带的

读一些更多有用的信息

+0

我在哪里可以把JList的? – JFetz2191 2012-08-14 03:30:23

+2

不是将按钮添加到框架,而是添加一个带有“BorderLayout”的面板。将'JList'按钮添加到'PAGE_START'和'JScrollPane'到'CENTER'中。一旦按钮被点击,文件被解析,为列表设置一个新的模型。 – 2012-08-14 03:33:14

+2

你想要的任何地方,它就是你的屏幕。就个人而言,我要么改变布局管理器,并稍微更好地布局组件 – MadProgrammer 2012-08-14 03:33:25

相关问题