0

I have two JFrames.

AddSongFrame takes in the user input through jTextFields and MusicPlayerandLibraryForm updates the jList with the user input.

However, I have run into a problem with adding elements to the jList. At the line

MusicPlayerAndLibraryForm mplf = new MusicPlayerAndLibraryForm();

it seems that the JFrame is not updating the jList. It clears the jList and then adds the songName to the jList.

How can I access the JFrame in a way where it doesn't clear the jList when the user accesses another 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);
    }
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Mana
  • 49
  • 6
  • 1
    I'm not sure what the behavior you're having is, but a [mcve] might be better than the current code as we don't have access to `Song` code nor `MusicPlayerAndLibraryForm`. Please go through the [help], take the [tour] and read [ask] a good question, so your question can receive more and better answers. – Frakcool Mar 15 '18 at 17:52
  • 1
    Also read [The use of multiple JFrames, good / bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (The general consensus is: it's bad). better to use `Dialog`s or `CardLayout` and build your GUI towards the use of `JPanel`s instead of `JFrame`s. As you're not changing the behavior of the `JFrame` and so, there's no need to `extend` it. See [extends JFrame or create it inside of class](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program) – Frakcool Mar 15 '18 at 17:53
  • 1
    The reason for the list being deleted on new `JFrame` click is that it's creating a new `List` on every click, but it's hard to tell without being able to reproduce the problem, and thus a MCVE might solve many of our doubts and point to the root of the problem and probably some minor other ones you might have – Frakcool Mar 15 '18 at 17:55

2 Answers2

1

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.

You need to create an instance of this class only once, then use that instance to update the JList, however this line:

MusicPlayerAndLibraryForm.getjListMusicLibrary()

Tells me that getjListMusicLibrary() is a static method and shows an problem of design in your program.

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. As I said, also avoid extending JFrame and create an instance of it inside your class.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
-3

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...

Hope this Helps.....

anon
  • 7