Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How can i use a local image for main menu, instead of remote one

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can i use a local image for main menu, instead of remote one

    Hi Guys,

    I am providing the code i wrote for java swing.In it the images for creating main menu is taken from net(via URL).
    How can i use a local image , instead of remote one.

    Code :



    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingConstants;
    import java.awt.image.BufferedImage;
     
    public class MenuTest {
     
        public static void main(String[] argv) throws Exception {
            // Create the menu bar
            JMenuBar menuBar = new JMenuBar();
     
            // Create a menu
            JMenu menu = new JMenu("File");
            BufferedImage image = ImageIO.read(new URL("http://www.dllc.org/thumbnail.aspx?path=%2FContent%2F10923%2Fthumbnails%2F331827-thumbnail.jpg&width=40&height=40"));
            menu.setHorizontalTextPosition(SwingConstants.CENTER);
            menu.setVerticalTextPosition(SwingConstants.BOTTOM);
            menu.setIcon(new ImageIcon(image));
            menuBar.add(menu);
     
     
     
     
     
             JMenu menu1 = new JMenu("View");
            BufferedImage image1 = ImageIO.read(new URL("http://www.gettyicons.com/free-icons/119/sleek-xp-software/png/48/compupic_48.png"));
            menu1.setHorizontalTextPosition(SwingConstants.CENTER);
            menu1.setVerticalTextPosition(SwingConstants.BOTTOM);
            menu1.setIcon(new ImageIcon(image1));
            menuBar.add(menu1);
     
     
     
     
     
            // Create a menu item
            JMenuItem item = new JMenuItem("Test Item");
            JMenuItem new_item=new JMenuItem("New");
            JMenuItem save=new JMenuItem("Save");
            JMenuItem open=new JMenuItem("Open");
     
            menu.add(new_item);
            menu.add(item);
            menu.add(open);
            menu.add(save);
     
            JFrame frame = new JFrame();
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            frame.setJMenuBar(menuBar);
            frame.setSize(500, 550);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
     
        }
    }



    Screen Shot

    img_Menu.jpg


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i use a local image for main menu, instead of remote one

    Got my answer !

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 01:09 PM
  2. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  3. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM
  4. Relay data from remote port to local port
    By chegers in forum Java Networking
    Replies: 0
    Last Post: November 7th, 2010, 11:46 AM
  5. Replies: 1
    Last Post: October 23rd, 2009, 03:17 PM