2

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.

Deepak
  • 6,684
  • 18
  • 69
  • 121
  • can you show use some relevant code? – MByD May 17 '11 at 21:12
  • 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 May 17 '11 at 21:16
  • For better help sooner, post an [SSCCE](http://pscode.org/sscce.html). BTW - by 'JScrollBar pane' did you mean `JScrollPane`? An SSCCE is good at answering questions like that. – Andrew Thompson May 17 '11 at 21:18
  • @Andrew Thompson: its jscrollpane i am sorry abt that.. – Deepak May 17 '11 at 21:20
  • 2
    @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 May 17 '11 at 21:40
  • BTW - short of *you* posting an SSCCE that *I* am prepared to look at, you might get some tips from this [example that adds labels dynamically](http://stackoverflow.com/questions/5621338/about-swing-and-jtable/5630271#5630271). It should work just as well for adding buttons. – Andrew Thompson May 17 '11 at 21:53
  • 2
    @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 May 18 '11 at 03:20
  • @Andrew Thompson: I got what i need before i post SSCCE which you asked me, thats y i didnt post it. And about the edits i was doing some edit at the same time you have done some edit it seems. so my edit has overwritten you'r. I am sorry for that if you are hurt!! anyways thanks for the example you have posted. My problem was i didnt use setPreferredSize() to set size.. – Deepak May 22 '11 at 13:36

1 Answers1

3

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)

Raze
  • 2,175
  • 14
  • 30