У меня вопрос, как я могу импортировать существующую строку массива в 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);
}
Вот мой код только из графического интерфейса
[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
/**
*
*/
}