0

I am somewhat new to the whole java swing scene, and I just want some clarifications due to a slight confusion I have. I have learned about orientation and buttons and all the basics. Also pointing to some good (non oracle) tutorials will be highly appreciated.

As far as I understand, we have our JFrame which is a window.
Then our JFrame consists of ContentPane, which I am using a container for.
Container content = frame.getContentPane();

Now that I have this container, can I add more containers within those containers? Let's say that I would like to have different parts that do different things, and for that I would like to create classes and such that each handle their own containers?

So what I am asking for is, how does one go about storing different content within the container? What is the proper way to go about it?

An example I would give is let's say I have a scoreboard (for soccer) that is on the top part of the window, on the middle part of the window there is some work related business stuff, and on the bottom part of the window I have some textbox that does its thing with a few buttons.

Sorry if this question is stupid, I am just trying to learning swing, and want to know proper way to arrange different components within the window.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Quillion
  • 6,346
  • 11
  • 60
  • 97
  • 2
    JPanel extends JComponont and JComponent extends Container, so you definitively can add containers ;) – Guillaume Jun 13 '13 at 16:29
  • See the [Nested Layout Example](http://stackoverflow.com/a/5630271/418556) for more ideas about how to combine layouts to create the required layout. – Andrew Thompson Jun 13 '13 at 19:19

1 Answers1

2

Yes, you can. Create an instance of JPanel and add your components to it, and then add them to the frame's content pane using a string:

JPanel panel = new JPanel();
//code to add stuff to the panel
frame.getContentPane().add("Center", panel);   //"North", "South", "East", "West", or "Center"
gparyani
  • 1,958
  • 3
  • 26
  • 39
  • 2
    IMO it's better to replace "Center" by the constant BorderLayout.CENTER – Guillaume Jun 13 '13 at 16:31
  • @Guillaume I'm looking up constant field values and `"Center"` is the value for `BorderLayout.CENTER`, and the literal is easier to type. – gparyani Jun 13 '13 at 16:34
  • Oh so then we add JPanel to the Container? So for each different component I will have a different JPanel then? That sounds very convenient :) – Quillion Jun 13 '13 at 16:34
  • 1
    @Quillion For each region of the frame, create a `JPanel`, add the components to the panel, and then add the panel into the right region onto the frame's content pane. – gparyani Jun 13 '13 at 16:37
  • Thanks :) I was searching for exactly that. Among all the JDesktopPane and everything else I was getting confused what was the right thing to do. – Quillion Jun 13 '13 at 16:43
  • 1
    *"I'm looking up constant field values and `"Center"` is the value for `BorderLayout.CENTER`"* While `Center` happens to be the value of the constant, the constant is defined by the API. To remove or rename the constant would require Oracle to deprecate the original, whereas they can ..change the *value* of that constant at any time they want. If your code breaks as a result of using the 'magic number', it is purely your own fault. Fragile design. – Andrew Thompson Jun 13 '13 at 19:23
  • Also a note on the other values.. `"East"` is better described as `BorderLayout.PAGE_END` since that will be shown on the right hand side of the GUI for locales that use left-to-right layout of text, & on the left hand side of the GUI for locales that use right-to-left text layout. If we use the JRE defined *constants,* we do not need to bother with the *values* and everything happens automatically. – Andrew Thompson Jun 13 '13 at 19:28
  • @AndrewThompson `BorderLayout.PAGE_END` is `"Last"`, not `"East"`. – gparyani Jun 18 '13 at 23:25
  • My bad. I meant [`BorderLayout.LINE_END`](http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html#LINE_END). – Andrew Thompson Jun 18 '13 at 23:48
  • @AndrewThompson That's `"After"`. – gparyani Jun 19 '13 at 01:16