2

I need to create this frame:

Which layout should I use? I am thinking about box or grid layout but then menu on the right will be a problem.

Jon7
  • 7,165
  • 2
  • 33
  • 39
hudi
  • 15,555
  • 47
  • 142
  • 246
  • Is the menu on the side a *sliding menu* i.e. appear and disappear on click? – iTech Mar 15 '13 at 00:56
  • 1
    If you go with `AWT` layout, [GridBagLayout](http://docs.oracle.com/javase/6/docs/api/java/awt/GridBagLayout.html) gives you the most control. – Mr. Polywhirl Mar 15 '13 at 00:57
  • it is dissapear by default and only arraow is visible and when mouse move to this arrow then this menu is visible – hudi Mar 15 '13 at 00:58
  • 3
    You should use lots of compound layouts... – MadProgrammer Mar 15 '13 at 00:59
  • yea but there should be one main – hudi Mar 15 '13 at 00:59
  • `GroupLayout` could create that GUI. Or a [nested/compound layout](http://stackoverflow.com/a/5630271/418556) as noted by @MadProgrammer. – Andrew Thompson Mar 15 '13 at 01:01
  • @hudi The base layout is most likely going to either a `BorderLayout`, `GridLayout` or `GridBagLayout` – MadProgrammer Mar 15 '13 at 01:07
  • Start by checking out [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). As @MadProgrammer suggests, you most likely will need to nest lots of different layouts to get the exact look that you want. It will take quite a bit of experimentation to get it exactly right. – Code-Apprentice Mar 15 '13 at 01:08

3 Answers3

8

There is a lot of repetition in your code. I would break down each section and make it a separate component and focus on it's individual layout needs.

enter image description here

In you main screen you have 4 main areas (excluding the menu).

I would use something like GridBagLayout to layout these 4 sections. This allows each section the ability to grow to it's required height, without effecting the others. You can also supply individual growth hints to each section as you see fit.

The first section is a JLabel, so it's pretty simple

enter image description here

This section is basially three labels with slightly different alignments. I would still use a GridBagLayout as it provides the greatest flexibility to allow for individual cell alignment, while allowing you to provide sizing hints (for example, I might make the time row in width as the screen size is changed).

enter image description here

This section is a little more complicated. Basically, it's the same component on the left and right, with some clever properties, you would be able to change the position of the labels without to much of an issue.

The individual dots/labels could be laid out using a GridLayout. I might err on the side of caution and use a GridBagLayout, as it would allow the labels to be different sizes to the dots

I would then use something like a GridLayout to place each side onto a component (which would then be placed on the main screen)

enter image description here

Again, this simple the same component mirrored. I would simply create a single component that could be adjusted via a property to change the alignment.

I would then simply use a GridBagLayout to lay out each label as required. I'd do it this way because it allows each row the ability to have it's own height.

I also have 15 years referring experience in Karate. Can I come work for you?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • thx a lot but there is still one question. How I can create menu which is overflowing 3 of main screen parts. PS: there isnt much to do I just creating some app for my own use – hudi Mar 15 '13 at 01:48
  • @hudi I would use the frames glass pane. I will allow you to "float" components ontop of the others. It's a little more complicated as you become responsible for positioning and sizing the components, but will give you the result you want. Take a look at [how to use glass panes](http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) – MadProgrammer Mar 15 '13 at 02:51
  • 2
    This is a great answer. However, I'd recommand to use an OverlayLayout instead of the glass pane. – barjak Mar 15 '13 at 09:40
  • @barjak You could also use JLayer, but it comes down to needs and knowledge. +1 for suggestion – MadProgrammer Mar 15 '13 at 10:32
0

If you go with a AWT layout, GridBagLayout gives you the most control.

enter image description here

I adopted the following function from an example from java2s.com.

/**  
 * A helper method to add Components to a Container
 * using GridBagLayout  
 */  
private static void addComponent(Container container,
  Component component, int gridX, int gridY,
  int gridWidth, int gridHeight, int anchor, int fill)
{
  Insets insets = new Insets(0, 0, 0, 0);
  GridBagConstraints gbc = new GridBagConstraints(
    gridX, gridY, gridWidth, gridHeight, 1.0, 1.0,
    anchor, fill, insets, 0, 0
  );
  container.add(component, gbc);
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
-1

Forget about the base layouts, especially the GridBagLayout, and give a look at MigLayout, this will save you a lot of troubles.

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
  • This is not an answer. This is just an opinion (this is like saying "Use Windows instead of MacOS") and it does not help the OP since you don't expose how to use the MigLayout. – Guillaume Polet Mar 15 '13 at 10:08
  • It's okay to suggest other layouts, but to say that GridBagLayout is not effective is just incorrect. – VGR Mar 15 '13 at 11:05
  • @VGR I do say that `GridBagLayout` is ineffective, that would be insane to recommend this layout in 2013. – Emmanuel Bourg Mar 15 '13 at 11:10
  • @GuillaumePolet Hudi asked what layout to use, that's exactly what I did. – Emmanuel Bourg Mar 15 '13 at 11:13