Working with Image Menus and files in Java Swings

Working with Images Menus and files in Java Swings

In this article, I am going to discuss Working with Images Menus and Files in Java Swings with Examples. Please read our previous article, where we discussed the Swing Dialog boxes in Java with Examples. At the end of this article, you will understand the following pointers in detail.

  1. Working with Images in Java Swings
  2. Working with Menus in Java Swings
  3. Working with Files in Java Swings
Working with Images in Java Swings:

To display an image on the window or to add an image as a background of the UI component, the Swing package provides ImageIcon class.

Steps to display an image on the window:
  1. Create an empty label component.
  2. Create an image icon class object and which encapsulates a given image location.
  3. Finally, add this image to the label component.
Sample Program Working with Images in Java Swings:
import javax.swing.*;
public class ImageDemo1
{
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        jf.setSize (500, 500);
        JLabel j1 = new JLabel ();
        ImageIcon ob = new ImageIcon ("G:/JAVA/Demo/image.jpeg");
        j1.setIcon (ob);
        jf.add (j1);
        jf.setVisible (true);
    }
}

Output:

Working with Images in Java Swings

Sample Program to set Image as a Background
import javax.swing.*;
public class ImageDemo2 extends JFrame
{
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        jf.setSize (100, 100);
        ImageIcon ob = new ImageIcon ("G:/JAVA/Demo/image.jpeg");
        JButton jb = new JButton ("Click", ob);
        jf.add (jb);
        jf.setVisible (true);
    }
}

Output:

Working with Image Menus and files in Java Swings

In the above application, we added the image as background to the button component and this will be done using JButton class constructor.

JButton(buttonname, ImageIcon object)

This constructor is responsible to create a button component as well as it adds an image on the background of the button component.

Note: Swing allows us to add an image as a background of any UI Component.

Working with Menu JMenu, JMenuBar, JMenuItem

The menu is a UI component that was used whenever if we have more number of items as per the hierarchy. Menu reduces space in the UI component and it increases a look of UI component.

Steps to create a menu:
  1. Create a menu component
  2. Create a MenuItem components
  3. Add MenuItems to Menu Component
  4. Create a MenuBar Component
  5. Add all menus to menu bar component
  6. Finally, add the MenuBar component to the window.
JMenuBar

It is used to create a MenuBar which can have several menus. This component is used to display menubar on the window or frame.

Declaration: public class JMenuBar extends JComponent implements MenuElement, Accessible

Syntax: JMenuBar jmb = new JMenuBar();

JMenu

This component is used to create a pull-down menu which can contain some menu item. It basically inherits the JMenuItem class. We can add the menu to the menu bar by using the add method.

Example : mbar.add(FileMenu)

Declaration: public class JMenu extends JMenuItem implements MenuElement, Accessible

Syntax: JMenu FileMenu = new JMenu(“File”);

JMenuItem

This component is used to create menu items that can be placed on the menu. It is used to add a simple labeled menu item. The items basically used in a menu must belong to the JMenuItem or any of its subclasses. We can add the menu item to the menu by using the add method.

Example: FileMenu.add(newItem)

Declaration: public class JMenuItem extends AbstractButton implements Accessible, MenuElement

Syntax: JMenuItem newItem = new JMenuItem(“New”);

Sample Program Working with Menu JMenu, JMenuBar, JMenuItem
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuDemo
{
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        jf.setSize (250, 250);
        JMenu jm1 = new JMenu ("File");
        JMenu jm2 = new JMenu ("Edit");
        JMenuItem j1 = new JMenuItem ("Open");
        JMenu jm3 = new JMenu ("Choose");
        JMenuItem j2 = new JMenuItem ("Java");
        MyListener4 ob = new MyListener4 ();
        j2.addActionListener (ob);
        JMenuItem j3 = new JMenuItem ("C");
        j3.addActionListener (ob);
        JMenuItem j4 = new JMenuItem ("C++");
        j4.addActionListener (ob);
        jm3.add (j2);
        jm3.add (j3);
        jm3.add (j4);
        jm1.add (j1);
        jm1.add (jm3);

        JMenu jm4 = new JMenu ("Edit");
        JMenuItem j5 = new JMenuItem ("Cut");
        JMenuItem j6 = new JMenuItem ("Copy");
        jm4.add (j5);
        jm4.add (j6);

        JMenuBar jb = new JMenuBar ();
        jb.add (jm1);
        jb.add (jm4);

        jf.add (jb, BorderLayout.NORTH);
        jf.setVisible (true);
    }
}

class MyListener4 implements ActionListener
{
    public void actionPerformed (ActionEvent ae)
    {
        String itemname = ae.getActionCommand ();
        if (itemname.equals ("Java"))
            System.out.println ("Java Selected");
        else if (itemname.equals ("C"))
            System.out.println ("C Selected");
        else
            System.out.println ("C++ Selected");
    }
}

Output:

Working with Menu JMenu, JMenuBar, JMenuItem

FileChooser Dialog box : JFileChooser

Swing package provides a readymade file chooser box that was used for browsing the file and saving the file in the selected location. JFileChooser class is used for creating a file chooser dialog box. It basically inherits JComponent class.

Declaration : public classJFileChooser extends JComponent implements Accessible

Syntax : JFileChooser fc = new JFileChooser();

JFileChooser Constructor
  1. JFileChooser(): It is used to construct a JFileChooser pointing to the user’s default directory.
  2. JFileChooser(File currentDirectory): It is used to construct a JFileChooser using the given File as the path.
  3. JFileChooser(String currentDirectoryPath): It is used to construct a JFileChooser using the given path.

This class provides two types of methods:

  1. showOpenDialog(parentwindow)
  2. showSaveDialog(parentwindow)
Sample Program to understand JFileChooser in Java Swings.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class JFileChooserDemo
{
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        FlowLayout ob1 = new FlowLayout ();
        jf.setLayout (ob1);
        jf.setSize (300, 300);
        JButton jb1 = new JButton ("Choose Dialog");
        JButton jb2 = new JButton ("Save Dialog");
        jf.add (jb1);
        jf.add (jb2);
        MyListener5 m1 = new MyListener5 ();
        jb1.addActionListener (m1);
        jb2.addActionListener (m1);
        jf.setVisible (true);
    }
}

class MyListener5 implements ActionListener
{
    public void actionPerformed (ActionEvent ae)
    {
        String name = ae.getActionCommand ();
        if (name.equals ("Choose Dialog"))
        {
         JFileChooser jf = new JFileChooser ();
         int i = jf.showOpenDialog (null);
         System.out.println (i);
         if (i == 0)
         {
             File f1 = jf.getSelectedFile ();
             System.out.println (f1.getName ());
             System.out.println (f1.getAbsolutePath ());
         }
        }
        else
        {
         JFileChooser jf = new JFileChooser ();
         int i = jf.showSaveDialog (null);
         System.out.println (i);
        }
    }
}

Output:

Program to understand JFileChooser in Java Swings

If you will click on “Choose Dialog” button, you will get the following option:

Working with Files in Java Swings

If you will click on the “Save Dialog” button, you will get the following option:

Working with Images, Menus and Files in Java Swings

In the next article, I am going to discuss Working with Tables and Progress Bars in Java Swings with examples. Here, in this article, I try to explain Working with Images, Menus, and Files in Java Swings and I hope you enjoy this Working with Images, Menus, and Files in Java Swings article.

Leave a Reply

Your email address will not be published. Required fields are marked *