-1

I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google. Here is my code:

JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);

The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!

  • null layout is __wrong__ - study how to use layoutManagers and apply what you learned - if still stuck come back with a [mcve] demonstrating what's wrong (using layout managers, of course :) – kleopatra Aug 23 '22 at 22:31
  • Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) section. – Gilbert Le Blanc Aug 24 '22 at 08:31
  • Thanks guys, I was learning how to use layout and I should be fine now to give it a try – scaresneeze Aug 24 '22 at 23:12

1 Answers1

-1
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);

JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);

listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);
ePortfel
  • 119
  • 6
  • __do not null the layoutManager__ – kleopatra Aug 24 '22 at 21:48
  • Thanks for your answer ePortfel, but it didn't seem to work on my laptop, I still can't see the vertical scrollbar for weird reasons. – scaresneeze Aug 24 '22 at 23:12
  • @kleopatra you're right but where do you see it nulled? i don't – ePortfel Aug 25 '22 at 10:01
  • @scaresneeze it is present, you might try to add much more songs to lmodel and resize window so you can see it more obviously – ePortfel Aug 25 '22 at 10:02
  • ehh ... nowhere, you are right, sry, missed that - but why call setBounds if there is one? They will not have any effect except cluttering the code :) – kleopatra Aug 25 '22 at 10:03
  • right it is my copy-paste laziness – ePortfel Aug 25 '22 at 10:08
  • you can edit the answer to fix the code :) And you might reconsider your comment _you might try to add much more songs_ - with policy ALWAYS it will show, well, always, independent on how much content the list has. The reason it appears to not be showing is the broken layout combined with setting prefSize to a fixed value (here the x is very small .. plus you shouldn't anyway) – kleopatra Aug 25 '22 at 10:14
  • also, the setup of the component hierarchy looks wrong: typically, the _list_ is wrapped into a ScrollPane - here that's done for its parent. – kleopatra Aug 25 '22 at 10:21
  • Thanks so much everyone, I added more songs into the list and changed the code based on kleopatra's idea, and now the code runs! – scaresneeze Aug 25 '22 at 14:27