Skip to main content

Palindrome in Java GUI

We all know that palindrome is a word or phrase that reads the same backward as forward. Here in this tutorial, we will create a program that can determine if the word inputted is a palindrome or not.

Now, let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of

2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the word.

  1. import javax.swing.*;

3. Before coding in the Main, create first a function named isPalindrome as Boolean that has variable word as string as parameter of it.

  1. public static boolean isPalindrome(String word) {
  2. int left = 0; // index of leftmost unchecked char
  3. int right = word.length() -1; // index of the rightmost
  5. while (left < right) { // continue until they reach center
  6. if (word.charAt(left) != word.charAt(right)) {
  7. return false; // if chars are different, finished
  8. }
  9. left++; // move left index toward the center
  10. right--; // move right index toward the center
  11. }
  13. return true; // if finished, all chars were same
  14. }

4. Initialized your variable in your Main, variable Str as string and make it as an input.

  1. String Str;
  2. Str = JOptionPane.showInputDialog(null, "Enter String:");

5. Lastly, display the output of the program whether the word is a palindrome or not.

  1. JOptionPane.showMessageDialog(null,"The word " + Str + " is palindrome: " + isPalindrome(Str) );

Take note that isPalindrome(Str) syntax triggers to access the boolean function that will validate the inputted word of variable Str.



Here's the full code of this tutorial:

  1. import javax.swing.*;
  3. public class palindrome {
  6. public static void main(String[] args) {
  8. String Str;
  10. Str = JOptionPane.showInputDialog(null, "Enter String:");
  12. JOptionPane.showMessageDialog(null,"The word " + Str + " is palindrome: " + isPalindrome(Str) );
  13. }
  14. public static boolean isPalindrome(String word) {
  15. int left = 0;
  16. int right = word.length() -1;
  18. while (left < right) {
  19. if (word.charAt(left) != word.charAt(right)) {
  20. return false;
  21. }
  22. left++;
  23. right--;
  24. }
  26. return true;
  27. }
  28. }

Hope this helps! :)

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at:

Add and Follow me on Facebook:

Visit my website at:

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.