Mnemonic on JMenuItem
I'm lost. Any idea why alt+a shortcut for my JMenuItem doesn't work?
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainClass {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menu = new JMenuBar();
menu.add(new JMenuItem(new ShowAction()));
frame.add(menu);
frame.setSize(350, 150);
frame.setVisible(true);
class ShowAction extends AbstractAction {
public ShowAction() {
super("fire!");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_A));
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("fired");
}
1) The mnemonic should be a character in the menu item text, maybe VK_F.
2) a JMenuItem is added to a JMenu which is added to a JMenuBar, which is added to the frame by using frame.setJMenuBar(...). Read the section from the Swing tutorial on [url http://download.oracle.com/javase/tutorial/uiswing/components/menu.html]How to Use Menus.
3) Then the mnemonic is activated only when the menu is opened.
Maybe you really want to use an "accelerator", which allows you to invoke the Action without opening the menu.
Or you can use "Key Bindings". The tutorial link from above also has a section on this topic.
Similar Messages
-
JMenuItem Tooltip and Action.MNEMONIC
Hello,
when I create a menu item from an Action and set a mnemonic for the text, the tooltip for that menu item will automatically be extended with an Alt+(Mnemonic Char). This is very annoying if the MenuItem read "Execute F5" and the tooltip says "Execute the statement Alt-E"
Is there a way to turn this off?
Here is my code How I create the JMenuItem
Action a = new MyExecuteAction()
a.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
a.putValue(Action.MNEMONIC, new Integer((int)'e'));
a.putValue(Action.SHORT_DESCRIPTION, "Execute the statement");
JMenuItem i = new JMenuItem();
i.setAction(a);
i.setAccelerator((KeyStroke)a.getValue(Action.ACCELERATOR_KEY));The accelerator key is displayed fine, and that's ok. What I don't want is the Alt-E hint in the tooltip.
Any input appreciated
ThomasThanks a lot.
I have searched the forum before posting but obviously I used the wrong keywords :-)
But interesting enough that the thread (dated May 2001) said this will be fixed in the next release but now it's one year later and JDK 1.4 is still doing it wrong...
Cheers
Thomas -
Alt+mnemonic key is not working properly for Menu Items
Assume there are two menus , File Menu with mnemonic Alt+F
and Save Menu with mnemonic Alt+S. File Menu contains the
menu items like PageSetup with Mnemonic S. Save menu has
the menu item Properties with Mnemonic P.
Pressing Alt+F opens the File Menu which has the menu item
PageSetup.Pressing S activates the PageSetup menu item.
Similarly Pressing Alt+S opens the Save Menu which has the
menu item Properties. Pressing P activates the Properties
menu item acion. But Pressing Alt+P also activates the
Properties menu item action(it is not the desired behaviour)
Pressing Alt+Mnemonic key has to open only the Menus.It
should not consider the menu items.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.Press Alt+F
2.File Menu is invoked
3.Press S
4. Invokes the menu item action
5. Press Alt+S
6. Save menu is invoked
7. Press P
8. Invokes the menu item action
9. Press Alt+P
10. Invokes the menu item action
EXPECTED VERSUS ACTUAL BEHAVIOR :
Pressing of Alt+Mnemonic key invokes the menu and also menu
item actions. If the same mnemonic is present in the Menu
it is giving the priority to Menu only.Pressing of Alt key
alone does not hiding the pull down menu
Expected Result;
Pressing of Alt+Mnemonic key has to list out the pull
down menu only.It should not consider the menu items with
the same mnemonic. If Alt key alone is pressed the pull
down menu has to hide.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaBug extends JFrame
public JavaBug()
// Create menu bar
JMenuBar menuBar = new JMenuBar(); // Create menu bar
setJMenuBar(menuBar); // Add menu bar to window
// Create first option on menuBar
JMenu m1 = new JMenu("File");
m1.setMnemonic('F');
JMenuItem m1o1 = new JMenuItem("PageSetup");
m1o1.setMnemonic('S');
m1o1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("File/PageSetup was selected");
m1.add(m1o1);
// Create second option on menuBar
JMenu m2 = new JMenu("Save");
m2.setMnemonic('S');
JMenuItem m2o1 = new JMenuItem("Properties");
m2o1.setMnemonic('P');
m2o1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Save/Properties was selected");
m2.add(m2o1);
menuBar.add(m1);
menuBar.add(m2);
this.setTitle("Mnemonic Bugs");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
setSize(400,250);
show();
public static void main(String[] args)
JavaBug bug = new JavaBug();
Suggestions welcomed
punniyaI tried your code, but didn't get your "bug" using jdk 1.3 on windows 2000 (at my school).
Kind regards,
Levi -
JMenu Mnemonic in JApplet not working
Hi,
Here is a code in my JApplet, but some how the Mnemonic is not working,
why??
I m using jdk1.4.1
Ashish
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
import java.math.*;
import javax.accessibility.*;
public class TestMenuApplet extends JApplet
public void init()
buildMenu();
private void buildMenu()
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File", true);
JMenu format = new JMenu("Format", true);
file.setMnemonic(KeyEvent.VK_F);
format.setMnemonic(KeyEvent.VK_O);
JMenuItem back = new JMenuItem("Back");
JMenuItem close = new JMenuItem("Close");
JRadioButtonMenuItem single =
new JRadioButtonMenuItem("Single");
JRadioButtonMenuItem multi =
new JRadioButtonMenuItem("Multiple", true);
ButtonGroup row = new ButtonGroup();
row.add(single);
row.add(multi);
JMenu monthly = new JMenu("Monthly");
JMenuItem one = new JMenuItem("One", KeyEvent.VK_O);
JMenuItem two = new JMenuItem("Two");
JMenuItem test = new JMenuItem("Test");
menuBar.add(file);
menuBar.add(format);
file.add(back);
file.add(close);
format.add(single);
format.add(multi);
format.addSeparator();
format.add(monthly);
format.add(test);
monthly.add(one);
monthly.add(two);
this.setJMenuBar(menuBar);
My HTML
<html>
<HEAD>
<TITLE>test</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="-1">
</HEAD>
<BODY >
This is testing of applet
<br>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<OBJECT
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = "300" HEIGHT = "300"
codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4-win.cab#Version=1,4,0,0">
<PARAM NAME = CODE VALUE = "TestMenuApplet.class" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.4">
<PARAM NAME="scriptable" VALUE="false">
<COMMENT>
<EMBED
type="application/x-java-applet;version=1.4"
CODE = "TestMenuApplet.class"
WIDTH = "300"
HEIGHT = "300"
scriptable=false
pluginspage="http://java.sun.com/products/plugin/index.html#download">
<NOEMBED>
</NOEMBED>
</EMBED>
</COMMENT>
</OBJECT>
<!--
<APPLET CODE = "TestMenuApplet.class"
WIDTH = "300"
HEIGHT = "300"
</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
</BODY>
</html>Did you convert your html code to call the sun VM instead of the default browser VM ?
Use the HtmlConverter.exe to do this passing the html file in argument.
Denis -
I want to have icons on my menu. I am using the Windows look and feel.
On most native windows menus the icons appear in a separate column on the left. When I use icons in java menus they appear much too far to the right and the text doesn't align properly with the icons unless I move the text using Insets. There has to be a better way that gets the icons further left and the text aligned. Here is my code...
private void createMenuItem(String text, int mnemon,
KeyStroke accel, JMenu owner, Icon ic)
JMenuItem temp=null;
if (ic == null)
temp = new JMenuItem(text,mnemon);
Insets in = temp.getInsets();
in.left += 20;//icon width is 20
temp.setMargin(in);
else
temp = new JMenuItem();
temp.setHorizontalTextPosition(SwingConstants.RIGHT);
temp.setText(text);
temp.setMnemonic(mnemon);
temp.setIcon(ic);
if (accel != null)
temp.setAccelerator(accel);
temp.addActionListener(this);
if (ic != null) temp.setIcon(ic);
owner.add(temp);
}I have had this problem too. The misaligned text looks terrible.
Here is the solution I found, using an EmptyIcon class :
http://www.javapractices.com/Topic169.cjp
There is an example Swing app implementation on the same site which uses this technique. You may browse the javadoc, which has links to source code :
http://www.javapractices.com/Topic170.cjp
Example of a class in the example app which uses this technique :
http://www.javapractices.com/apps/stocksmonitor/javadoc/stocksmonitor/ui/EditUserPreferencesAction.html
Clicking on a class or method name will link to the source code....
- John -
JMenu, passing mnemonic/ keyevent
Hi
Im building a JMenu but as it has many items im using a function to make each JMenuItem. The function calls the JMenu constructor with the text and the image to be used. I want to pass the mnemonic but can't work out how to do it.
//example of function call
menu1.add(add("myText","myPic.gif"));
//what i need to send to function
menuItem.setMnemonic(KeyEvent.VK_T);also is it possible to do the same with accelerators, ie pass this info to the function
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.ALT_MASK));thanksOkay, it's still not clear to me what you're trying it write, but is it a method like the following?
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class Blank implements ActionListener {
static JMenuItem add(JMenu menu, ActionListener al, String text, int mnemonic, String keyStoke, String iconURL) {
JMenuItem item = new JMenuItem(text);
if (al != null)
item.addActionListener(al);
if (mnemonic != 0)
item.setMnemonic(mnemonic);
if (keyStoke != null)
item.setAccelerator(KeyStroke.getKeyStroke(keyStoke));
try {
if (iconURL != null)
item.setIcon(new ImageIcon(new URL(iconURL)));
} catch (MalformedURLException e) {
e.printStackTrace();
return menu.add(item);
public void actionPerformed(ActionEvent evt) {
System.out.println("your code here");
public static void main(String[] args) {
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("menu");
add(menu, new Blank(), "blank", KeyEvent.VK_K, "control B", "http://forum.java.sun.com/im/ic_eye.gif");
add(menu, null, "zip", 0, null, null);
mb.add(menu);
final JFrame f = new JFrame("Blank");
f.setJMenuBar(mb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
f.setLocationRelativeTo(null);
f.setVisible(true);
} -
Hi everybody,
we're running a swing application with Java 1.4.2_003 under XP. User's can choose between the Platform or the Windows LAF.
Now, we're encountering a strange behaviour under the Platform LAF but NEVER under Windows LAF.
If the user controls the GUI with the keyboard and tries to trigger an action by using the button's mnemonic character. After firing the action, a new Dialog is opened (as expected) but at the same time the following exception is raised:
Exception occurred during event dispatching:
java.lang.NullPointerException
at sun.awt.windows.WInputMethod.dispatchEvent(WInputMethod.java:253)
at sun.awt.im.InputContext.dispatchEvent(InputContext.java:238)
at sun.awt.im.InputMethodContext.dispatchEvent(InputMethodContext.java:180)
at java.awt.Component.dispatchEventImpl(Component.java:3565)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:592)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:141)
at java.awt.Dialog$1.run(Dialog.java:540)
at java.awt.Dialog.show(Dialog.java:561)
at com.cash.ui.delegate.stock.StockDataPanel$NewAction.actionPerformed(StockDataPanel.java:407)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener$ReleasedAction.actionPerformed(BasicButtonListener.java:301)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1530)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2438)
at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:253)
at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:202)
at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2515)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2507)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2401)
at java.awt.Component.processEvent(Component.java:4909)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:592)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)Prior to this action the user has accessed a JMenuItem in the JMenuBar via keyboard without any problems.
Also, as soon as I switch panels, the mnemonic shortcuts don't work at all anymore. If you have any ideas, please post them. Thanks.
WaltPls see that it is fixed in JDK 1.6 which should be brought to JDK 1.5.0 also
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5057174
The workaround is also discussed at
http://forums.sun.com/thread.jspa?threadID=5295077
Which I am yet to try for my code, if it works or not !!! :(
-Ranjan -
Disabled JMenuItem doesn show animated gif
Hi there
I would like to have a popup menu with actions that are enabled after they have finished with some background task taking some time. Basically this works fine for the enabling action on a shown popup. However, adding an animated gif as the icon or disabled icon does not show it in disabled state. In enabled state it works perfect. Please have a try with the sample code. You should see the disabled item for 2 secs and the icon is not showing up. After being enabled, it does. Invoking the menu again shows the animated gif in its last state left, but not moving any more.
I guess, repaints are not done appropriately in disabled state... Any ideas how to solve that would be highly appreciated :-)
Actually I used the icon at http://mentalized.net/activity-indicators/indicators/pascal_germroth/indicator.white.gif
Cheers
Daniel
public class Main
public static void main(String[] args)
final JFrame frame = new JFrame();
frame.addMouseListener(new MouseAdapter()
public void mousePressed(final MouseEvent e)
final JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Open..."));
popup.add(new JMenuItem("Close"));
final JMenuItem action = new JMenuItem("Long loading until enabled");
action.setIcon(new ImageIcon("C:/spinner.gif"));
action.setDisabledIcon(new ImageIcon("C:/spinner.gif"));
popup.add(action).setEnabled(false);
popup.show(e.getComponent(), e.getX(), e.getY());
SwingUtilities.invokeLater(new Runnable()
public void run()
try
Thread.sleep(2000);
action.setEnabled(true);
catch (InterruptedException e1)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}Edited by: daniel.frey on Apr 22, 2009 7:50 AMThe problem is that you are causing the EDT to sleep, which means the GUI can't repaint itself. Read the section from the Swing tutorial on Concurrency to understand what is happening.
-
Dear all,
I downloaded the following program from Sun to create JMenuBar and JManuItem but there is no function. I need to display say 'Hello World' when I click one of the JMenuItem. May I know how to write the function in the following context ?
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
* This class exists solely to show you what menus look like.
* It has no menu-related event handling.
public class MenuLookDemo extends JFrame {
JTextArea output;
JScrollPane scrollPane;
public MenuLookDemo() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JCheckBoxMenuItem cbMenuItem;
JRadioButtonMenuItem rbMenuItem;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
//Add regular components to the window, using the default BorderLayout.
Container contentPane = getContentPane();
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
contentPane.add(scrollPane, BorderLayout.CENTER);
//Create the menu bar.
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//Build the first menu.
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);
//a group of JMenuItems
menuItem = new JMenuItem("A text-only menu item",
KeyEvent.VK_T);
//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menuItem = new JMenuItem("Both text and icon",
new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);
//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
menu.add(rbMenuItem);
//a group of check box menu items
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.setMnemonic(KeyEvent.VK_C);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("Another one");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
menu.add(cbMenuItem);
//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu);
//Build second menu in the menu bar.
menu = new JMenu("Another Menu");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);
public static void main(String[] args) {
MenuLookDemo window = new MenuLookDemo();
window.setTitle("MenuLookDemo");
window.setSize(450, 260);
window.setVisible(true);
}In the same tutorial there is a section titled "Handling Events From Menu Items" which includes sample code:
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#event -
Add Listener to Unknown Number of JMenuItems
How can I add an ActionListener to each JMenuItem by cycling through an unknown number of menu items?
I have a program that prompts the user for a database, then creates a JMenu of table names based on whatever database the user selects.
This means that I don't know the table names or number of tables that will be added to the JMenu.
I need to add an ActionListener to EACH JMenuItem. The problem is that I am only able to select one table from the menu. Subsequent menu clicks (on different table names) have no affect on the output.
Here is an excerpt of my code:
ResultSet rs = md.getTables(null, null, null, types);
while (rs.next()) {
tables.add(rs.getString("table_name"));
for (Iterator i = tables.iterator(); i.hasNext(); )
String table = (String) i.next();
JMenuItem mi = new JMenuItem(table);
j1.add(mi);
mi.addActionListener(this);
[END CODE]
Thanks for any help you can give!I got it. It turns out that I WAS setting the listeners correctly. My additonal output was "hidden" in the window. I found it by pure luck when I expanded the window size.
I added a line to remove all components from the JPanel before adding a new one and this "refresh" worked.
Thanks fot the help anyway! It is greatly appreciated. -
How can I distinguish a JMenuItem , JPopupMenu and TopLevelMenu?
I used the following methods to get the component who (a menu)gains the focus:
JComponent com = JFrmame.getFocusOwner();
or
MenuSelectionManager menuMgr = MenuSelectionManager.defaultManager();
MenuElement[] menuPath = menuMgr.getSelectedPath();
if(menuPath != 0) {
JComponent comp = Array.get(menuPath, menuPath.length-1); // this is the item with the focus
then I need to use getAction() to get the menuItem's action. But how can I filter JMenuItem and JPopUpMenu from TopLevelMenu (JMenuBar item) ?
Thanks!The instanceof may provide the simplest solution, since JPopupMenu, JMenuBar and JMenuItem don't have any kind of ancestor relationship with each other.if (comp instanceof JPopupMenu) {
// Popup
} else if (comp instanceof JMenuItem) {
// Item
} else if (comp instanceof JMenuBar) {
// Bar
} else ...?Hope this helps,
-Troy -
How do I find the parent JFrame of a JMenuItem?
When an ActionEvent occurs from a JMenuItem selection, how can
I get a reference to the JFrame the JMenuItem is on?
ThanksGood afternoon...
it's a pretty good / standard idea to make your event-thread a subclass of the JFrame. i.e.:
public class MySwingApp extends JFrame implements ActionListener {
public MySwingApp() [
super("My App Title");
// ...build your GUI...
// ...register this as your action listener for the menu items...
/* this is in the class that is the JFrame of the application */
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getCommand());
}hope that helps
Schultz -
How can I have a shortcut key that is not a JMenuItem
Hi,
I'm trying to make "ctrl-f" a shortcut key in my program.
I know I can do this as a JMenuItem by:
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK));
However, I would rather not have it in the menu.
How would I do this without having it as a JMenuItem?
Thanks,
Daniel LorimerAs a user I hate designs like this. How am I supposed to know what all the shortcut keys are? Using a menu item is a form of self documentation
Anyway, check out the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings. -
Dynamic addition of JMenuItem in JMenu
I am trying to create a menu for my project. For that i have added the main menus and for each menu there will be menu item. But the problem is i will get the list of menu items for each menu , only at the runtime of my project. i.e. i wabt to add progrramatically add the menu items. Iam really confused with this. I will appriciate if any one help me on this.
And also i want to know ..is that possible to get the mouse listener for menu.
Thank in advance
Regards,
SAthishYou can add and remove menu items from the jmenu dynamically, the only thing you will have to take care of is invoking validate() after each insertion/deletion (it's a bit buggy there). All you have to do is create the items on demand and add (remove) them via the add(JMenuItem) (remove(JMenuItem)) method.
As for your second question, a JMenu is nothing but a JComponent, so you should be able to add a mouse listener like you would with any other component... -
How to use an accented character in a mnemonic ?
Hi all,
I'd like to set the mnemonic of a component (no matter what kind of component) to an accented character such as � / � / � / � / � / �.
but with setMnemonic(char c) it doesn't work.
with setMnemonic(int code), I can't find a virtual key for any of those character in class KeyEvent (KeyEvent.VK_� ??).
Has anyone an idea ?
Thanks.
Have a nice week-end.
Herbien
PS : I know it seems strange to set a mnemonic to a accented character, but I must implement an existing C++ application in Java, and the application must mostly be the same. The users of this application are used to work with the keyboard..... :-(girija_pathak wrote:
How can I know which is the last character? and How to handle it?You use the DUMP() function in SQL in order to see the actual content of the column.
E.g.
select DUMP(bac_person_id) from bkmap_personid_stg where bac_person_id like '%27136317%'
The decimal character values will be displayed - enabling you to see where and what control characters characters exist in the string value for that column.
Maybe you are looking for
-
Family sharing of itunes account
Trying to figure out how to use 1 itunes account but have multiple ipods on the account and also allowing each ipod to have its own email for TextMe. The problem is I'm a mom that has 4 kids that all have ipods. I would prefer to have 1 itunes accoun
-
Hi i have a small doubt in while i was performing a transaction code KSB1 by entering that i got the back ground program in which i saw a statement in the start of program but1 = text-bt1. so now i want to know what is the use of "but" and can u pro
-
I just updated my iPhone and would like to change the color of the bubble for the text reply. Mine is now GRAY and it is much more difficult to read. The outgoing text bubble is still blue. Can I do this?
-
Segmentation Tools with BW datasets
Hello CRM Experts, In our business scenario we would like segment with contact people. In our CRM 4.0 system we use the relationship tab within business partner master data to define the link to the respective contact people. The standard BW object
-
I hate this song that is on my phone and I did not mean to purchase it. How can I be refunded or erase this horrible artist ?