JPopupMenu appears but seems to not react - java

I've problems to get a JPopupMenu working correctly.
What I expect
The menu should pop up once I do a right click with my mouse. Then I can select an item from the menu and do whatever I want..
What I actually get
The menu appears once I do a right click but after that I can not select a menu item or at least I am missing the well known mouse hover highlight effect (I would expect that the item I am currently hovering is highlighted, like it is the case in the normal menu).
The see problem here (no highlight on hover):
Here is my example code:
package com.mycompany.mavenproject2;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class PopupMenuTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel pane = new JPanel();
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("A"));
popup.add(new JMenuItem("B"));
pane.setSize(300,300);
pane.add(popup);
pane.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)) {
popup.setLocation(e.getXOnScreen(), e.getYOnScreen());
popup.setVisible(true);
}
}
});
frame.setTitle("Test");
frame.add(pane);
frame.setPreferredSize(new Dimension(300,300));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Edit
Compare to a "normal" menu with working hovering:
Edit #2
Please see the current (unexpected) behaviour:

You need to use the JPopupMenu show method, rather than the setVisible method.
Here's the code I tested with. I'm running Windows 10 and using the Java JDK 13.0.2 with Java 8 compliance. I get the highlight on the mouse over.
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class JPopupMenuTest implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JPopupMenuTest());
}
#Override
public void run() {
JFrame frame = new JFrame();
JPanel pane = new JPanel();
pane.setPreferredSize(new Dimension(300, 300));
pane.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("A"));
popup.add(new JMenuItem("B"));
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
frame.setTitle("JPopupMenu Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Related

How to add non editable text view to a JDialog?

I have a JFrame with a button labelled as "order". When that button is clicked, a dialog box comes up. This dialog box is supposed to show a summary of items that are ordered as a text view. I thought of having a string as a way to view the order, but that did not work out...
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;
import javax.swing.JRadioButton;
public class PizzaOrder3 {
PizzaOrder3() {
JFrame frame = new JFrame();
JButton order = new JButton("Order");
JPanel panel1 = new JPanel();
panel1.add(order);
frame.getContentPane().add(panel1);
panel1.setBounds(350,632,110,40);
panel1.setOpaque(false);
//Action listener for showing summary of the order
order.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JDialog d = new JDialog(frame, "Hello", true);
//Where the coding for text view is meant to go
d.setSize(400, 300);
d.setVisible(true);
}
});
frame.setLayout(null);
frame.setSize(600, 700);
frame.getContentPane().setBackground(new Color(40, 80, 120));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
PizzaOrder3 PizzaOrder3 = new PizzaOrder3();
}
}

Display PopMenu with java Swing

