Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add icon and text in JMenu in swing

I am working on swings. I have to create a mainscreen consisting 10 menus. I have created the form by JFrame and menu by JMenu. Now that menu should contain text and image both and action performed method should be called on clicking of the menu so that action could be performed on clicking of the menu button.

Right now I'm making Jmenu with JMenuItem and icon on image icon but I want the menu with icon and text

my current code is

public class MenuScreen  extends JFrame implements ActionListener{

             Container cp;
             JMenuBar menuBar;

             JLabel logo;
                public MenuScreen() {
                    super("");
                    cp=this.getContentPane();
                    cp.setBackground(Color.gray);

                    menuBar= new JMenuBar();
        logo=new JLabel(im);
                cp.add(logo);
                    logo.setBounds(100,80,500,350);

            helpmenu= new JMenu(" Help  ");
        homemenu=new JMenu(" Home  ");
        fieldsmenu= new JMenu(" Fields  ");
        backmenu= new JMenu(" Back  ");
        forwardmenu= new JMenu(" Forward  ");
        panelmenu= new JMenu(" Panel  ");
        searchmenu= new JMenu(" Search  ");
        quickmenu= new JMenu(" Quick  ");
        infomenu= new JMenu(" Info  ");
        exitmenu= new JMenu(" Exit  ");
        mastermenu= new JMenu(" Master  ");
        tarrifmenu= new JMenu(" Tarrif  "); 
        contactmenu= new JMenu(" Contact  ");
        webmenu= new JMenu(" Web  ");
        wordmenu= new JMenu(" Word  ");
        legaldictionarymenu= new JMenu(" LegalDictionary  ");
        budgetmenu=new JMenu(" Budget 2012 2013  ");
        memberdetailmenu= new JMenu(" Member Details  ");


        Font f1= new Font("Arial",Font.BOLD,16);

        budgetmenu.setFont(f1);
        legaldictionarymenu.setFont(f1);
        helpmenu.setFont(f1);

        JMenuItem backmenuitem= new JMenuItem(backicon);
        backmenu.add(backmenuitem);


        JMenuItem exitmenuitem= new JMenuItem(exiticon);
        exitmenu.add(exitmenuitem);

        menuBar.add(helpmenu);
        menuBar.add(homemenu);
        menuBar.add(fieldsmenu);
        menuBar.add(backmenu);
        menuBar.add(forwardmenu);
        menuBar.add(panelmenu);
        menuBar.add(searchmenu);
        menuBar.add(quickmenu);
        menuBar.add(infomenu);
        menuBar.add(exitmenu);
        menuBar.add(mastermenu);
        menuBar.add(tarrifmenu);
        menuBar.add(contactmenu);
        menuBar.add(webmenu);
        menuBar.add(wordmenu);
        menuBar.add(legaldictionarymenu);
        menuBar.add(budgetmenu);
        menuBar.add(memberdetailmenu);

        setJMenuBar(menuBar);

    /*
     Adding Listeners to the menus where required 

     */
            searchmenu.addActionListener(this);

    }

    public void actionPerformed(ActionEvent ae)
    {
     JOptionPane.showMessageDialog(null,"clicked");
          if(ae.getActionCommand().equals("Search"))
          {

           SearchForm frm=new SearchForm();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          frm.setBounds(0,0,screenSize.width, screenSize.height);
          frm.setVisible(true);


          }           

    }

   public static void main(String args[])
{

       MenuScreen frm= new MenuScreen();

       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
       frm.setBounds(0,0,screenSize.width, screenSize.height);
       frm.setVisible(true);
}
}

please help me.

like image 255
ADESH RAJPUT Avatar asked Dec 05 '22 14:12

ADESH RAJPUT


2 Answers

Use the constructor JMenuItem(String text, Icon icon)
If you want JMenu with Icon and Text do

helpmenu= new JMenu(" Help  ");
helpmenu.setIcon(..);
like image 111
basiljames Avatar answered Dec 12 '22 07:12

basiljames


- For the JMenuItem use JMenuItem((String text, Icon icon) constructor,

- And for JMenu try using the constructor which takes String as an Argument JMenu(String s), and method setIcon()

For setting the special key options on the MenuItems use setAccelerator()

Eg:

myItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));

like image 24
Kumar Vivek Mitra Avatar answered Dec 12 '22 08:12

Kumar Vivek Mitra