#Popup.hide() doing absolutely nothing

75 messages · Page 1 of 1 (latest)

dry mica
#

I have a swing Popup. I am creating it like this:

JPanel panel = new JPanel(new GridLayout(0, 1));
Point screenPoint = getLocationOnScreen();
popup = PopupFactory.getSharedInstance().getPopup(this, panel, screenPoint.x, screenPoint.y + getHeight());

As you can see, it looks good (I am doing this inside a custom dropdown JComponent). Then, I add buttons to it:

for (E item : items) {
    panel.add(new UButton(item.toString(), 0, 0, 200, 40, UAlignment.Default, e -> {
        open = !open;
        selectedItem = item;
        popup.hide();
        repaint();
    }) {
        @Override
        int getTexIndex() {
            return selectedItem == item ? 2 : super.getTexIndex();
        }
    });
}

(getTexIndex() is for what texture the button should use, doesn't matter here)
The consumer of MouseEvent is run when the button is released. It should hide the popup, but it doesn't. Then, I show the popup after adding the stuff:

popup.show();

I have absolutely no idea why this code isn't working as intended. Also, it isn't just in this area. Calling hide() somewhere else doesn't work. But, calling hide right after showing it works just fine?

frozen talonBOT
#

This post has been reserved for your question.

Hey @dry mica! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

dry mica
#

Yes, I have tried SwingUtilities.invokeLater, no effect

#

And I can't use JPopupMenu because of the natural select overlay. It bleeds through my texture and looks like 🤮

dry mica
#

ugh

gloomy verge
dry mica
#

It is a custom class I made

#

here

#
package net.typho.utils.uwt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;

public class UButton extends UButtonHusk {
    private long lastPress;
    private static final int sticky = 500;

    public UButton(String text, int x, int y, int width, int height, UAlignment alignment, Consumer<MouseEvent> action) {
        super(text, x, y, width, height, alignment);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                setTexIndex(1);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setTexIndex(0);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                setTexIndex(2);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (contains(e.getX(), e.getY())) {
                    setTexIndex(1);
                } else {
                    setTexIndex(0);
                }

                action.accept(e);

                lastPress = System.currentTimeMillis();

                new Timer(sticky, ae -> repaint()) {{
                    setRepeats(false);
                    start();
                }};
            }
        });
    }

    @Override
    int getTexIndex() {
        return System.currentTimeMillis() - sticky > lastPress ? super.getTexIndex() : 2;
    }
}
#

and UButtonHusk

package net.typho.utils.uwt;

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

public class UButtonHusk extends JComponent {
    private String text;
    private int texIndex = 0;

    public UButtonHusk(String text, int x, int y, int width, int height, UAlignment alignment) {
        this.text = text;
        setBounds(alignment.get(x, y, width, height));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        UWT.stretchDrawImage(g, 0, 0, getWidth(), getHeight(), UStyles.stretchImageScale, UStyles.buttonTexture.getSection(getTexIndex()).get(), this);

        UWT.drawStringAligned(g, getWidth() / 2, getHeight() / 2, UAlignment.Center, UStyles.textFont, UStyles.textColor, text);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 40);
    }

    void setTexIndex(int texIndex) {
        this.texIndex = texIndex;
        repaint();
    }

    int getTexIndex() {
        return texIndex;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        repaint();
    }
}
gloomy verge
#

Have you checked that the mouse listener is actually triggered for your UButton. If I remember correctly JComponent is not set to be focusable meaning that it can't intercept mouse events.

dry mica
#

It works just fine yeah

#

huh it didn't capture everything

#

nvmd

#

apparently xbox game bar doesn't capture popups

#

any ideas?

gloomy verge
#

checking something rn, will let you know

dry mica
#

k thank you

frozen talonBOT
# dry mica k thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

dry mica
#

ping me when your back

gloomy verge
dry mica
#

huh

#

can I see code?

gloomy verge
#
JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(600, 800));
        frame.setLocationRelativeTo(null);
        frame.setLayout(new BorderLayout());
        
        
        JPanel contentPane = (JPanel) frame.getContentPane();
        contentPane.setLayout(new FlowLayout());
        
        final JPanel panel = new JPanel(new GridLayout(3, 1));
        final Popup popup = PopupFactory.getSharedInstance().getPopup(null, panel, 400, 200);
        final String[] examples =  {"A","B","C"};
        {
            for (int i = 0; i< examples.length; i++) {
                panel.add(new UButton(examples[i], 0, 20 * i, 200, 40, f -> {
                    popup.hide();
                }));
            }
        }
        
        
        JButton button = new JButton("This is a test");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                popup.show();
            }
        });
        contentPane.add(button);
        frame.setVisible(true);

I had to modify your code a bit. I got rid of the UAlignment parameters and rewrote the paintComponent method in the UBottonHusk as follows:

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawRect(getX(),getY(),getWidth(),getHeight());
            g.drawString(text,getX(),getY());
        }

the buttons are renderd weird but that's not related to your issue.

dry mica
#

huh

#

The issue could be related to the JButton

#

actually i have absolutely no clue

gloomy verge
#

the JButton is just a means to display the popup it shouldn't impact the behavior of the popup itself

dry mica
#

I am looking at this and thinking that there is absolutely no reason why it shouldn't work for me

#

lemme check one thing

#

nope that wasnt it

#

minecraft villager hrm sound

gloomy verge
#

do you have your code uploaded somewhere so I can have a look ?

dry mica
#

i do

#

but its not just the UWT part

#

its everything I've worked on in the past 6 months

#

i can separate it if you give me a min

gloomy verge
#

if you have the newest version in some folder it's alright.

dry mica
#

im stupid jar files exist

#

and its a modular project

#

sorry i was grabbing h2o

gloomy verge
#

no worries

dry mica
#

Whats the cmd prompt command to run a jar with a custom main method path?

#

java -jar something?

gloomy verge
#

java -cp SomeJar.jar full.Name.To.MainClass I think

dry mica
#

yeah did that and got no main manifest

#

oh wait

#

sorry

#

the middle one with abc on it is the dropdown

gloomy verge
#

i'm not going to run a random jar file

dry mica
#

k

gloomy verge
#

if you can give me the source code as a zip or even better upload it to github I can have a look

dry mica
#

may not import the best since its a module

gloomy verge
#

I'll have a look

dry mica
#

ok

#

thank you

frozen talonBOT
# dry mica thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

dry mica
#

I feel like when I click it to close the dropdown it just opens it again

#

Proven because the content of the dropdown reset when doing that

#

Thinking why

#

AHA

gloomy verge
#

I don't know why I didn't catch that

dry mica
gloomy verge
#

You are creating the popup every time the menu is rendered

#

In your paintMenu

        if(popup == null) {
            popup = PopupFactory.getSharedInstance().getPopup(this, panel, screenPoint.x, screenPoint.y + getHeight());
        }
dry mica
#

I removed the paintMenu from end of paintComponent, then changed the mouseReleased to be that, and works just fine

#

yeah

gloomy verge
#

and then just set the popup to null when a button is pressed

dry mica
#

yes

#

I also need to repaint the other buttons when one is pressed, but it works now

#

thank you

frozen talonBOT
# dry mica thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.