-1

Hi guys I have been trying to make my JPanel scrollable but no luck.

The JPanel contains other JPanels which is then added to a JScrollPane but the JScrollPane is not displaying anything.

This is what I have tried

int size=rs.getRow();
EnrollNewStudent enroll=new EnrollNewStudent();
JPanel resultPanel=new JPanel();
resultPanel.setLayout(null);
resultPanel.setOpaque(false);
int y=10;
int pace=(int)Math.floor(80/size);
for(int i=1;i<=size;i++){
   JPanel panel=enroll.initComponents(rs,i);
   panel.setBounds(0,y,1090,550);
   resultPanel.add(panel);
   y+=555;
   searchProgressBar.setValue(searchProgressBar.getValue()+pace);
}
resultPanel.setBounds(0,150,1090,y);
searchProgressBar.setValue(90);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int xlocation=dim.width/2-545;
int ylocation=dim.height/2-277;
this.setBounds(xlocation,ylocation,1090,553);
holderPanel.remove(searchProgressBar);
search.setBounds(420, 80, 100, 30);
holderPanel.setBounds(2,30,1086,521);
closeButton.setBounds(1070, 10, 26, 14);
resultScrollPane=new JScrollPane();
resultScrollPane.setBounds(0,150,1090,400);
//resultPanel.setBounds(0,150,1090,400);
resultScrollPane.setViewportView(resultPanel);
resultScrollPane.setOpaque(false);
holderPanel.add(resultScrollPane);
repaint();

Your assistance is much appreciated

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Algorithm
  • 149
  • 1
  • 8
  • 2
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). **That is most likely the cause of the current problem.** 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jan 28 '15 at 12:33
  • the problem i am having is that the JPanel is not even displaying not let alone the scrollpane scrolling – Algorithm Jan 28 '15 at 13:39
  • Again.. a little louder this time. **For better help sooner, post an MCVE** – Andrew Thompson Jan 28 '15 at 13:48
  • Ooh right.. `holderPanel.add(resultScrollPane); repaint();` Adding components after the initial GUI is constructed? Instead use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jan 28 '15 at 13:49
  • when i add border to the scrollpane I can see the scrollpane but the scrollpane is not showing the resultPanel – Algorithm Jan 28 '15 at 13:54
  • If you want further help from me, post an MCVE. – Andrew Thompson Jan 28 '15 at 14:04

1 Answers1

1

You can define resultPanel.setPreferredSize() passing desired size of scroll pane content but actual solution would be using proper Layout Manager.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 1
    I think this answer should be trimmed down to the 'actual solution' part. As to the first part, that just seems to be compounding (or hiding, for the moment) the problem.. OP: See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 28 '15 at 12:35
  • @AndrewThompson I definitely agree with you that directn answer is not good. Of course setPreferredSize() is not perfect for real world application but... but I would rather prefer OP will understand it after some experience using pixel based GUI. Guess OP can read a lot about correct building swing GUI. – StanislavL Jan 28 '15 at 12:40
  • *"I would rather prefer OP will understand it after some experience using pixel based GUI."* I would rather prefer that we not have to answer the next four ('Why is my GUI broken?') questions that will result in. ;) – Andrew Thompson Jan 28 '15 at 12:49
  • @AndrewThompson actually there is no difference which way the OP will choose. The same questions will appear anyway. There are a lot of beginners who post questions before reading existing answers. – StanislavL Jan 28 '15 at 13:15