I have a problem with popapMenu with java swing can you help me
there is my code
package com.bar.menu;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
#SuppressWarnings("serial")
public class PopMenuSample extends JFrame {
public PopMenuSample() {
super("Pop menu exemple");
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel panel = (JPanel) getContentPane();
// the content of the window
JScrollPane LeftjScrollPane = new JScrollPane(new JTree());
LeftjScrollPane.setPreferredSize(new Dimension(200, 0));
JTextArea textArea = new JTextArea();
JScrollPane rightjScrollPane = new JScrollPane(textArea);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
LeftjScrollPane, rightjScrollPane);
panel.add(splitPane);
JPopupMenu popupMenu = this.createPopupMenu();
textArea.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent event) {
if (event.isPopupTrigger()) {
popupMenu.show(event.getComponent(), event.getX(),
event.getY());
}
}
});
}
private JPopupMenu createPopupMenu() {
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuNew = new JMenuItem("New File");
popupMenu.add(menuNew);
return popupMenu;
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
PopMenuSample menuSample = new PopMenuSample();
menuSample.setVisible(true);
}
}
this exemple is very easy (a window is contains two zones on the left is the Jtree() and on the right is textArea in this place i want to activate my problem when is of the right mouse i want to display New File) but i can't to show the popmenu New File in the textArea by the left boton of my mouse
I used java 8
Can you help me and thanks :)
public void mousePressed(MouseEvent event) {
You are only checking the mousePressed event.
The popup trigger can be different for different LAF's.
You also need to check the mouseReleased event.
See the section from the Swing tutorial on Bringing Up a Popup Menu for more information.

How to take out white borders in JMenu and JMenuItems

I'm doing this Graphic Interface with Swing. The problem that i'm having is that i can't take the white borders that are arround the JMenuItems, and paint it all of black. Here is an image:
I would like to paint it like this (I have edited the image with paint :D):
Could someone help me? I'll appreciate any help. Thank you!
I just did this quick test, using
UIManager.put("PopupMenu.border", new LineBorder(Color.RED));
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
UIManager.put("PopupMenu.border", new LineBorder(Color.RED));
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Stuff");
menu.add(new JMenuItem("A"));
menu.add(new JMenuItem("BB"));
menu.add(new JMenuItem("CCC"));
menuBar.add(menu);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setJMenuBar(menuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Now, this may not be desirable, as this will effect ALL popup menus in your program
Updated
I had a look at JMenu and through it's UI delegates and it appears that the popup menu is created within a private method of JMenu called ensurePopupMenuCreated, which would have been an excellent place to inject your custom code.
The method is actually called in a number of different places, but possibly the getPopupMenu is the most accessible
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Stuff") {
private Border border = new LineBorder(Color.RED);
#Override
public JPopupMenu getPopupMenu() {
JPopupMenu menu = super.getPopupMenu();
menu.setBorder(border);
return menu;
}
};
JMenuItem mi = new JMenuItem("Help", 'H');
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.META_MASK));
menu.add(new JMenuItem("A"));
menu.add(new JMenuItem("BB"));
menu.add(new JMenuItem("CCC"));
menu.add(mi);
menuBar.add(menu);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setJMenuBar(menuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
I am adding a second approach. You could use setBorder method:
menuBar.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
This solution has the advantage that you can set the level of thickness. In the example above level of thickness is set to 2.
Also you can use setBorder in many components to paint various borders as in the example below:
First the basic code:
Main class:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
CustomGUI sw = new CustomGUI();
sw.setVisible(true);
});
}
}
CustomGui.class:
import java.awt.*;
import javax.swing.*;
public class CustomGUI extends JFrame {
public CustomGUI() {
super("Simple example");
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setSize(500,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.DARK_GRAY);
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("menu B");
menuBar.add(menu1);
JMenuItem item = new JMenuItem("A text-only menu item");
menu1.add(item);
JMenu menu2 = new JMenu("menu B");
menuBar.add(menu2);
JMenuItem item2 = new JMenuItem("A text-only menu item");
menu2.add(item2);
add(menuBar);
}
}
Now by adding line:
menuBar.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
in CustomGUI() we get:
Now if we want to add border to jmenu's (which is what the OP asked) as well add:
menu1.getPopupMenu().setBorder(BorderFactory.createLineBorder(Color.BLUE, 4));
menu2.getPopupMenu().setBorder(BorderFactory.createLineBorder(Color.BLUE, 4));
(as you can see in the image i didn't set border for menubar but this could be done as described above using:
menuBar.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
And also I added another item just for the menu to be bigger and easier to see the border...)

How can I switch focus to a canvas?

