JScrollPane problem

允我心安 提交于 2019-12-11 03:54:46

问题


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:


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)



来源:https://stackoverflow.com/questions/6037156/jscrollpane-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!