1

I'm new to Java GUI.

First I created a JFrame and have added a JPanel. After that I set it as a Null Layout. Then added a label and set a background image for the frame. For my project I need to add separate 4 JPanels for this JFrame. On those JPanels I'm going to add Labels and Text boxes. I want to know whether it's correct or not to add 4 JPanels on a main JPanel?

codehesh
  • 875
  • 10
  • 29
  • Using JPanels has nothing to do with NetBeans per se: it's all just "Swing". And there's nothing wrong with adding as many JPanels as you need to do the job. Q: Exactly what are you trying to do? Q: What, if anything, is going wrong? Please update your post: 1) Clarify exactly what you're trying to ask, 2) Post the code that shows the problem. – paulsm4 Oct 02 '15 at 06:04
  • 1
    *"After that I set it as a Null Layout."* Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 02 '15 at 06:06
  • *"I'm new to Java GUI. .. added a label and set a background image for the frame."* Since you're new to GUIs, I'd recommend working out how to create them in stages. Adding a background image is more of an advanced functionality and should not be attempted until about 'stage 8' .. – Andrew Thompson Oct 02 '15 at 06:09
  • 1
    There's the [Swing Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/) that has a lot of information. For some reason the people who need it the most (the people new to Java/Swing) seem to be the same people who read it the least. – Kayaman Oct 02 '15 at 06:11
  • @Kayaman *"(tutorials) For some reason the people who need it the most .. seem to be the same people who read it the least."* Aaah.. the 'teachers corollary' of Murphy's Law. ;) – Andrew Thompson Oct 02 '15 at 06:19

1 Answers1

3

I want to know whether it's correct or not to add 4 JPanels on a main JPanel?

Sure. Most of the Java GUIs you see that are any more than trivial put panels inside other panels.

Sometimes a different panel is used to group common controls or output components and possibly give them a titled border. More commonly different panels are used in order to use different layout managers in separate parts of the GUI. Here is a well known example that puts the details of the layout in a titled border for the panel..

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    @coderaizer: If using the NetBeans GUI editor, focus on the nested panel that needs it most, as suggested [here](http://stackoverflow.com/a/2561540/230513). – trashgod Oct 02 '15 at 08:31
  • Thank you very much Andrew & all who commented – codehesh Oct 16 '15 at 06:00