This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
With the following code, I get two layout problems. One, my menubar does not stretch all the way to each side of the JFrame. This in turn seems to be causing my TabbedPanes to line up incorrectly, with there only being room for two TabbedPanes per row. I would like all 5 to be in one row? What is causing this?
Ignore makeConversionsLabel, for it was not finished at the time of posting.
Thanks in advance for your help.
Joined: Mar 07, 2004
Ah ha! I fixed the TabbedPane problem, but the menu bar still does not reach from side to side, how do I fix this?
EDIT: Changed screen to screen to, side to side. [ October 15, 2005: Message edited by: Atrus Greyor ]
I came up with a slightly different approach (before seeing Michael's post) and a few other comments: JMenuBar extends from JComponent so you can add it to a Container just like any other JComponent. When you add the JMenuBar to a JPanel (which has FlowLayout as its default layout manager) it will be centered and shown at its preferred size. When you add the JPanel to the north section of a BorderLayout the panel will expand horizontally to fill the width of the north section allowing the JMenuBar to remain at its preferred size.
You can add the JMenuBar to the JFrame with the 'setJMenuBar' method. Or you could add the JMenuBar to the north section of the JFrames default BorderLayout which will expand it (the menuBar) to fill the north section (which the menuBars parent JPanel was doing before).
Here I used the 'setJMenuBar' method, eliminated the JPanel extension from the (CustomMenu) class that makes the menus and menuBar and added a 'get' method to return the JMenuBar.
About the JFrame: you can extend JFrame and call JFrame methods on it from within the class. Or you can not extend JFrame, create a new JFrame and call JFrame methods on it, ie the reference to the new JFrame (the variable 'frame' in your code). These are two ways of making a JFrame for your gui so choose one or the other but not both. The first uses inheritance. The second uses composition.