2
votes

i have a jscrollpane added to jtabbedpane and inside the jscrollbar pane i have a jpanel. In the jpanel i have few buttons which i am creating on runtime. My idea is it get a scroll bar when the button size that i am adding dynamically during runtime grows. I cant get that to happen. The button size is increased and it hides when the size goes beyond the jpanel's view. I am increasing the size of jpanel and the buttons inside it using setSize(). I have also used jScrollPane2.setViewportView(Jpanel1); to set the viewportview.

Dimension t_size = table_button.getSize(); 
table_button.setSize((int)t_size.getWidth(), (int) (t_size.getHeight() + 150));
       Rectangle bounds = global_variables.bottom_panel.getBounds();
       int y_new = (int) (bounds.getY() + 150);
       int x_new = (int) bounds.getX();
       bounds.setLocation(x_new, y_new);
       global_variables.bottom_panel.setBounds(bounds);
floor_plan_admin_single.table_base.setSize((int)floor_plan_admin_single.table_base.getWidth(), (int)floor_plan_admin_single.table_base.getHeight()+150);

Here table_button is the button i am adding dynamically and global_variables.bottom_panel is the panel which stays below table_button and as i increase the height of table_button i am moving the bottom_panel down. floor_plan_admin_single.table_base is the jpanel added to scrollbar. even if i change the height of tat table_base panel i cant see the scrollbar in action.

1
can you show use some relevant code? - MByD
that something wrong with LayoutManager, and PreferredSize for JPanel, (MiGLayout, isn't it) for any better hepl is more than required to Edit your post with your code that shows what you are... - mKorbel
For better help sooner, post an SSCCE. BTW - by 'JScrollBar pane' did you mean JScrollPane? An SSCCE is good at answering questions like that. - Andrew Thompson
@Deepak: If you are going to be sorry about anything, be sorry about: 1) Ignoring my advice to post an SSCCE rather than code snippets. 2) Wiping out the capitals corrections and code formatting that I made in an edit to your question. 3) Using nonsense words like 'abt'. - Andrew Thompson
@Deepak, I woule like to strongly emphasize @Andrew's point. A compilable code, containing main() and not more than 15 lines of code would have been very useful to convey the question effectively. I knew exactly what problem you were facing because you mentioned setSize() and setViewport(), but an SSCCE would have probably given you an answer quicker. - Raze

1 Answers

3
votes

There are a few things to note when using a JScrollPane. The easiest way to initialize a JScrollPanel is by passing the panel you want to make scrollable in the constructor.

JScrollPane scrollPane = new JScrollPane(panel1);

Then, to set the sizes, you have to use setPreferredSize, not setSize().

button.setPreferredSize(new Dimension(120, 120));

Do not set the size on the panel given inside the constructor of JScrollPane. (ie panel1)