Results 1 to 3 of 3
Like Tree2Likes
  • 2 Post By JosAH

Thread: how to delete single string from JTextField??

  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default Help me understand this..

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class calcSample extends JFrame{
        
        public calcSample(){
            setVisible(true);
            setSize(400,400);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setLayout(new GridLayout(3,1));
         txt1 = new JTextField();
         btnDel = new JButton("Del");
         btnOne = new JButton("1");
         txt1.setEditable(false);
         btnDel.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String delString1 = txt1.getText(); 
          StringBuilder stBuild = new StringBuilder(txt1);
               stBuild.deleteCharAt(i);
               txt1.setText(stBuild.toString());
               System.out.println("Index "+i);
               System.out.println("String Length "+ delString1.length());
               System.out.println("String Edited " + String.valueOf(stBuild));
            }});
         btnOne.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String One = txt1.getText();
                    txt1.setText(One+1);
                }
            });
         
         add(txt1);
         add(btnDel);
         add(btnOne);
        }
        public static void main(String[]args){
            new calcSample();
        }
        private JTextField txt1;
        private JButton btnDel,btnOne;
        
    }
    Output:
    Java Code:
    1111
    After clicking Del
    111
    click Del again
    11
    and so on.....
    but i don't understand how this code works:
    Java Code:
     btnDel.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String delString1 = txt1.getText(); 
          StringBuilder stBuild = new StringBuilder(txt1);
               stBuild.deleteCharAt(i);
               txt1.setText(stBuild.toString());
               System.out.println("Index "+i);
               System.out.println("String Length "+ delString1.length());
               System.out.println("String Edited " + String.valueOf(stBuild));
            }});
    the println code is for the debugging.
    Can someone tell me how this code deleted the last character of the inputted String w/ for loop simulation please :D
    because for loop is
    Last edited by raffs03; 07-03-2013 at 04:23 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: how to delete single string from JTextField??

    The deleteCharAt(int index) method of the StringBuilder object delete the character at the specified index. But I didn't see any variable "i" declared in your code.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help me understand this..

    Quote Originally Posted by raffs03 View Post
    but i don't understand how this code works:
    I think it is ok to assume that you didn't write the code yourself; why don't you ask the author(s) of the code what it means and how it works?

    kind regards,

    Jos
    DarrylBurke and MR bruto like this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 18
    Last Post: 01-10-2013, 10:51 AM
  2. Replies: 0
    Last Post: 08-08-2012, 08:42 AM
  3. Testing for single string in IF
    By carman12 in forum New To Java
    Replies: 3
    Last Post: 01-31-2011, 07:21 PM
  4. Replies: 2
    Last Post: 03-27-2010, 07:22 PM
  5. delete string in JtextArea
    By cloudet in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2008, 10:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •