jmenu

how to get the name of a JMenu when a JMenuItem is clicked

依然范特西╮ 提交于 2019-12-10 11:45:34
问题 How would one get the name of the JMenu holding a clicked JMenuItem? I tried doing this: public void actionPerformed(ActionEvent arg0) { JMenu menuthing = (JMenu)(arg0.getSource()); String menuString = menuthing.getText(); JMenuItem source = (JMenuItem)(arg0.getSource()); String colorType = source.getText(); But it gives me this error: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JMenuItem cannot be cast to javax.swing.JMenu So is there a way to cast to

JComboBox on a JPopupMenu

Deadly 提交于 2019-12-08 18:43:28
问题 I'm trying to use a compound Swing component as part of a Menu. Everything works just fine, apart from one detail: The component contains JComboBox es and whenever the user clicks on one of them to open its dropdown, the dropdown opens but the menu disappears. Is it possible to make the menu stay open when a JComboBox is clicked? I sub-classed JMenu . This is the corresponding code: public class FilterMenu extends JMenu { public FilterMenu(String name) { super(name); final JPopupMenu pm =

Java repainting a component at mouse-over.

蓝咒 提交于 2019-12-07 16:44:53
问题 I am attempting to make my first properly customised GUI, but I am having difficulty changing the image which has been painted for a component. Basically, for my exitButton (a JMenu) I overwrite the paint method, and then add a Mouse listener, but I am not sure how to repaint the image within the mouseListener interface in the Mouse entered method and again in the mouse exited method. Essentially I am looking for a way to repaint the image, but I am lost as to what I can do. Any help would be

How to set a size for JMenuItem?

蹲街弑〆低调 提交于 2019-12-07 15:29:36
问题 As you can see, it's ugly to have these kind of JMenuItem s. The width of the menu items is quite small. Here is the code: JMenu menuOne=new JMenu("MenuOne"); JMenu menuTwo=new JMenu("MenuTwo"); JMenu menuThree=new JMenu("MenuThree"); JMenu menuFour=new JMenu("MenuFour"); JMenuBar mbar=new JMenuBar(); //add the menu to menubar mbar.add(menuOne); JMenuItem OneItOne=new JMenuItem("1"); JMenuItem OneItTwo=new JMenuItem("2"); menuOne.add(OneItOne); menuOne.addSeparator(); menuOne.add(OneItTwo);

Java Swing - Add leniency when selecting items in submenus

送分小仙女□ 提交于 2019-12-07 03:05:30
问题 When attempting to click on an item in a submenu, it is natural to quickly draw your mouse across the menu items below it. Both Windows and Mac natively handle this by putting a small delay before the a menu is opened. Swing JMenus do not handle this, and the menu the mouse briefly hovers over would be opened before the mouse reaches the intended menu item. For example, in the image below, if I tried to select Item 3 , but in the process my mouse briefly slid across Menu 2 , the Menu 1

How to set a size for JMenuItem?

核能气质少年 提交于 2019-12-06 01:59:42
As you can see, it's ugly to have these kind of JMenuItem s. The width of the menu items is quite small. Here is the code: JMenu menuOne=new JMenu("MenuOne"); JMenu menuTwo=new JMenu("MenuTwo"); JMenu menuThree=new JMenu("MenuThree"); JMenu menuFour=new JMenu("MenuFour"); JMenuBar mbar=new JMenuBar(); //add the menu to menubar mbar.add(menuOne); JMenuItem OneItOne=new JMenuItem("1"); JMenuItem OneItTwo=new JMenuItem("2"); menuOne.add(OneItOne); menuOne.addSeparator(); menuOne.add(OneItTwo); mbar.add(menuTwo); mbar.add(menuThree); mbar.add(menuFour); setJMenuBar(mbar); Simply adding some blanks

Java repainting a component at mouse-over.

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 01:44:59
I am attempting to make my first properly customised GUI, but I am having difficulty changing the image which has been painted for a component. Basically, for my exitButton (a JMenu) I overwrite the paint method, and then add a Mouse listener, but I am not sure how to repaint the image within the mouseListener interface in the Mouse entered method and again in the mouse exited method. Essentially I am looking for a way to repaint the image, but I am lost as to what I can do. Any help would be greatly appreciated. Here is the relevant code snippet: exitBtn = new JMenu(){ @Override protected

How to prevent JMenuItem from closing Menu upon clicking the JMenuItem

折月煮酒 提交于 2019-12-06 00:56:37
How do I prevent a JMenuItem from closing the menu when the JMenuItem is clicked? The JMenuItem is enabled. So this is the scenario, I have 3 JMenuItems : JMenuItem: A, B, C ; C displays an integer X . A and B are used to increment or decrement X by a value of 1. If A or B is clicked, the default nature is that the menu will close upon click. I want to be able to repeatedly click A or B and have the menu remain up, and perform the associated 'action' upon each click. Thanks! First, using a menu to do this may be the wrong approach. JSpinner seems more appropriate. However, to do this with a

Java Swing - Add leniency when selecting items in submenus

强颜欢笑 提交于 2019-12-05 06:29:01
When attempting to click on an item in a submenu, it is natural to quickly draw your mouse across the menu items below it. Both Windows and Mac natively handle this by putting a small delay before the a menu is opened. Swing JMenus do not handle this, and the menu the mouse briefly hovers over would be opened before the mouse reaches the intended menu item. For example, in the image below, if I tried to select Item 3 , but in the process my mouse briefly slid across Menu 2 , the Menu 1 submenu would disappear before I got to it. Does anyone have any tips or suggestions for getting around this?

Can JTable cell edit consume key strokes?

自闭症网瘾萝莉.ら 提交于 2019-12-04 22:17:37
In my program, there is a JMenu with many accelerators, and a JTable that is editable. Problem is, when editing the JTable, the accelerator keys still get triggered. E.g., if I enter the letter 'n' into the jtable cell, the 'next' menu option fires as well. How would one go about making the jtable cell editor consume the keystrokes exclusively? import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JTable; import javax.swing.KeyStroke; import java.awt.event.KeyEvent; import java.awt.Toolkit; public class SampleClass{