• We just launched and are currently in beta. Join us as we build and grow the community.

Unwanted Text Space Remover

v4mp1n1k4

Continuous Integration Specialist
V Rep
0
0
0
Rep
0
V Vouches
0
0
0
Vouches
0
Posts
141
Likes
198
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
TUTORIAL NO 2



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.png


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. JTextArea

    t_area =

    new

    JTextArea

    (

    )

    ;

    // to paste the text
  3. JLabel

    label =

    new

    JLabel

    (

    "Enter the text then press "

    )

    ;
  4. JButton

    jb =

    new

    JButton

    (

    "REMOVE SPACES"

    )

    ;
  5. JScrollPane

    sp =

    new

    JScrollPane

    (

    t_area)

    ;

    //adding the scroll bar
  6. 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. add(

    up, "North"

    )

    ;

    //adding the up panel in north position
  5. up.add

    (

    label)

    ;

    //first adding the label
  6. up.add

    (

    jb)

    ;

    //then adding the button

  7. 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. public

    void

    mouseClicked(

    MouseEvent

    me)
  3. {
  4. text =

    t_area.getText

    (

    )

    ;

    //storing the text in a string
  5. char

    arr[

    ]

    =

    text.toCharArray

    (

    )

    ;

    // string to charcter array
  6. text =

    ""

    ;

    //empty string in text
  7. int

    space =

    0

    ;

    // space counter

  8. for

    (

    int

    i =

    0

    ;

    i <

    arr.length

    ;

    i++

    )

    {
  9. if

    (

    arr[

    i]

    ==

    ' '

    |

    arr[

    i]

    ==

    '\t

    '

    )

    //finding spaces and tabs
  10. {
  11. space++;

    // if space is found then increment counter
  12. if

    (

    space ==

    1

    )

    //for first value insert 1 space
  13. text +=

    " "

    ;
  14. }
  15. else

    // if no space is found
  16. {
  17. space =

    0

    ;

    //reset the counter
  18. text +=

    arr[

    i]

    ;

    //add in string again
  19. }
  20. }
  21. t_area.setText

    (

    text)

    ;

    //now set this string text to text area
  22. }
  23. }

    )

    ;

    //end anonymous MouseAdapterClass
  24. }

    //end addMouseListener
  25. }

    // 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. Removerpanel fp =

    new

    Removerpanel(

    )

    ;

  3. SpaceRemover(

    )

    {
  4. setTitle(

    "SPACE REMOVER"

    )

    ;

    //setting title
  5. setDefaultCloseOperation(

    SpaceRemover.EXIT_ON_CLOSE

    )

    ;

    //for closing
  6. setSize(

    500

    , 500

    )

    ;

    // frame size
  7. add(

    fp)

    ;

    // adding the removerpanel in the frame
  8. setVisible(

    true

    )

    ;

    // set frame visibility
  9. }

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_1.png


AFTER PRESSING THE BUTTON
output2_1.png



Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top