22. september 2003 - 16:06Der er
5 kommentarer og 1 løsning
JMenuItem Problem
Hi guys!
I have a problem that I hope you guys can solve or at least try ;)
I need to set an ActionListener on a JMenuItem and later, when the user picks that item, the selected value in the textarea will be replaced by the new one that the item carries.
-[The Code]
JMenuItem menuItem013
...
menuItem013 = new JMenuItem("HTML"); menuItem013.addActionListener(this); submenu.add(menuItem013);
It is kinda' trange that replaceSelection doesn't work with menuItems. I does though work with JMenuButtons. I did the same thing with JMenuButton and did work, but it does not work with JMenuItem.
Please guys, can you try to solve this problem? If you have an other way of solving this problem, please tell me. I am open for new suggessions.
Hos Computerworld it-jobbank er vi stolte af at fortsætte det gode partnerskab med folkene bag IT-DAY – efter vores mening Danmarks bedste karrieremesse for unge og erfarne it-kandidater.
I think you need to give us some more code, and tell us more what doesn't work and what works. An I guess that you talk about JButton , couse JMenuButton doesn't exist, or am I wrong?
The link below will take you to my program. I hope that you can solve the problem that way. What I want to do is: I want to put an ActionListener on the JMenuItem(s). When the user picks that menu item, a specific text will be inserted into the text are (for that i use replaceSelection).
Det her er én måde at add'e en actionlistener til en menu...
MenuListener listener = new MenuListener();
menuItem01 = new JMenuItem("Nytt"); menuItem01.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, actionEvent.CTRL_MASK)); menuItem01.addActionListener(listener); <<- her add'er du en actionlistener. menu.add(menuItem01);
menuItem02 = new JMenuItem("Öppna"); menuItem02.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, actionEvent.CTRL_MASK)); menuItem02.addActionListener(listener); <<- her add'er du en actionlistener. menu.add(menuItem02);
......
private class MenuListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object source = event.getSource();
if (source == menuItem01) { htmlTextArea.replaceSelection("frankfrickytastic"); }
if (source == menuItem02) { htmlTextArea.replaceSelection("hejsa"); }
It didn't really work this time either =(. I wonder, could it have something to do with focus, I mean when I click on the menu, the textarea looses the focus...i don't know, and maby because of that the text can't be inserted. What do you think?
private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } }
public static void main(String[] args) { new MenuTest().show(); } }
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.