Ján Srniček
Ján Srniček

Reputation: 525

JPanel does not show added components

i would like to do a dynamic song list represented as JPanel,with song items also represented as JPanel's,added to parent song list JPanel

Here's my code / Constructor of MainContext class

public MainContext() {
        initComponents();
        PanelItem item=new PanelItem();
        songListPanel.add(item);
        item.setEnabled(true);
        item.setVisible(true);
        item.setSongLabel("bla bla");
        songListPanel.revalidate();
    }

This is what i get

enter image description here

What i want is that in that left panel will be shown multiple PanelItem's

hope you can help

Upvotes: 1

Views: 1335

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I'm gong to guess (because that is all we can do currently) that your problem is with your container JPanel, the songListPanel, that you've given it a GroupLayout or other layout that does not easily accept new components like you're trying to give it.

But having said that, I think that a much better solution for you is to use a JList on the left side of your GUI, since it seems built for just what you're trying to do.

Please have a look at the JList Tutorial for more on this.

As an example of what I'm talking about...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class JListExample extends JPanel {
   private static final String PROTOTYPE_SONG = "ABCDEFGHIJKLMNOPQRS";
   private DefaultListModel<String> songListModel = new DefaultListModel<>();
   private JList<String> songList = new JList<>(songListModel);
   private JTextField songField = new JTextField(20);

   public JListExample() {
      songList.setPrototypeCellValue(PROTOTYPE_SONG);

      JPanel southPanel = new JPanel();
      southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.LINE_AXIS));
      southPanel.add(new JLabel("Song:"));
      southPanel.add(songField);
      AddSongAction songAction = new AddSongAction("Add Song");
      southPanel.add(new JButton(songAction));
      songField.setAction(songAction);

      setLayout(new BorderLayout());
      add(new JScrollPane(songList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
            BorderLayout.LINE_START);
      add(Box.createRigidArea(new Dimension(400, 400)));
      add(southPanel, BorderLayout.PAGE_END);
   }

   private class AddSongAction extends AbstractAction {
      public AddSongAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         songListModel.addElement(songField.getText());
      }
   }

   private static void createAndShowGui() {
      JListExample mainPanel = new JListExample();

      JFrame frame = new JFrame("JListExample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Upvotes: 5

Related Questions