Well sounds little weird, but I am trying to overlap twoJButtons
(here jButton1
and jButton2
).I am using NetBeans GUI builder for this.Nothing extraordinary,I am using JLayeredPane to make it possible.
jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
Well it does overlap,but the JButton
shows its self up,when Clicked by a mouse.
A screenshot will make it clear:
(take highlighted portion as a mouse pointer)
and
When the mouse pointer is clicked on the next JButton
,it comes up,hiding the first JButton.
but,I don't want the second JButton
to pop up(overlap 1stJButton
) when clicked by a mouse.
How can i do this??Well, I think this may go critical.
Here is the code,if required:
public class NewJFrame1 extends javax.swing.JFrame {
public NewJFrame1() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLayeredPane1 = new javax.swing.JLayeredPane();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLayeredPane2 = new javax.swing.JLayeredPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(51, 51, 51));
jButton1.setText("jButton1");
jButton1.setBounds(0, 40, 73, 23);
jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jButton2.setText("jButton2");
jButton2.setBounds(40, 40, 73, 23);
jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(98, 98, 98)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(139, 139, 139)
.addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(161, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame1().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JLayeredPane jLayeredPane2;
private javax.swing.JPanel jPanel1;
}
UPDATE :
If I am not clear, here i go again.
1) I don't want to make separate buttons,they should be overlapped .
2)Overlapping area may differ,the size of the button.
3)The second JButton should not come above the 1st JButton,when clicked.(Solution for this,but not for how to layout the buttons.)
4)There may be confusion,which button was clicked,but its ok for me.
What does layout manager have to do with it?
@Patricia Shanahan has pointed out the problem with overlapping the buttons. One solution is to let the layout do the work. In the example below, butonPanel
has a centered FlowLayout
by default. It adopts the preferred size of the buttons in any Look & Feel.
Addendum: Ah, you want the buttons to overlap but retain a stable z-order. Because rendering is controlled by the button's UI delegate, a subclass of ButtonUI
, the effect is Look & Feel dependent. For example, both com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
and com.apple.laf.AquaLookAndFeel
behave as you want, while many others do not. Absent creating your own delegate, shown here, you might try to adapt the approach shown here, which hides the button's and uses an overlaid image. Depending on the enclosing container's layout, either FlowLayout
or GridLayout(1, 0)
should work.
As an aside, don't let the GUI designer dictate your GUI design!
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** @see https://stackoverflow.com/a/14075990/230513 */
public class LayoutTest {
private void display() {
JFrame f = new JFrame("LayoutTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JPanel(){
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 150); // content placeholder
}
}, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(); // default: FlowLayout, centered
buttonPanel.add(new JButton("Button1"));
buttonPanel.add(new JButton("Button2"));
f.add(buttonPanel, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new LayoutTest().display();
}
});
}
}