1

I am creating a virtual piano using JFrame, using buttons to represent keys.

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
getContentPane().setLayout(layout); 
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGap(94, 94, 94).addComponent(
jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, 
javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)

This is what I have come up with

virtual piano

I want the buttons close to each other without gaps. Is there a possibility of removing the spaces between each button ?

Community
  • 1
  • 1
clem
  • 61
  • 2
  • 6
  • 2
    Yes, the possibility exists. However it is hard to help you without seeing your code. – AlexR Jul 23 '14 at 10:57
  • Which layout manager are you using? Please post your code. – Edwin Torres Jul 23 '14 at 11:01
  • thanks for the reply. I just designed them using the palette in NetBeans. – clem Jul 23 '14 at 12:42
  • javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(94, 94, 94) .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) – clem Jul 23 '14 at 12:46
  • 2
    See: http://stackoverflow.com/questions/4280281/making-jbuttons-overlap for some ideas. – splungebob Jul 23 '14 at 13:18
  • the black keys would be on top right? So a higher Z-order? – Oliver Watkins Jul 23 '14 at 14:00

1 Answers1

0

Use a layered pane, It supports the overlapping of components but you can manipulate the pane to make the components very close to each other without overlapping.

JLayeredPane overlap = new JLayeredPane();   
getContentPane().add(overlap);
// then you overlap.add(ITEM YOU WANT, new Integer(1));
//integers determine how you want what to overlap in an ordered fashion
Samuel Musa
  • 68
  • 2
  • 11