JButtons disappear after the popup menu shows

Go To StackoverFlow.com

2

I added listeners to my JButtons for the popup menu but when the popup menu appears the JButtons disappear and I would need to hover my cursor on the buttons to make them appear again. Why is it like this?

(all the methods here are in the same class)

public Inventory() {
    setLayout(null);
    setBounds(0, 0, 175, 210);

    initPopupMenu(); // this just sets what is inside the popup menu
    int x;
            // 30 buttons
    for(x = 0; x < 30; x++) {
        button[x] = new JButton();
        add(button[x]);
        button[x].addMouseListener(this);
    }

    x = 0;
            // it's a grid of buttons
    for(int i = 0; i < 5; i++) 
        for(int j = 0; j < 6; j++) {
            button[x].setBounds(i*35+1,j*35+1, 33,33);  
            x++;
        }


}

public void mouseClicked(MouseEvent e) {
    for(int j = 0; j < 30; j++) // i tried this one but it still disappears
        button[j].repaint();

    for(int i = 0; i < 30; i++) {
        if(e.getSource() == button[i]) {
            System.out.println("You pressed Button "+i);
            popup.show(e.getComponent(), e.getX(), e.getY());
        }

    }

}

This is what happens, jbuttons popup

2012-04-04 07:40
by Zik
It would be helpful if you posted some code that includes how you create the buttons and the menus. Does the menu hide the button, or do they just "dissapear" - John Snow 2012-04-04 07:43
"Why is it like this?" Because of the code used. For better help sooner, post an SSCCE - Andrew Thompson 2012-04-04 07:45
You can add some code so we can understand what is wrong - mbaydar 2012-04-04 07:50
@RahulBorkar "to receive something personally from you" It does not count for much, but you already have. I regularly see answers from you that make me think "Great answer, now I don't have to deal with that. +1". ; - Andrew Thompson 2012-04-04 08:38
"all the methods here are in the same class" Thanks for the edit, but note that I still think you are more likely to get help by posting an SSCCE, rather than uncompilable code snippets - Andrew Thompson 2012-04-04 08:46
my class implements the mouse listener of cours - Zik 2012-04-04 08:46
@AndrewThompson ok. sorry, I'm new her - Zik 2012-04-04 08:46
No need to be sorry. We were all new at one stage, and some very experienced folk have not heard of an SSCCE. :) (In addition to that, many code problems are solved without an SSCCE. - Andrew Thompson 2012-04-04 08:48


3

Stop using null Layout, seems like that can be one of the issues regarding this. Your JFrame appears sort of BLACK to me, is this some THEME or a new LOOK AND FEEL you are using, that can be the cause of this thing too. Here check this out, it's working flawlessly here with this code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonPopUp
{
    private static final int SIZE = 30;
    private JButton[] button = new JButton[SIZE];
    private JPopupMenu popup = new JPopupMenu("Hello World");

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Button POP UP Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationByPlatform(true);

        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 5));

        JMenuItem menuItem1 = new JMenuItem("Menu Item 1");
        JMenuItem menuItem2 = new JMenuItem("Menu Item 2");
        //popup.add(greetings);
        popup.insert(menuItem1, 0);
        popup.insert(menuItem2, 1);

        for (int i = 0; i < SIZE; i++)
        {
            button[i] = new JButton();
            button[i].setBorder(BorderFactory.createEtchedBorder());
            button[i].addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent me)
                {
                    System.out.println("I am WORKING!!");
                    popup.show((JComponent)me.getSource(), me.getX(), me.getY());
                }
            });
            contentPane.add(button[i]);
        }

        frame.getContentPane().add(contentPane);
        frame.setSize(175, 250);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonPopUp().createAndDisplayGUI();
            }
        });
    }
}

Here is the output :

Before Event After Event

2012-04-04 10:58
by nIcE cOw
+1 corrent answer, null layout is evil - Stephan 2012-04-04 11:01
@Stephan : Thankyou and Keep Smiling :-) , that LnF can be another culprit : - nIcE cOw 2012-04-04 11:03
This would become even nicer and more dynamically if you use a JTable with custom cells. ;- - Stephan 2012-04-04 11:05
The black grid behind the buttons is just because of paint method. Thank you very much! I will experiment with this code - Zik 2012-04-04 11:05
@Marvin : Try this latest code, I had added Border to it, why you using paint(...) method, any specific reason for that, since in Swing never override paint(...) always override paintComponent(...) if the said JComponent has one. You are MOST Welcome and Keep Smiling :- - nIcE cOw 2012-04-04 11:11
Ok I will take note of that, but why should you never override paint? I'd just like to ask. And also calling repaint() calls paint() not paintComponent(), am I right? or not? I'm just a beginner in Java. : - Zik 2012-04-04 11:31
More than me, Java Docs can help you on this, why paintComponent(...)nIcE cOw 2012-04-04 11:33
ok thanks again = - Zik 2012-04-04 11:37
Ads