Issue
I really hope you can help me with this problem. I have been working in a NetBeans app, and I created an animated slide menu using a JPanel, so I was intending to make the user shows up the menu that slides and covers part of the content of the current panel in the app, the user interacts with menu (it presses one of the buttons on the menu) and menu slides left to be hidden again.
Photos of the menu Hidden and Shown:
My issue here, is if among the content covered by the menu (when the menu is released) there is a jButton or a jLabel, these just overlay the content of the menu, they keep visible, and they came in front covering the menu's content.
Issues here:
I have tried fixing it by making some button and labels hidden when the menu shows up, using the method ButtonOrLabel.setVisible(false)
and once the user presses the button that hides the menu, they are set to visible again, however mainting this is a nightmare, since my app is quiet big and I need to manage this for each element (button, lapel, panel, etc...) that cames covered by the menu when it is released.
Another issue that I noticed, it is that the menu pops up inmediatly when a button is pressed in some panel or if the user resizes the windows of the app. It just appears immediately without any animation.
So what am I doing wrong? How can accomplish fixxing this? Thank you.
"After the edit:" I incorporate here some code that will show the problem. Is a MRE version of the main problem.
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class test extends javax.swing.JFrame {
public test() {
initComponents();
this.setLocationRelativeTo(null); }
/////// THIS CODE IS GENERATED BY THE IDE/ / // ///////////////////////////////////////////////////////////////////////////
private void initComponents() { // You can skip this part, since this is the init of each component
menu = new javax.swing.JPanel();
goToA = new javax.swing.JButton();
hideMenu = new javax.swing.JButton();
goToA1 = new javax.swing.JButton();
interfaces = new javax.swing.JPanel();
sttings = new javax.swing.JPanel();
keepVisibleAlwaysThisTitle = new javax.swing.JLabel();
showMenu = new javax.swing.JButton();
home = new javax.swing.JPanel();
panelA = new javax.swing.JPanel();
textArea = new javax.swing.JTextArea();
cleares = new javax.swing.JButton();
writes = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setBackground(new java.awt.Color(255, 255, 255));
setBounds(new java.awt.Rectangle(0, 0, 1206, 808));
setLocation(new java.awt.Point(0, 0));
setMaximizedBounds(new java.awt.Rectangle(0, 0, 1206, 808));
setMinimumSize(new java.awt.Dimension(1206, 808));
setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
setName("Main"); // NOI18N
setSize(new java.awt.Dimension(1206, 808));
menu.setBackground(new java.awt.Color(38, 39, 40));
menu.setAlignmentX(0.0F);
menu.setAlignmentY(0.0F);
menu.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
menu.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
menu.setDoubleBuffered(false);
menu.setMaximumSize(new java.awt.Dimension(293, 1808));
menu.setMinimumSize(new java.awt.Dimension(0, 808));
menu.setPreferredSize(new java.awt.Dimension(295, 808));
menu.setRequestFocusEnabled(false);
menu.setVerifyInputWhenFocusTarget(false);
goToA.setBackground(new java.awt.Color(38, 39, 40));
goToA.setFont(new java.awt.Font("Dialog", 1, 13)); // NOI18N
goToA.setForeground(new java.awt.Color(255, 255, 255));
goToA.setText("GO TO A");
goToA.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
goToA.setContentAreaFilled(false);
goToA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
goToA.setMaximumSize(new java.awt.Dimension(235, 50));
goToA.setMinimumSize(new java.awt.Dimension(235, 50));
goToA.setPreferredSize(new java.awt.Dimension(235, 50));
goToA.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
goToAMouseClicked(evt); } });
goToA.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); } });
hideMenu.setBackground(new java.awt.Color(102, 102, 102));
hideMenu.setForeground(new java.awt.Color(255, 255, 255));
hideMenu.setText("CLOSE THE MENU");
hideMenu.setToolTipText("");
hideMenu.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
hideMenu.setBorderPainted(false);
hideMenu.setContentAreaFilled(false);
hideMenu.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
hideMenuMouseClicked(evt); } });
goToA1.setBackground(new java.awt.Color(38, 39, 40));
goToA1.setFont(new java.awt.Font("Dialog", 1, 13)); // NOI18N
goToA1.setForeground(new java.awt.Color(255, 255, 255));
goToA1.setText("GO TO HOME");
goToA1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
goToA1.setContentAreaFilled(false);
goToA1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
goToA1.setMaximumSize(new java.awt.Dimension(235, 50));
goToA1.setMinimumSize(new java.awt.Dimension(235, 50));
goToA1.setPreferredSize(new java.awt.Dimension(235, 50));
goToA1.addMouseListener(new java.awt.event.MouseAdapter() { });
goToA1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToA1ActionPerformed(evt); } });
javax.swing.GroupLayout menuLayout = new javax.swing.GroupLayout(menu);
menu.setLayout(menuLayout);
menuLayout.setHorizontalGroup(
menuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(menuLayout.createSequentialGroup()
.addGroup(menuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(menuLayout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(menuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(goToA1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(goToA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(menuLayout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(hideMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(32, 32, 32)) );
menuLayout.setVerticalGroup(
menuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(menuLayout.createSequentialGroup()
.addGap(102, 102, 102)
.addComponent(goToA, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 186, Short.MAX_VALUE)
.addComponent(hideMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(159, 159, 159)
.addComponent(goToA1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(225, 225, 225)) );
interfaces.setBackground(new java.awt.Color(255, 255, 255));
interfaces.setAlignmentX(0.0F);
interfaces.setAlignmentY(0.0F);
interfaces.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
interfaces.setMaximumSize(new java.awt.Dimension(1206, 808));
interfaces.setMinimumSize(new java.awt.Dimension(100, 808));
interfaces.setPreferredSize(new java.awt.Dimension(1206, 808));
interfaces.setLayout(new java.awt.CardLayout());
sttings.setBackground(new java.awt.Color(255, 255, 255));
sttings.setAlignmentX(0.0F);
sttings.setAlignmentY(0.0F);
sttings.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
sttings.setMaximumSize(new java.awt.Dimension(1206, 100));
sttings.setMinimumSize(new java.awt.Dimension(0, 0));
sttings.setOpaque(false);
sttings.setPreferredSize(new java.awt.Dimension(1206, 808));
keepVisibleAlwaysThisTitle.setBackground(new java.awt.Color(238, 206, 62));
keepVisibleAlwaysThisTitle.setFont(new java.awt.Font("Dialog", 1, 26)); // NOI18N
keepVisibleAlwaysThisTitle.setForeground(new java.awt.Color(255, 255, 255));
keepVisibleAlwaysThisTitle.setText(" KEEP THIS TITLE ALWAYS ON TOP");
keepVisibleAlwaysThisTitle.setMaximumSize(new java.awt.Dimension(interfaces.getWidth(), 40));
keepVisibleAlwaysThisTitle.setMinimumSize(new java.awt.Dimension(interfaces.getWidth(), 40));
keepVisibleAlwaysThisTitle.setOpaque(true);
keepVisibleAlwaysThisTitle.setPreferredSize(new java.awt.Dimension(interfaces.getWidth(), 40));
showMenu.setText("SHOW MENU");
showMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showMenuActionPerformed(evt); } });
home.setBackground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout homeLayout = new javax.swing.GroupLayout(home);
home.setLayout(homeLayout);
homeLayout.setHorizontalGroup(
homeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1206, Short.MAX_VALUE) );
homeLayout.setVerticalGroup(
homeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 808, Short.MAX_VALUE) );
panelA.setBackground(new java.awt.Color(255, 255, 255));
panelA.setAlignmentX(0.0F);
panelA.setAlignmentY(0.0F);
panelA.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
panelA.setMaximumSize(new java.awt.Dimension(1206, 808));
panelA.setMinimumSize(new java.awt.Dimension(1206, 808));
panelA.setPreferredSize(new java.awt.Dimension(1206, 808));
textArea.setColumns(20);
textArea.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
textArea.setForeground(new java.awt.Color(255, 255, 255));
textArea.setRows(5);
textArea.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(255, 212, 57)));
textArea.setOpaque(false);
cleares.setText("WIPES THE TEXT BELOW");
cleares.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearesActionPerformed(evt); } });
writes.setText("WRITE A TEXT BELOW");
writes.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
writesActionPerformed(evt); } });
javax.swing.GroupLayout panelALayout = new javax.swing.GroupLayout(panelA);
panelA.setLayout(panelALayout);
panelALayout.setHorizontalGroup(
panelALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelALayout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(panelALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(panelALayout.createSequentialGroup()
.addComponent(writes)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cleares))
.addComponent(textArea, javax.swing.GroupLayout.PREFERRED_SIZE, 1140, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(33, Short.MAX_VALUE)) );
panelALayout.setVerticalGroup(
panelALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelALayout.createSequentialGroup()
.addGap(280, 280, 280)
.addGroup(panelALayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(writes)
.addComponent(cleares))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textArea, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(78, Short.MAX_VALUE)) );
javax.swing.GroupLayout sttingsLayout = new javax.swing.GroupLayout(sttings);
sttings.setLayout(sttingsLayout);
sttingsLayout.setHorizontalGroup(
sttingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, sttingsLayout.createSequentialGroup()
.addGap(0, 11, Short.MAX_VALUE)
.addComponent(showMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(keepVisibleAlwaysThisTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 1076, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(sttingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelA, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(sttingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(home, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
sttingsLayout.setVerticalGroup( sttingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(sttingsLayout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(sttingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(keepVisibleAlwaysThisTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(showMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(728, Short.MAX_VALUE))
.addGroup(sttingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelA, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(sttingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(home, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) );
interfaces.add(sttings, "card2");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1206, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(menu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(0, 911, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(interfaces, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) );
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 808, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(menu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(interfaces, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) );
menu.getAccessibleContext().setAccessibleDescription(""); pack(); }
/////////// END OF THE INIT OF THE COMPONENTS /////// THIS CODE IS GENERATED BY THE IDE/ / // ///////////////
/// HERE IS MY CODE:
private void hideMenuMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("Closing Menu");
if (menu.getWidth() > 0) {
menu.setFocusable(true);
Thread th = new Thread(){
public void run () {
try {
for (double i = menu.getMaximumSize().width; i > -1; i=i-1.5) {
Thread.sleep(1);
menu.setSize((int) i, menu.getHeight()); } /*LOOP END*/
} catch (Exception e) {JOptionPane.showMessageDialog(null, e); } } };
th.start(); } }
private void goToAMouseClicked(java.awt.event.MouseEvent evt) {
panelA.setVisible(true);
keepVisibleAlwaysThisTitle.setText(" PANEL A");
hideMenuMouseClicked(evt);
}
private void showMenuActionPerformed(java.awt.event.ActionEvent evt) {
if (menu.getWidth() == 0) {
menu.setSize(0, menu.getHeight());
Thread th = new Thread(){
public void run () {
try {for (double i = 0; i < menu.getMaximumSize().width + 1; i=i+1.5) {
Thread.sleep(1);
menu.setSize((int) i, menu.getHeight()); } /*LOOP END*/
} catch (Exception e) {JOptionPane.showMessageDialog(null, e); } } };
th.start();
menu.setSize(menu.getMaximumSize().width, menu.getHeight());
menu.setFocusable(true); } }
private void clearesActionPerformed(java.awt.event.ActionEvent evt) {
textArea.setText(""); }
private void writesActionPerformed(java.awt.event.ActionEvent evt) {
textArea.setText("PANEL A IS ONE OF THE DIFFERENT PANELS THAT THE APP WILL HAVE, AND THE USER WILL ACCESS TO THOSE VIA THE MENU"); }
private void goToA1ActionPerformed(java.awt.event.ActionEvent evt) {
panelA.setVisible(false);
home.setVisible(true);
keepVisibleAlwaysThisTitle.setText("You are now in home"); }
/////////////////// THIS IS THE MAIN CLASS ///////////// THIS CODE IS GENERATED BY THE IDE/ / // //////////////////
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break; } } } catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(mreForStackOverflow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(mreForStackOverflow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(mreForStackOverflow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(mreForStackOverflow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mreForStackOverflow().setVisible(true); } }); }
public javax.swing.JButton hideMenu;
private javax.swing.JPanel interfaces;
private javax.swing.JPanel sttings;
private javax.swing.JButton cleares;
private javax.swing.JButton goToA;
private javax.swing.JButton goToA1;
private javax.swing.JPanel home;
private javax.swing.JLabel keepVisibleAlwaysThisTitle;
private javax.swing.JPanel menu;
private javax.swing.JPanel panelA;
private javax.swing.JButton showMenu;
private javax.swing.JTextArea textArea;
private javax.swing.JButton writes;}
As far as I know, I am fairly sure you can just copy this code and compile it, and hopefully you may be able to reproduce the issue I am describing.
Solution
Well, still not an MRE since:
- the code doesn't even compile since the class name is incorrect. Class names should start with an upper case character. Why do I have to keep mentioning little details like this??? You get one free answer, but no more if you can't follow Java conventions when posting code!
- there is still a lot of code not directly related to the problem. Your problem is about layering a menu over top of a button.
So all you need is:
- All you need is a frame with the "Show Menu".
- Then on the menu you need a "Hide Menu" button.
All the other components are not relevant to the problem you are describing.
I was able to resolve some issues but not everything.
there is a jButton or a jLabel, these just overlay the content of the menu, they keep visible, and they came in front covering the menu's content.
Mouse events are passed to the component which has a MouseListener added to it. When the menu is visible and you move the mouse around the menu as the mouse moves over a button under the menu the button is painted because the button has a mouse listener added to it to handle the rollover effects of the button.
I was able to prevent this in most cases by adding a dummy MouseListener to the panel:
menu.addMouseListener( new MouseAdapter() {});
menu.addMouseMotionListener( new MouseMotionAdapter() {});
However, it still causes a problem when you click on the "Show Menu" button and then move the mouse. Not sure why.
the menu pops up inmediatly when a button is pressed in some panel or if the user resizes the windows
You logic only manipulates the setSize(...) method of the menu to hide/show it.
However when the frame is resized the layout manager is invoked which will reset the size() of the menu based on its preferred size and the menu is repainted.
The solution I used was to add:
menu.setVisible(false);
in the "Hide Menu" logic and add
menu.setVisible(true)
in the "Show Menu" logic.
I have never used a GroupLayout. I also don't use layout managers to display component in the 3rd dimension so I'm not sure if using the GroupLayout is the best solution for something like this.
Maybe instead the menu should be a Glass Pane
of the frame that you can show/hide are required.
Or maybe you should be using a JLayeredPane.
Both of the above are better designed to show components in layers.
Read the Swing Tutorial for more information. Specifically the sections on:
- How to Use Root Panes
- How to Use Layered Panes
Answered By - camickr
Answer Checked By - Cary Denson (JavaFixing Admin)