Java JList的问题

Java JList的问题,java,jlist,Java,Jlist,我们的老师不希望我们在GUI类内部做任何事情,所以我们所有的元素添加和删除都必须在它自己的类外部进行。我已经在JList中读到了向量以及元素的添加和删除,但正如我所说的,我们不允许这样做。我的问题是,当我需要用新列表填充列表或更新原始列表时,我将如何清除或刷新列表。我的问题是,每当我单击“打开”按钮时,列表都会自动复制,而我需要它做的是每次单击时都刷新它 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.e

我们的老师不希望我们在GUI类内部做任何事情,所以我们所有的元素添加和删除都必须在它自己的类外部进行。我已经在JList中读到了向量以及元素的添加和删除,但正如我所说的,我们不允许这样做。我的问题是,当我需要用新列表填充列表或更新原始列表时,我将如何清除或刷新列表。我的问题是,每当我单击“打开”按钮时,列表都会自动复制,而我需要它做的是每次单击时都刷新它

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.Collections;

import java.io.*;

/* Simple example of using the contents of a file to populate a JList
 * 
 * @author Jill Courte
 */

public class JListFromFile extends JFrame {

  private TextField wordA;
  private TextField wordB;
  private JButton openButton;
  private JButton newButton;
  private JButton addButton;
  private JButton deleteButton;
  private JButton saveButton;
  private TextField output;
  private JList listFromFile;
  private JPanel listPanel;
  private JPanel textPanel;
  private JPanel inputPanel;
  private JPanel buttonsPanel;
  private DataSource2 dataSource;
  private WordPair wordPair;
  public JListFromFile ()
  {
    // create the object to provide the data
    dataSource = new DataSource2();
    wordPair = new WordPair();
    // use a border layout for the main window
    getContentPane().setLayout(new BorderLayout(20, 20));

    buttonsPanel = new JPanel();
    openButton = new JButton("Open");
    newButton = new JButton("New");
    addButton = new JButton("Add");
    deleteButton = new JButton("Delete");
    saveButton = new JButton("Save");
    addButton.setEnabled( false ); 
    deleteButton.setEnabled( false );
    saveButton.setEnabled( false );
    buttonsPanel.add(openButton);
    buttonsPanel.add(newButton);
    buttonsPanel.add(addButton);
    buttonsPanel.add(deleteButton);
    buttonsPanel.add(saveButton);

    //add the button listeners
    openButton.addActionListener(new OpenButtonListener());
    addButton.addActionListener(new AddButtonListener());

    add(buttonsPanel, BorderLayout.NORTH);

    // create the panel to hold the list
    listPanel = new JPanel();

    // create your JList
    listFromFile = new JList();
    listFromFile.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listFromFile.setSelectedIndex(0);

    //add the list selection listener
   listFromFile.addListSelectionListener(new WordSelection());

    //add the translation text box 
    textPanel = new JPanel();
    output = new TextField();
    output.setEditable(false);
    output.setPreferredSize(new Dimension(200, 25));
    textPanel.add(output);

    // add scroll bars to list 
    JScrollPane listScrollPane = new JScrollPane(listFromFile);
    listScrollPane.setPreferredSize(new Dimension(150, 200));

    //add user input textfield 
    wordA = new TextField ("Word to Add");
    wordB = new TextField ("Translated word");
    wordA.setPreferredSize(new Dimension(200, 25));
    wordB.setPreferredSize(new Dimension(200, 25));
    wordA.setEnabled(false);
    wordB.setEnabled(false);

    //create panel to hold input textfield
    inputPanel = new JPanel();
    inputPanel.add(wordA);
    inputPanel.add(wordB);

    //add the list (via the scroll pane) to the panel
    listPanel.add(listScrollPane);

    //add the panels to the frame
    add(listPanel, BorderLayout.WEST);
    add(textPanel, BorderLayout.CENTER);
    add(inputPanel, BorderLayout.SOUTH);
    pack();
  }


  private File findFile ()
  { 
    JFileChooser chooser = new JFileChooser();

    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    // Start in current directory
    chooser.setCurrentDirectory(new File("."));
    int status = chooser.showOpenDialog(null);      

    if (status != JFileChooser.APPROVE_OPTION)
    {
      JOptionPane.showMessageDialog(null, "No File Selected");
      throw new NoFileChoosenException();
    }
    else
    {    
      File file = chooser.getSelectedFile();

      System.out.println(file.getName());      
      System.out.println(file.getPath());


      return file;
    }
  }


  private class OpenButtonListener implements ActionListener
  {
    public void actionPerformed (ActionEvent event)
    {

      File file = null;

      try
      {
        file = findFile();

        //tell the data source what file to use
        dataSource.readDataFromFile(file);

        //JList needs an array of strings, retrieve it from the data source
        String [] data = dataSource.getData();

        data = dataSource.insertion(data);
        data = wordPair.remove(data);
        listFromFile.setListData(data);
      }
      catch (Exception e)
      { 
      }
      addButton.setEnabled(true); 
      deleteButton.setEnabled(true);
      saveButton.setEnabled(true);
      wordA.setEnabled(false);
      wordB.setEnabled(false);
    }
  }

  private class AddButtonListener implements ActionListener
  {
    public void actionPerformed (ActionEvent event)
    {
      wordA.setEnabled(true);
      wordB.setEnabled(true);

    }
  }

  private class WordSelection implements ListSelectionListener
     {
      public void valueChanged (ListSelectionEvent event)
      {
          int index = listFromFile.getSelectedIndex();
          if (index >= 0)
          {
           String s = wordPair.getWord(2, index);
           output.setText(s);
          }
      }
     }

  public static void main(String[] args)
  {
    JListFromFile jListFromFile = new JListFromFile();
    jListFromFile.setVisible(true);
  }
}

对我来说,什么是允许做的,什么是不允许做的,听起来很模糊。考虑询问你的老师对此的说明。你正在使用<代码> SETListDATAs/COD>,它每次调用它时都会创建一个新的<代码> ListMead ,因此,问题不在<代码> jList或你使用它的方式,而是(最有可能的)与<代码> DATSUCCE2……通常你会调用RePrimeTo()。内容更改后视觉组件的方法。但我可能误解了你的意思吗need@GermannArlington:当更改涉及替换列表模型或向新列表模型添加数据时,这一点都不正确。当发生更改时,模型应该在内部调用fireXXX方法,从GUI向侦听器通知更改,因此组件会自动更新。在JLabel或JTextComponent上调用
setText(…)
时也会发生同样的情况。请注意,您也不需要为它们调用repaint。@气垫船上满是鳗鱼,所以我应该在GUI之外调用“clear”方法来获得我想要的结果。我认为您理解我的要求……因为我们不应该刷新GUI类内部的jList。