I have this class and I want to switch focus to the Game class right after it was invoked. I might've not understand the purpose of focus but when I press start I have to click on the game canvas itself so I can use the keyboard . In other words: How can I make it so I don't have to click on it to use the keyboard?
package com.runner.panels;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import com.runner.main.Game;
import com.runner.main.Main;
public class PlayPanel extends JPanel{
private static final long serialVersionUID = 1L;
public PlayPanel(){
//setting the layout of the playpanel to null
setLayout(null);
//setting up the info panel : high score, meters ran, pause button etc...
JPanel info = new JPanel();
info.setBounds(0,0,1200,50);
add(info);
//back button
JButton back = new JButton("Back");
info.add(back);
Game game = new Game();
game.setBounds(0,50,1200,521);
game.setBackground(Color.black);
add(game);
back.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) Main.mainp.getLayout();
cl.show(Main.mainp, "Menu");
}
});
}
}
Off topic: (kinda)
The fact that you are doing Main.mainp.getLayout();, calling the panel statically tells me you have poor design and should be looking into other options like an Model-view-controller pattern, an Observer pattern, or at the very least passing a reference of of the Main to the panel, instead of using static objects/calls.
Back on topic
Sounds like a common KeyListener problem. Generally to gain focus you call requestFocusInWindow(). But you still have to worry about other components stealing the focus way after the fact.
I would instead recommend using Key Bindings instead of KeyListener. You have more control over the focus. For instance, by using
InputMap im = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("SPACE"), "hitSpace");
panel.getActionMap().put("hitSpace", new AbstractAction(){
public void actionPerformed(ActionEvent e) {
// do something.
}
});
The panel will have immediate access to the action once you show it from the CardLayout. If you happen to use any other components that would steal the focus away from the panel, the action is still accessible because of the WHEN_IN_FOCUSED_WINDOW input map
Here's a simple example. Type A if it is on panel A, you will see it print. If you type B, it won't print because panel A is in the window. Also if you try and press the button in the panel to steal the focus, you can still type and it will still print. Same goes for panel B
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class Main {
CardLayout layout = new CardLayout();
JPanel panel = new JPanel(layout);
JPanel p1 = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
JPanel p2 = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
JButton b1 = new JButton("panelA");
JButton b2 = new JButton("panelB");
public Main() {
addKeyBind(p1, "pressA", "A");
addKeyBind(p2, "pressB", "B");
p1.add(new JButton("Button for Panel A"));
p2.add(new JButton("Button for Panel B"));
panel.add(p1, "panelA");
panel.add(p2, "panelB");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
show("panelA");
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
show("panelB");
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(b1);
buttonPanel.add(b2);
JFrame frame = new JFrame();
frame.add(panel);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void show(String panelName) {
layout.show(panel, panelName);
}
private void addKeyBind(JComponent comp, String name, final String stroke) {
InputMap im = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(stroke), name);
comp.getActionMap().put(name, new AbstractAction(){
public void actionPerformed(ActionEvent e) {
System.out.println(stroke + " pressed");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Main();
}
});
}
}
Take some time to go over the link I gave you for Key Bindings to learn more.
Back off-topic
Take a look at #AndrewThompson's comment about the null layouts. Learn how to use the LayoutManagers
I believe your problem would be fixed if you add
setFocusable(true);
to your PlayPanel constructor (works for me).
Also, if you want a specific Panel in your GUI to have focus when you start your application, follow the link in the comment of "user3218114", as this will explain how to implement this functionality with Listeners.
Good luck!
You don't need to do anything. The container should be focusable for that.
Here is a code to demonstrate.
package one;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
public class PlayPanel extends Canvas {
public static void main(String... args) {
PlayPanel p = new PlayPanel();
p.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
p.msg = "Focus gained";
p.repaint();
}
#Override
public void focusLost(FocusEvent e) {
p.msg = "Focus Lost";
p.repaint();
}
});
p.setBackground(Color.GRAY);
JFrame f = new JFrame();
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setLocation(300, 300);
f.setVisible(true);
}
String msg = "NO FOCUS";
public void paint(Graphics g) {
g.drawString(msg, 50, 50);
}
}

how to add images to a panel at any location (x,y)

i want to add images or labels with image in it to a panel at any location which will be decided by the user clicking on the panel(add image where user clicks in the panel).
how to do this.
thanks
try (and tweak) this sscce:
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JLabelOnClick extends JFrame {
public static void main(String[] args) {
final JFrame frame = new JLabelOnClick();
final JPanel panel = new JPanel();
panel.setLayout(null);
frame.setContentPane(panel);
frame.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
// if you want an image instead, use the JLabel(Icon image)
// constructor
JLabel label = new JLabel("test");
label.setBounds(e.getX(), e.getY(), label.getPreferredSize().width, label
.getPreferredSize().height);
panel.add(label);
panel.validate();
frame.repaint();
}
});
frame.setSize(new Dimension(200, 200));
frame.setVisible(true);
}
}
set null as LayoutManager for that panel and then manually set position for each image.

Categories