This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Oversized JPanel  RSS feed

 
Mark Gilliam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'm working in netbeans and when I add an object to my JPanel, the objects flow off the screen horizontally although I would like for them to move vertically and a scroll bar appear without having to resize the entire JPanel.

Any help with this would be greatly appreciated!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, welcome to the Ranch!

Use an appropriate Layout Manager. Also note that the NetBeans visual designer aka Matisse is definitely not a beginners' tool. It's nore suitable for use by those who already understand Swing coding.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The child component placement in a container is defined by the container's layout. The JPanel defaults to FlowLayout which places the children in a row. You will need to set an appropriate layout for your panel to have the children placed vertically.
More about layouts here http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html

PS. You can also try overriding the getPreferredSize() of your JPanel. When the aggregated width of the current children exceeds the panels preferred width, the layout will wrap the next child onto the next line like this

----------------------------
CompA CompB CompC
CompD
----------------------------
 
Mark Gilliam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome it works now! Thanks to you who helped me.

Now I am trying to make this clearObjectViewer(); display more than one object at a time. When I run this, I clear out what I have created and it creates only one.
How can I create more than one person? Can I use an Integer < or > something? Thanks in advance!

This is what I have...

 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear what you are getting at. Here's the code to create one person:

If you want to create more persons, you just execute that code more times. In a loop, or something. But that isn't going to help, I don't think, because I don't see anything in your code (or in your question) where you know how many persons you want to create.

Or were you perhaps asking about the GUI aspects of adding more components to the display? As I said, I don't see the point of the question.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!