This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Main menu  RSS feed

 
Matthew John Dunk
Greenhorn
Posts: 10
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am tasked with creating several JPanels each of which has a title centered at the top. As such I have created parent panel
that each panel extends. However, I am uncertain as to
whether drawing the string on the panel or using a JLabel
is the best solution. I would greatly appreciate any advice on this matter

Note: The panels might allow dynamic resizing
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest using a JLable for the title.

First think how this title would be placed in those panels. Should the title itself be a JPanel and put in the top? You mentioned a parent panel, how are you planning to use the parent panel?


 
Matthew John Dunk
Greenhorn
Posts: 10
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My idea was to have a panel class with methods to "draw the String or place the Jlabel" in order to reduce redundancies.


If do decide to use a JLabel. What layoutManager would be most appropriate?
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i were doing it, I will use:
BorderLayout for the outer JPanel (hence the title JLabel goes in the NORTH)
Have panel(s) to do the rest of the items and put in CENTER (these panels can each have its own layout managers)

Thinking about it, your title can indeed be a JPanel itself and in its own class. That way you can reuse it.
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Dunk wrote:I suppose the titlePanel will also have a borderLayout with the label centered?


Yes the CENTER is good.

Another approach is use the default JPanel layout (FlowLayout) because you only have 1 component (JLabel).
 
Matthew John Dunk
Greenhorn
Posts: 10
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct in think a gridLayout is appropriate for the buttonPane?

Do I add a emptyBorder to stop the buttons filling the screen?


Thanks for all you help, K. Tang
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The GridLayout sounds good.

I don't recall if the JButton can set dimension or not. Border around the buttons isn't necessary if your intention is to make it certain size.
 
Matthew John Dunk
Greenhorn
Posts: 10
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying both setPreferredSize() and setSize() has no effect.
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider using the BoxLayout for the buttonPanel.

If that doesn't work, then wrap the JButton in a JPanel and add this panel to the layout.
 
Matthew John Dunk
Greenhorn
Posts: 10
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BoxLayout won't work in this case. Alternatives?
 
K. Tsang
Bartender
Posts: 3624
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the relevant code you tried for adding buttons. It's hard to visualize the screen without knowing how you build the GUI.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!