简体   繁体   English

一个如何从另一个JFrame更新并添加到JList?

[英]How can one update and add to a JList from another JFrame?

I have two JFrames . 我有两个JFrames

AddSongFrame takes in the user input through jTextFields and MusicPlayerandLibraryForm updates the jList with the user input. AddSongFrame通过jTextFields接收用户输入, MusicPlayerandLibraryForm使用用户输入更新jList

However, I have run into a problem with adding elements to the jList . 但是,在将元素添加到jList遇到了问题。 At the line 在生产线上

MusicPlayerAndLibraryForm mplf = new MusicPlayerAndLibraryForm();

it seems that the JFrame is not updating the jList . 看来JFrame没有更新jList It clears the jList and then adds the songName to the jList . 它将清除jList ,然后将songName添加到jList

How can I access the JFrame in a way where it doesn't clear the jList when the user accesses another jFrame ? 我怎样才能访问JFrame在某种程度上它不清除jList当用户访问另一个jFrame

public class AddSongFrame extends javax.swing.JFrame {

        ArrayList<Song> songs = new ArrayList<Song>();
        ArrayList<Song> songFileLibrary = new ArrayList<Song>();
        DefaultListModel dlm = new DefaultListModel();

        int currentIndex = 0;



        public AddSongFrame() {
            initComponents();
        }


        private void jButtonBrowseFilesActionPerformed(java.awt.event.ActionEvent evt) {                                                   
           JFileChooser fileBrowser = new JFileChooser();
           fileBrowser.showOpenDialog(null);
           File f = fileBrowser.getSelectedFile();
           String fileName = f.getAbsolutePath();
           jTextFieldFileName.setText(fileName);

        }                                                  

        private void jButtonAddSongActionPerformed(java.awt.event.ActionEvent evt) {                                               

            String fileName = jTextFieldFileName.getText();
            String songName = jTextFieldSongName.getText();
            String songGenre = jTextFieldSongGenre.getText();
            String songArtist = jTextFieldArtist.getText();

            Song song = new Song(songName, fileName, songGenre, songArtist);
            Song songFiles = new Song(fileName,songName, songGenre,songArtist);
            songs.add(song);
            songFileLibrary.add(songFiles);


            updatejListMusicLibrary();

        }                                              



        private void updatejListMusicLibrary()
        {

            MusicPlayerAndLibraryForm mplf = new MusicPlayerAndLibraryForm();
            MusicPlayerAndLibraryForm.getjListMusicLibrary().setModel(dlm);
            mplf.setDlmMain(dlm);
            this.setVisible(false);
            mplf.setVisible(true);
    }

Your problem is more than likely here: 您的问题很可能在这里:

MusicPlayerAndLibraryForm mplf = new MusicPlayerAndLibraryForm();

This is being called everytime you click your JButton and it create a new instance of MusicPlayerAndLibraryForm everytime. 每次单击JButton调用此方法,它每次都会创建MusicPlayerAndLibraryForm的新实例。

You need to create an instance of this class only once, then use that instance to update the JList , however this line: 您只需要创建一次此类的实例,然后使用该实例更新JList ,但是此行:

MusicPlayerAndLibraryForm.getjListMusicLibrary()

Tells me that getjListMusicLibrary() is a static method and shows an problem of design in your program. 告诉我getjListMusicLibrary()static方法,并显示程序中的设计问题。

However as I already said in the comments above, avoid the use of multiple JFrames and use Dialogs or a CardLayout for asking user for information and then use it to update the single instance of the JList in the JFrame . 但是,正如我在上面的评论中已经说过的那样,避免使用多个JFrame并使用DialogsCardLayout询问用户信息,然后使用它来更新JFrame JList的单个实例。 As I said, also avoid extending JFrame and create an instance of it inside your class. 就像我说的那样,还请避免扩展JFrame并在您的类中创建它的实例。

You can make the list static and then create a method in your JFrame class which calls the static list and adds to it, when the button or any other thing is pressed in the JFrame... 您可以将列表设为静态,然后在JFrame类中创建一个方法,当在JFrame中按下按钮或其他任何东西时,该方法将调用静态列表并将其添加到列表中。

Hope this Helps..... 希望这可以帮助.....

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

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