Search code examples
javauser-interfacejlabelnon-static

How do I reference the "display" variable from my inner class, to use in the outer class in my JLabel


I want to enter text into my fourth JPanel, and every time I hit enter to update the first JPanel with the text. I added a keylistener for the return key, it runs a function in the inner class AddSong, that function takes in an arraylist of text, and returns a string. I want to display that string on the JPanel in the first quadrent, but it doesn't work. Right now I believe it is removing the first JPanel, and not updating. I am very confused because this is my first encounter with key events as I am new to programming.

Here's the code:

public class MusicPlayer extends JFrame{

  private static final int WIDTH = 400;
  private static final int HEIGHT = 400;
  private JLabel songList,songPlaying,c;
  private JTextField enterSong;
  private AddSong input;

  public MusicPlayer(){

     //Text box enterSong, input passed to actionListener
     input = new AddSong();
     enterSong = new JTextField(10);
     enterSong.addActionListener(input);

     //Numbers the quadrents
     songList = new JLabel(input.printSongs(input.songList),SwingConstants.CENTER);
     songPlaying = new JLabel("2",SwingConstants.CENTER);
     c = new JLabel("c",SwingConstants.CENTER);

     //Makes and sets size of pane
     Container pane = getContentPane();
     pane.setLayout(new GridLayout(2,2));

     //Add JLabels to panes in the container
     pane.add(songList);
     pane.add(songPlaying);
     pane.add(c);
     pane.add(enterSong);




     //KeyListener - return
     addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent ke){}
        @Override
        public void keyReleased(KeyEvent ke){}
        @Override
        public void keyTyped(KeyEvent ke){
           if(ke.getKeyCode()==KeyEvent.VK_ENTER){
              input.printSongs(input.songList);
           }
        }
     });
     add(songList);
     pack();

     setTitle("Andrew's Music Player");
     setSize(WIDTH,HEIGHT);
     setVisible(true);
     setDefaultCloseOperation(EXIT_ON_CLOSE);

  }

  public class AddSong implements ActionListener{

     //List of Songs
     ArrayList<String> songList = new ArrayList<String>();
     String display;

     public AddSong(){
        display = "";
     }
     public void actionPerformed(ActionEvent e){

        String song;
        //get the text
        song = enterSong.getText();

        //add current text string to list
        songList.add(song);

        //print songlist
        System.out.print(printSongs(songList));

     }

     public String printSongs(ArrayList<String> songList){
        display = "";
        for(int i = 0; i < songList.size(); i++){
           display += songList.get(i) + "\n";
        }
        return display;
     }
  }

  public static void main(String args[]){
     MusicPlayer boi = new MusicPlayer();
  }

}


Solution

  • Welcome to SO. Having a action listener, you can drop the key listener.
    Please note the comments :

    public class MusicPlayer extends JFrame{
    
        private static final int WIDTH = 400;
        private static final int HEIGHT = 400;
        private JLabel songsList,songPlaying,c;
        private JTextField enterSong;
        private AddSong input;
    
        public MusicPlayer(){
    
            //Text box enterSong, input passed to actionListener
            input = new AddSong();
            enterSong = new JTextField(10);
            enterSong.addActionListener(input);
    
            //Numbers the quadrents
            songsList = new JLabel("No songs in list",SwingConstants.CENTER);
            songPlaying = new JLabel("2",SwingConstants.CENTER);
            c = new JLabel("c",SwingConstants.CENTER);
    
            //Makes and sets size of pane
            Container pane = getContentPane();
            pane.setLayout(new GridLayout(2,2));
    
            //Add JLabels to panes in the container
            pane.add(songsList);
            pane.add(songPlaying);
            pane.add(c);
            pane.add(enterSong);
    
            //adding a key listenr to the Jframe is not need
            //the  enterSong.addActionListener(input); does the work
    
            add(songsList);
            pack();
    
            setTitle("Andrew's Music Player");
            setSize(WIDTH,HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        }
    
        public class AddSong implements ActionListener{
    
            //List of Songs
            ArrayList<String> songList = new ArrayList<>();
    
            public AddSong(){ }
    
            @Override
            public void actionPerformed(ActionEvent e){
    
                String song;
                //get the text
                song = enterSong.getText();
    
                //add current text string to list
                songList.add(song);
    
                String songListAsString = printSongs(songList);
    
                //update display with new song
                songsList.setText(songListAsString);
    
                //clear text field
                enterSong.setText("");
            }
    
            public String printSongs(ArrayList<String> songList){
    
                //using string builder is more efficient
                //to have multiple lines in a JLabel you have to use
                //html tags
                //a JList would be more appropriiate
                StringBuilder sb = new StringBuilder("<html>");
    
                for(int i = 0; i < songList.size(); i++){
                    sb.append("<p>")
                      .append(songList.get(i))
                      .append("</p>");
                }
    
                sb.append("</html>");
                return sb.toString();
            }
        }
    
        public static void main(String args[]){
            MusicPlayer boi = new MusicPlayer();
        }
    }