Avatar billede sickenbow Nybegynder
22. september 2003 - 16:06 Der 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);

...

if(e.getSource() == menuItem013)
{
htmlTextArea.replaceSelection("frankfrickytastic");
}


but that wount work =(
I tried with System.out.println("text") too and that did work...

-[Code]

if(e.getSource() == menuItem013)
{
System.out.println("Wazzup")
}

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.

Thanks guys!
Avatar billede fredand Forsker
23. september 2003 - 11:00 #1
Hola!

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?

Best regards
Fredrik
Avatar billede sickenbow Nybegynder
23. september 2003 - 18:04 #2
Hi fredrik....
thank you for responding!

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).

Thank you!
Avatar billede sickenbow Nybegynder
23. september 2003 - 18:05 #3
Avatar billede mikkelbm Nybegynder
23. september 2003 - 22:23 #4
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");
      }

  }
}


Håber du kan bruge det...
Avatar billede sickenbow Nybegynder
24. september 2003 - 15:08 #5
Hi!

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?
Avatar billede mikkelbm Nybegynder
24. september 2003 - 15:18 #6
This little example do what you are asking for.
I hope it's useful...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MenuTest extends JFrame

      private JMenuItem hejsa;
      private JMenuItem farvel;

    private JTextArea textArea = new JTextArea();

      public MenuTest()
      {     
      final int DEFAULT_FRAME_WIDTH = 300;
          final int DEFAULT_FRAME_HEIGHT = 300;
          setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
       
        addWindowListener(new WindowCloser());
         
          Container contentPane = getContentPane();
        contentPane.add(textArea);           
          // construct menu
     
          JMenuBar menuBar = new JMenuBar();
          setJMenuBar(menuBar);
     
          JMenu fileMenu = new JMenu("Text");
          menuBar.add(fileMenu);
     
          MenuListener listener = new MenuListener();
     
          hejsa = new JMenuItem("Hejsa");     
          fileMenu.add(hejsa);
          hejsa.addActionListener(listener);
         
          farvel = new JMenuItem("Farvel");     
          fileMenu.add(farvel);
          farvel.addActionListener(listener);
     
      }
 
 
      private class MenuListener implements ActionListener
      { 
            public void actionPerformed(ActionEvent event)
          { 
     
            Object source = event.getSource();
       
            if (source == hejsa)
            {
              textArea.setText("Hello World");
            }

            else if (source == farvel)
            {
                textArea.setText("Goodbye World");
            }
          }
      }

      private class WindowCloser extends WindowAdapter
      { 
          public void windowClosing(WindowEvent event)
          { 
              System.exit(0);
          }
      }
     
      public static void main(String[] args)
      {
          new MenuTest().show();
      }
}
Avatar billede Ny bruger Nybegynder

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester