Good day. Tell me, please, how to change the JList list depending on the JComboBox? I need to write a simple dictionary where a list of words in one language is given via JList, and you can choose a language through JComboBox and depending on which language you choose, you need to change this list to the selected one.

Approximate view:

Dictionary

FormClass.java

import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.Vector; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class FormClass extends JFrame { JTextField text; JButton button; JList list; JComboBox combo; JPanel panel; String[] eng, rus, temp; String line; int tempIndex, choice; public FormClass() throws FileNotFoundException{ setLayout(new BorderLayout()); panel = new JPanel(); panel.setLayout(new GridLayout(1, 2)); text = new JTextField(); button = new JButton("Search"); File file = new File("input.txt"); Scanner sc = new Scanner(file); temp = new String[(int) file.length()]; while(sc.hasNextLine()){ line = sc.nextLine(); temp = line.split("="); } rus = new String[temp.length]; eng = new String[temp.length]; for(int i = 0; i < temp.length; i++){ if(i % 2 != 0){ rus[i] = temp[i]; }else{ eng[i] = temp[i]; } } list = new JList(eng); list.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { // TODO Auto-generated method stub tempIndex = list.getSelectedIndex(); text.setText(rus[tempIndex + 1]); } }); combo = new JComboBox(); combo.addItem("Eng"); combo.addItem("Rus"); combo.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub } }); panel.add(text); panel.add(button); add(panel, BorderLayout.NORTH); add(list, BorderLayout.CENTER); add(combo, BorderLayout.SOUTH); } } 

MainClass.java

 import java.io.FileNotFoundException; import javax.swing.WindowConstants; public class MainClass { public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub FormClass form = new FormClass(); form.setSize(300, 300); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); form.setVisible(true); } } 

    1 answer 1

    Create a sheet and give it a DefaultListModel.

     DefaultListModel mod=new DefaultListModel(); JList list = new JList(mod); 

    Write code for a JComboBox listener. For example, such.

     @Override public void itemStateChanged(ItemEvent e) { mod.clear(); if(combo.getSelectedItem().equals("Rus")){ for(String g:rus){ System.out.println(g); mod.addElement(g); } } else System.out.println("eng"); } }); 

    It just seems to me that you read the wrong file.

     while(sc.hasNextLine()){ line = sc.nextLine(); temp = line.split("="); } 

    You have an array temp each loop step is initialized again. That is, at the end of the loop, the temp array will contain only the data from the last line.
    Ps Or you can create a sheet for each language and just change them.

    • You are right, you took out the temp for the cycle, now it records normally, thank you) - Equalizer