How to import an image in menu bar
Asked Answered
M

2

1

I've created a simple menu bar and I don't know how to import an image in the free space.

My code is below:

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

public class MyMenu extends JFrame {

    JMenuBar menubar;
    JMenu file, edit, contact, quit;
    JMenuItem exit, open, search, delete, registration, informations;

    public MyMenu() {
        setLayout(new FlowLayout());
        //___________________________ FILE __________________________________
        menubar = new JMenuBar();
        setJMenuBar(menubar);
        file = new JMenu("Αρχείο");
        menubar.add(file);
        open = new JMenuItem("Άνοιγμα πελατολογίου");
        file.add(open);
        event e1 = new event(); // Compiler Error
        open.addActionListener(e1);
        //__________________________________ EDIT ____________________________
        edit = new JMenu("Ενέργειες");
        menubar.add(edit);
        search = new JMenuItem("Αναζήτηση");
        edit.add(search);
        registration = new JMenuItem("Καταχώρηση");
        edit.add(registration);
        delete = new JMenuItem("Διαγραφή");
        edit.add(delete);
        //_________________________________ CONTACT __________________________
        contact = new JMenu("Επικοινωνία");
        menubar.add(contact);
        informations = new JMenuItem("Πληροφορίες");
        contact.add(informations);
        //___________________________________QUIT_____________________________
        quit = new JMenu("Έξοδος");
        menubar.add(quit);
        exit = new JMenuItem("Έξοδος");
        quit.add(exit);
        event e = new event(); // Compiler Error
        exit.addActionListener(e);
    }

    public class MyEvent implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }

        public void actionPerformed2(ActionEvent e1) {
            final SimpleTableDemo a = new SimpleTableDemo(); // Compiler Error
            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    a.createAndShowGUI();
                }
            });
        }
    }

    public static void main(String[] args) throws IOException {
        MyMenu gui = new MyMenu();
        gui.getContentPane().add(panel); // Compiler Error
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(2400, 1900);
        gui.setVisible(true);
    }
}

It is related to the class Panel? How can I take advantage of the free space and use an image?

Microbalance answered 24/8, 2011 at 15:44 Comment(5)
Do you have a screen-shot of another app. that does this? BTW - one '?' indicates a question.Stokeontrent
@Andrew Thompson lots of thanks for corrections :-)Kennakennan
BTW - after spending a few minutes attempting to get that code to compile, a few tips. 1) Don't put special characters (e.g. Greek letters) into examples intended for general consumption. 2) Translate language in GUIs into English as best you can, when posting to forums that use English. 3) Don't include 5+ menu items in an example that merely shows your complete inability to do something once. Which brings me to.. 4) For better help sooner, post an SSCCE.Stokeontrent
@Andrew Thompson :-) I can't let it be :-) , really time to swith your IDE to UTF-8, big part of this small world is non-ACSII worlds, and Java is really Excelent for (built-in) supports for all possible Charsets that exists around us, but agreed (and for my endless bad too) this is English ForumKennakennan
"..really time to swith your IDE" My I-D-what? ;) I use TextPad (a really old version as well) as my source editor. For building beyond what can be done from the standard menus of TP, I use Ant (and have defined a short-cut to invoke it). As my excuse ..well I won't even try and justify it, that will just get people pointing and laughing.Stokeontrent
K
4

please post here Runnable code without Errors from Java Compilator (marked in you code with // Compiler Error) before any of your request for AddingImage / ImportImage in JMenuBar

required tutorials

1) JMenu, JMenuBar, JMenuItems

2) Laying Out Components Within a Container

3) How to Write an Action Listener

4) How to Use Icons

5) and finally for set Image as JMenuBar BackGround you have to read something about 2D Graphics

6) examples on this forum, here or here

Kennakennan answered 24/8, 2011 at 16:3 Comment(1)
be sure 1st. of OP's edits was by my person, just put that together as an answer :-)Kennakennan
H
4

A JMenuItem can be constructed in order to display an image.

ImageIcon icon = new ImageIcon("path_to_your_image");
JMenuItem item = new JMenuItem(icon);
Hearst answered 24/8, 2011 at 15:47 Comment(5)
I have to define an actiun in JMenuItem or just to open an image there?Microbalance
@snake plissken: If you need an action you can also use it. That's not mandatory anyway.Hearst
I tried it iside the constructor. However the image never opened. Basically i want just to have as a background one picture.Microbalance
Thanks for your help guys i found solution. I ve just create a JLabel and put it there my image. I dont clearly understand what exactly is the JLabel but nevermind i did my job. Thanks a lot.Microbalance
@snake plissken - actually, no: if you dont understand how/why your code is doing what you expect you did not do your job (which is to create stable, robust, maintainable, ...) software.Fisticuffs
K
4

please post here Runnable code without Errors from Java Compilator (marked in you code with // Compiler Error) before any of your request for AddingImage / ImportImage in JMenuBar

required tutorials

1) JMenu, JMenuBar, JMenuItems

2) Laying Out Components Within a Container

3) How to Write an Action Listener

4) How to Use Icons

5) and finally for set Image as JMenuBar BackGround you have to read something about 2D Graphics

6) examples on this forum, here or here

Kennakennan answered 24/8, 2011 at 16:3 Comment(1)
be sure 1st. of OP's edits was by my person, just put that together as an answer :-)Kennakennan

© 2022 - 2024 — McMap. All rights reserved.