简体   繁体   English

如何将文件文本读取到JList

[英]How to read file text to JList

在此处输入图片说明

I have a question, how can I import existed array string to jlist for each element in array, I know a lot of you guy asked it before, but mostly, I saw that how to do it with a jButton , I also heard about DefaultListModel but it did't work 我有一个问题,如何将存在的数组字符串导入数组的每个元素的jlist中,我知道很多人以前都问过它,但是大多数情况下,我看到了如何使用jButton ,我也听说过DefaultListModel但是没有用

private void JListAction(java.awt.event.ActionEvent evt) {
        DefaultListModel listModel = new DefaultListModel();
        for (int i = 0; i < dm.getSize(); i++) {
            listModel.addElement(dic.newWord.get(i).getWordExplain());
        }
        jList2.setModel(listModel);
    }

Here is my code from GUI only 这只是我的GUI代码

[import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;
import javax.swing.JScrollPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Quốc Khánh
 */
public class GUI extends javax.swing.JFrame {

    DictionaryCommandline dcl = new DictionaryCommandline();
    DictionaryManagement dm = new DictionaryManagement();
    Dictionary dic = new Dictionary();
    DictionaryApplication dicA = new DictionaryApplication();
    private Object textArea;
    Font font = new Font("consolas", Font.BOLD, 18);

    /**
     * Creates new form GUI
     */
    public GUI() {
        setTitle("từ điển Anh Việt v1.0");
        dm.insertFromFile(dic);
        initComponents();
        jTextPane1.setFont(font);
        jTextArea1.setFont(font);
        jTextArea1.setLineWrap(true);
        jTextArea1.setForeground(Color.BLACK);
        jButton1.setFont(font);
        JScrollPane pane=new JScrollPane(jList2);

    }

    /**
     * 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();
        jTextPane1 = new javax.swing.JTextPane();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jTextPane1);

        jButton1.setText("translate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane3.setViewportView(jTextArea1);

        jScrollPane4.setViewportView(jList2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane3)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        jButton1.getAccessibleContext().setAccessibleName("translate");

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        String word = jTextPane1.getText();

        String mean = dicA.getWord(dic, dm.getSize(), dm, word);
        jTextArea1.setText(dicA.getWordEx(dic, dm.getSize(), dm, mean));


    }                                        
    private void JListAction(java.awt.event.ActionEvent evt) {
        DefaultListModel listModel = new DefaultListModel();
        for (int i = 0; i < dm.getSize(); i++) {
            listModel.addElement(dic.newWord.get(i).getWordExplain());
        }
        jList2.setModel(listModel);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String\[\] args) {
        java.awt.EventQueue.invokeLater(() -> {
            new GUI().setVisible(true);
        });
    }


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JList<String> jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration                   

    /**
     *
     */
}

Is your Array of Strings already available, when the GUI is generated? 生成GUI时,您的字符串数组是否已经可用? If so, you can insert it directly into your JList by using it as a parameter. 如果是这样,您可以将其用作参数直接将其插入到JList中。

Instead of using JList jList2 = new javax.swing.JList<>(); 而不是使用JList jList2 = new javax.swing.JList<>(); (as it is done in line 5 of your initComponent() method) you'd put your String[] into the brackets: (正如在initComponent()方法的第5行中所做的那样),您需要将String []放在方括号中:

String[] data = {"a","b","c"};
JList jList2 = new javax.swing.JList<>(data);
JScrollPane pane=new JScrollPane(jList2);

In this case your ScrollPane would look something like this 在这种情况下,您的ScrollPane看起来像这样

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

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