Unwanted Text Space Remover

TUTORIAL NO 2

Text Space Remover

In this tutorial you will learn:
  1. Event handling
  2. Layout Manager
  3. JAVA awt basics
  4. JAVA swing basics
  5. Adapters
  6. Anonymous classes
  7. Using two separate classes
Very often I face a problem when copying from PDF that there are random spaces in the text. So I decided to make a simple app for text adjustment. In this tutorial I will teach you how to make unwanted text space remover. text space remover In this tutorial we will be working in JAVA SWING. The first thing that we are going to do is setting up a JFrame and adding the required panels and Components. Basic step: Download and install ECLIPSE and set up a JAVA PROJECT. Then create a new class and name it SpaceRemover. Then follow the steps 1.IMPORT STATEMENTS First of all write these import statements in your .java file
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
We require event import for mouse click , awt and swing imports for visual design. We will be using separate classes for JPanel and JFrame. First thing is setting up the panel and after that we will setup the frame. Then we will do composition i.e make and object of the panel class and add it to frame class. 2.SETTING UP THE PANEL CLASS
  1. class Removerpanel extends JPanel {
  2.  
  3. JTextArea t_area = new JTextArea(); // to paste the text
  4. JLabel label = new JLabel("Enter the text then press ");
  5. JButton jb = new JButton("REMOVE SPACES");
  6. JScrollPane sp = new JScrollPane(t_area); //adding the scroll bar
  7. String text;
Create the panel class by extending it from JPanel (i.e Inheritence) and then create the text area , label and button finally making a scrollPane for automatically creating a vertical and horizontal scroll bars when the data exceeds the view size. 3. SETTING UP THE JPANEL We will do everything in a constructor so that the jpanel gets setup whenever we create the object of that JPanel ( Remember that: we will create the object of the JPanel in the frame class in this case i.e COMPOSITION ). Now writing the constructor
  1. Removerpanel() {
  2. setLayout(new BorderLayout()); // for adding components north
  3. JPanel up = new JPanel(); //creating sub panel for north
  4.  
  5. add(up, "North"); //adding the up panel in north position
  6. up.add(label); //first adding the label
  7. up.add(jb); //then adding the button
  8.  
  9. add(sp); //adding scrollpane in the middle (i.e main Panel)
We are using border layout so that we can adjust our components to north and center positions. We created another jpanel first which we added north, then we added label and the button in that Jpanel. After that we added the scrollPane which has the text area in center ( Remember: By default components are added in the center if we don’t specify any other position in BORDER LAYOUT).
  1. jb.addMouseListener(new MouseAdapter() { //anonymous class
  2.  
  3. public void mouseClicked(MouseEvent me)
  4. {
  5. text = t_area.getText(); //storing the text in a string
  6. char arr[] = text.toCharArray(); // string to charcter array
  7. text = ""; //empty string in text
  8. int space = 0; // space counter
  9.  
  10. for (int i = 0; i < arr.length; i++) {
  11. if (arr[i] == ' ' | arr[i] == '\t') //finding spaces and tabs
  12. {
  13. space++; // if space is found then increment counter
  14. if (space == 1) //for first value insert 1 space
  15. text += " ";
  16. }
  17. else // if no space is found
  18. {
  19. space = 0; //reset the counter
  20. text += arr[i]; //add in string again
  21. }
  22. }
  23. t_area.setText(text); //now set this string text to text area
  24. }
  25. }); //end anonymous MouseAdapterClass
  26. }//end addMouseListener
  27. }// end finderpanel
In the mouselistener we created an anonymous MouseAdapter class (Remember: anonymous class is the one in which you only have to create one object). Now we override the mouse clicked function. The first thing we need to do is to get the text from the text area. Then convert that text to a character array so that individual characters can be edited (Remember : space is also a character). Now we empty our string and create a space counter. We know that we only need a single space after every word so we run a for loop and check for spaces and tabs. We counted the number of times they appear after every word. Then we put a condition that only keep the first space after the word and add that space to our string text after every word. There is also a possibility that only one space is found after the text so in that case we go to else and simply add the next word to the text string. After that we simply set the text area to our string text variable. 3. SETTING UP THE JFRAME CLASS
  1. public class SpaceRemover extends JFrame {
  2.  
  3. Removerpanel fp = new Removerpanel();
  4.  
  5. SpaceRemover() {
  6. setTitle("SPACE REMOVER"); //setting title
  7. setDefaultCloseOperation(SpaceRemover.EXIT_ON_CLOSE);//for closing
  8. setSize(500, 500); // frame size
  9. add(fp); // adding the removerpanel in the frame
  10. setVisible(true); // set frame visibility
  11. }
Setting up our frame class is quite simple. We simply created a public class which is extending from JFrame then we created an object of jpanel class that we created above. Set the frame title, close operation and size and after that added the object of that jpanel class in the frame constructor. 4. DEFINING THE MAIN FUNCTION
  1. public static void main(String args[]) {
  2. new SpaceRemover(); // creating the object of our JFrame class
  3. }// end main
  4. }// end filefinder class
In the main function we only created the object of the JFrame class and our application is complete. 5.OUTPUT: AFTER PASTING THE TEXT AND BEFORE PRESSING THE BUTTON output1
AFTER PRESSING THE BUTTON final output

Tags

Add new comment