• Post Reply Bookmark Topic Watch Topic
  • New Topic

SpringLayout Question - (code snipet inside)

 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question pertains to the code below. I'm creating several classes which load into a cardlayout on a main form. In order to create a border on the left side of this portion of the screen, I am nesting one JPanel within another and using the swinglayout to set my distance from the WEST side of the parent JPanel.

The problem I have is that the 2 components (textfield and label) do not appear in the resulting JFrame. However, if I comment out the line borderPanelLayout = new SwingLayout() then I do get the components to appear. I cannot, for the life of me, figure out why this is. Any and all help is appreciated.

Thanks,
David

 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using the 1.5 sdk? If not then you might have some confusion on the contentpane.

If you want to add things to a JFrame you need to do a myFrame.getContentPane() and then add everything to it. You should have gotten a run time error with the code you have under 1.4.

If you want an area on the side, why not use a borderlayout? You would do something similiar to this -



Would this work?

Regards,
Aaron R>
 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I wasn't clear. This class extends JPanel and implements serializable thus the reason I don't have any reference to .get or .set contentPane. So, yeah the code compiles just fine and if I comment out the springLayout line from the inner JPanel, then the panel will display just fine....albeit without the formatting I want.

Thanks,
David
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your code (with this change JLabel lblXXXXXNumber = new JLabel("XXXXX P/N");). Nothing showed up in the frame. Then I added this line

just before the call to add(contentPane); and the components seems to show up okay. When we extend JPanel we get the default FlowLayout manager with it. FlowLayout is respectful of the preferred size(s) of the components it lays out. If none is given then you get the JPanel default size of (10 x 10) or worse...

A trick is to set the layout to BorderLayout and add your component (here, contentPane) to the center and it will expand to fill the space.
 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. Worked like a charm. Now one further challenge......and this one is a bit of a mystery to me. You see in the above code where I'm offsetting the WEST edige of the labels against the location of the WEST side of the contentPane, right? Well I have to add this code for every component. My original intent was to have the offset added to the contentpane itself, however if I add setLayoutManger(new SpringLayout()); and then set the constraint against the inner panel, I get nothing in my display.

Is it not possible to offset a pane using SpringLayout?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be very possible. Learning to work with constraint–based layout managers (those that implement LayoutManager2) seems to take some patience and lots of practice. If you like SpringLayout and want to master it I'd say keep playing with it, try different things. Make up little test programs, start simple and experiment.

Here's another way to lay out your components:
 
David Mace
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that code and it worked well. Thanks for the help Craig. I'm fairly new to this particular board and, let me tell you, I'll never go back to the sun java forums again. The people here are very helpful and friendly and are always willing to bend over backwards to help out.

Thanks again!!!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!