Hiển thị file text lên jList

em có làm phần mềm từ điển,em muốn đưa tất cả nghĩa lên 1 list(em dùng jlist),em đã thử dùng listModel nhưng không được

private void addtoList() {
    for (int i = 0; i < dm.getSize(); i++) {
        listModel.addElement(dic.newWord.get(i));
    }
    jList1.setModel(listModel);
}

em mò mãi mà vẫn chưa ra, mọi người giúp đỡ em với ạ, em cảm ơn nhiều
đây code đồ họa em

import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JTextArea;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JOptionPane;
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);

    DefaultListModel listModel = new DefaultListModel();

    /**
     * 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);
        jList1.setFont(font);

    }

    /**
     * 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();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = 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);

        jScrollPane2.setViewportView(jList1);

        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(jScrollPane2))
                .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(javax.swing.GroupLayout.DEFAULT_SIZE, 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))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        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 addtoList() {
        for (int i = 0; i < dm.getSize(); i++) {
            listModel.addElement(dic.newWord.get(i));
        }
       jList1.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> jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration                   

    /**
     *
     */
}

Ở đây bạn đã gọi void addtoList() đâu mà nó hiển thị được =_=

3 Likes

À không phải. Đó hàm dịch ạ. Em muốn làm hiện nguyên text vào cái jlist trước luôn ấy ạ

Hiện trước là sao @@ Hiện lúc bắt đầu mở App á ?? Nhưng bạn muốn hiện thì cũng phải gọi void đó lên thì nó mới hiện chứ @@

1 Like

Em muốn nó hiện trước ghi ấn button thì phải làm như nào ạ

vâng đúng ạ,em muốn gọi khi mở lên luôn ấy, mà gọi void đó như nào ạ hicc@@

Thì bạn gọi nó ở trong Constructor của GUI thôi. Như việc bạn setFont hay insert ý

1 Like

vâng em cảm ơn ạ, hic ,em là newbie nên còn nhiều cái chưa biết

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?