Let's deep dive into GUI program: Java Swing for creating window based application.

What will you learn?

  1. GUI program with Java Swing: Check Palindrome, Reverse the String, and find vowel characters in a string
  2. JMenuBar and JPanel in Java with programming example

GUI Form with Java Swing: Check Palindrome, Reverse the String, and find vowel characters in a string

Create a GUI program with a swing that includes a text field, a text label with the input message "Input any String," and three buttons with the captionCheckPalindrome, Reverse, and FindVowels. Write a complete program for the above scenario and for checking the palindrome in the first button, reverse it after clicking the second button, and extract the vowels from it after clicking the third button.

Palindrome String: A string is said to be a palindrome if it is the same when we are reading either from forward or backward. Madam, for example, is a palindrome string.
Reverse: A reverse of the word 'java' is 'avaj'
Vowels: a, e, i, o, u are vowel characters in the alphabet

package Ishwar;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class GUI_Form implements ActionListener{

	JLabel l1;
	JTextField t1;
	JButton b1,b2,b3;
	JFrame f = new JFrame();
	GUI_Form(){
		l1 = new JLabel("Input any string");
		t1 = new JTextField();
		
		b1 = new JButton("CheckPalindrome");
		b2 = new JButton("Reverse");
		b3 = new JButton("FindVowels");
		
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		
		f.add(l1);f.add(t1);
		f.add(b1);f.add(b2);f.add(b3);
		
		f.setSize(300,150);
		f.setLayout(new GridLayout(3,2));
		f.setVisible(true);
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent e) { //Madam #rev = madaM
		if(e.getSource()==b1) {
			String s = t1.getText().toLowerCase(); //Madam --> madam
			String rev = "";
			
			for(int i=s.length()-1;i>=0;i--) {
				rev = rev+s.charAt(i);
			}
			if(s.equals(rev)) {
				JOptionPane.showMessageDialog(f, s+" is a palindrome");
			}
			else {
				JOptionPane.showMessageDialog(f, s+" is not a palindrome");
			}
			
		}
		
		if(e.getSource()==b2) {
			String s = t1.getText(); //Madam --> madaM
			String rev = "";
			
			for(int i=s.length()-1;i>=0;i--) {
				rev = rev+s.charAt(i);
			}
			JOptionPane.showMessageDialog(f, "The reverse string is "+rev);
			
		}
		
		if(e.getSource()==b3) {
			String s = t1.getText().toLowerCase();
			List<Character> lst = new ArrayList<Character>();
			for (int i=0;i<s.length();i++) {
				if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' ||
						s.charAt(i)=='o' || s.charAt(i)=='u') {
					lst.add(s.charAt(i));
				} 
			}
			JOptionPane.showMessageDialog(f, "The vowel character in your string: "+lst);
		}
	}
	
	public static void main(String[] args) {
		new GUI_Form();
	}
}

JMenuBar and JPanel in Java with programming example

How can we create a menu bar (file menu, edit menu, view menu) and panel with Java?
JMenuBar:
JMenuBar is provided by the Java Swing package. We can create a menu bar easily with JMenuBar. There are several constructors related to this term:

  1. JMenuBar(): It creates a new MenuBar.
  2. JMenu(): It creates a new Menu with no text.
  3. JMenu(String name): It creates a new Menu with a given name.


JPanel: JPanel is also provided by the Java Swing package. This panel represents a container that can store a group of components. For example, we can store labels, text fields, buttons, etc. in this container. There are several constructors related to this term:

  1. JPanel(): It creates a new panel with the default layout manager i.e., flow layout.
  2. JPanel(LayoutManager l): It creates a new panel with the given layout manager.
package Ishwar;

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

public class Main extends JFrame {

    public Main(){
        setJMenuBar(getMyMenu());
        add(getPanel());
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setMinimumSize(new Dimension(200,200));
        setResizable(false);
        pack();
    }

    public JMenuBar getMyMenu(){
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu viewMenu = new JMenu("View");

        JMenu newItem = new JMenu("New");
        JMenuItem openFile = new JMenuItem("Open");
        JMenuItem recentFile = new JMenuItem("Open Recent");

        JMenuItem project = new JMenuItem("Project");
        JMenuItem existing = new JMenuItem("Project from Existing Sources");
        JMenuItem version = new JMenuItem("Project from Version Control");

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(viewMenu);

        fileMenu.add(newItem);
        fileMenu.add(openFile);
        fileMenu.add(recentFile);

        newItem.add(project);
        newItem.add(existing);
        newItem.add(version);

        return menuBar;

    }

    public JPanel getPanel(){
        JPanel myPanel = new JPanel();

        myPanel.setBorder(BorderFactory.createTitledBorder("Sign in"));
        //two method: GridLayout and GridBagLayout
        myPanel.setLayout(new GridLayout (3,2));

//        myPanel.add(new JLabel("Username: "));
//        myPanel.add(new JTextField(20));
//
//        myPanel.add(new JLabel("Password: "));
//        myPanel.add(new JPasswordField(20));
//
//        myPanel.add(new JButton("Submit"));

        myPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        myPanel.add(new JLabel("Username"),gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        myPanel.add(new JTextField(20),gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        myPanel.add(new JLabel("Password"),gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        myPanel.add(new JPasswordField(20),gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        myPanel.add(new JButton("submit"),gbc);


        // 0,0     1,0
        // 0,1     1,1    2,1
        return myPanel;
    }


    public static void main(String[] args) {
	// write your code here
        new Main();
    }
}
 

Conclusion:

In this way, you can write a complete GUI program in Java with swing components. JMenuBar and JPanel are other swing packages that you can use to create a Java swing program.

I hope this post is very clear to you. Don't hesitate to ask me in the comment section if you have any questions. I will reply as soon as possible. Thanks.