Piano Keys in Swing Layout: Left-Padding?
Question
I am creating a container of JComponents which will look like a piano keyboard. The black keys look like this (Groovy)
def setBlackNotes(buttons) {
def octaves = (int)(buttons.size() / 5)
def gridLayout = new GridLayout(1, octaves*7);
def blackNotePanel = new JPanel(gridLayout)
this.add blackNotePanel
def i = 0
octaves.times {
2.times {
blackNotePanel.add buttons[i++]
}
blackNotePanel.add Box.createHorizontalBox()
3.times {
blackNotePanel.add buttons[i++]
}
blackNotePanel.add Box.createHorizontalBox()
}
}
Which is just what I need, and looks like this:
alt text http://dl.dropbox.com/u/2652228/Screen%20shot%202010-03-25%20at%208.10.07%20PM.png
but then I'd like to move this over to the right by half-a-key width. All of my attempts to move the blackNotePanel
over by an arbitrary width -- wrapping it a BorderLayout, a MigLayout, etc. -- have failed or changed the spacing of the GridLayout radically.
Any suggestions on how to move this over to the right by an arbitrary amount in pixels?
Solution
Add an EmptyBorder to the panel. You can specify the left inset to be whatever you want.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow