Popup.hide() doing absolutely nothing

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());
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();
}
});
}
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();
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?
43 Replies
JavaBot
JavaBot11mo ago
This post has been reserved for your question.
Hey @The Typhothanian! 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.
The Typhothanian
The TyphothanianOP11mo ago
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 🤮 ugh
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
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;
}
}
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();
}
}
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();
}
}
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
It works just fine yeah
The Typhothanian
The TyphothanianOP11mo ago
huh it didn't capture everything nvmd apparently xbox game bar doesn't capture popups any ideas?
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
k thank you
JavaBot
JavaBot11mo ago
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.
The Typhothanian
The TyphothanianOP11mo ago
ping me when your back
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
huh can I see code?
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
huh The issue could be related to the JButton actually i have absolutely no clue
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
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
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
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
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
im stupid jar files exist and its a modular project sorry i was grabbing h2o
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
Whats the cmd prompt command to run a jar with a custom main method path? java -jar something?
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
yeah did that and got no main manifest oh wait sorry the middle one with abc on it is the dropdown
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
k
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
may not import the best since its a module
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
ok thank you
JavaBot
JavaBot11mo ago
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.
The Typhothanian
The TyphothanianOP11mo ago
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
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
fix
No description
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
I removed the paintMenu from end of paintComponent, then changed the mouseReleased to be that, and works just fine yeah
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
The Typhothanian
The TyphothanianOP11mo ago
yes I also need to repaint the other buttons when one is pressed, but it works now thank you
JavaBot
JavaBot11mo ago
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.
JavaBot
JavaBot11mo ago
Post Closed
This post has been closed by <@801145088830210129>.
Want results from more Discord servers?
Add your server