2

I'm trying to insert a new panel into another panel in runtime everytime I press a button. My problem is the original panel runs out of space and I can't see the new panels I'm adding.

What I've tried so far:

  • Using scrollpane for vertical scrolling with no success.
  • Using flowlayout-no luck. Tried disabling horizontal scrolling-keep pushing the new panel to the right (can't get to it because there is no scrolling).
  • Tried using borderlayout-no luck.

testpanel t = new testpanel();
t.setVisible(true);
this.jPanel15.add(t);   
this.jPanel15.validate();
this.jPanel15.repaint();

This code suppose to insert the t panel into jpanel15. With flowlayout it pushes the t panel downwards just like I want it to but with no vertical scroll.

PS: I'm using netbeans in order to create my GUI.

TT.
  • 15,774
  • 6
  • 47
  • 88
user1864229
  • 53
  • 2
  • 4
  • 11

2 Answers2

1

My problem is the original panel runs out of space and I cant see the new panels i'm adding. Tried using scrollpane for vertical scrolling with no success.

A FlowLayout adds components horizontally, not vertically so you will never see vertical scrollbars. Instead you can try the Wrap Layout.

The basic code to create the scrollpane would be:

JPanel main = new JPanel( new WrapLayout() );
JScrollPane scrollPane = new JScrollPane( main );
frame.add(scrollPane);

Then when you dynamically add components to the main panel you would do:

main.add(...);
main.revalidate();
main.repaint(); // sometimes needed
camickr
  • 321,443
  • 19
  • 166
  • 288
0
  1. Use JScrollPane instead of the (outer) JPanel
  2. Or have a BorderLayout for the JPanel, put in a JScrollPane at BorderLayout.CENTER as the only control. The JScrollPane takes a regular JPanel as view.

In any case you will then add the control to the JScrollPane. Suppose your JScrollPane variable is spn, your control to add is ctrl:

// Creation of the JScrollPane: Make the view a panel, having a BoxLayout manager for the Y-axis
JPanel view = new JPanel( );
view.setLayout( new BoxLayout( view, BoxLayout.Y_AXIS ) );
JScrollPane spn = new JScrollPane( view );

// The component you wish to add to the JScrollPane
Component ctrl = ...;

// Set the alignment (there's also RIGHT_ALIGNMENT and CENTER_ALIGNMENT)
ctrl.setAlignmentX( Component.LEFT_ALIGNMENT );

// Adding the component to the JScrollPane
JPanel pnl = (JPanel) spn.getViewport( ).getView( );
pnl.add( ctrl );
pnl.revalidate( );
pnl.repaint( );
spn.revalidate( );
TT.
  • 15,774
  • 6
  • 47
  • 88
  • Note that in case 1/ the view for the JScrollPane is also a JPanel. You will have to set the alignment for added controls yourself, eg ctrl.setAlignmentX( Component.LEFT_ALIGNMENT ); – TT. Jun 03 '13 at 11:34
  • 'JPanel pnl = (JPanel) spn.getViewport( ).getView( );' return null.it doesnt return a panel.I added JScrollPane to my main panel and copied the rest of the code above but get null pointer exception. – user1864229 Jun 03 '13 at 13:11
  • You will have to create the JScrollPane as `JScrollPane spn = new JScrollPane( new JPanel( ) )`. That's what I mean by setting the view as a JPanel (the parameter in the constructor is called view for a reason). You can set the scroll-pane as your content pane in case your container derives from RootPaneContainer (eg JFrame or JDialog). – TT. Jun 03 '13 at 13:30
  • after done what you suggested,I can add the panel into the ScrollPane,but every new panel that i add,appears to the right of the previous panel and not below it. – user1864229 Jun 03 '13 at 15:21
  • Do this first before setting the view: `JPanel view = new JPanel( ); view.setLayout( new BoxLayout( view, BoxLayout.Y_AXIS ) ); JScrollPane spn = new JScrollPane( view );`. Then for the panels you add, make sure you set the alignment as I outlined in my previous comment. Then add the panels to the view as I outlined in my original post. HTH. If it works, please make sure to upvote this answer... I want to get credit if it does :) – TT. Jun 03 '13 at 19:20