Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: backspace action(deleting a character in a string) in a paint event, looping forever

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default backspace action(deleting a character in a string) in a paint event, looping forever

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class BackSpaceSample extends JPanel {
     
        private String word;
     
        public BackSpaceSample() {
     
            setBackground(Color.BLACK);
            addKeyListener(new KeyListener());
            setFocusable(true);
            setLayout(null);
            word = "";
        }
     
        public void paint(Graphics g) {
     
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.WHITE);
            g2d.setFont(new Font("Arial", Font.BOLD, 24));
            g2d.drawString(word, 100, 100);
            repaint();
        }
     
        private class KeyListener extends KeyAdapter {
     
            public void keyTyped(KeyEvent e) {
     
                String alphabet =  "abcdefghijklmnopqrstuvwxyz";
                Character charac = e.getKeyChar();
     
                for (int x = 0; x < alphabet.length(); x++) {
     
                    if (charac.toString().equalsIgnoreCase("" + alphabet.charAt(x))) {
     
                        word = word + e.getKeyChar();
                    }
                }
            }
     
            public void keyPressed(KeyEvent e) {
     
                if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
     
                    System.out.println("Backspace is being pressed");
     
                    // iterating action that will re-assign the string itself
                    // but without the last character
                    for (int x = 0; x < word.length() -1; x++) {
     
                        word = word + word.charAt(x);
     
                        System.out.println("What the hell??    :    " + x);
                    }
                }
            }
        }
     
        public static void main(String[] args) {
     
            JFrame f = new JFrame();
            f.add(new BackSpaceSample());
            f.setSize(400, 200);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    }

    im trying to perform a backspace action in a paint method, i was trying to put the logic of it in the keyPressed event, where there is a loop that will iterate according to the length of the word (minus 1) and re-assgin the characters in the string instance but without the last character, but the loop is looping forever, my first assumption was that MAYBE I DONT HAVE A keyRelease event, so what i did is incuded a keyReleased event and put the same statements, still it loops forever, my second assumption was that MAYBE i should just put the statement in the released event, still the loop is going forever, i even put a statement that will check the value of 'x' of the for-loop counter, and i saw that it just keeps on iterating, i dont know what is going on. need help again please....


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    Yeah, that is going to loop forever. Think about what you're doing- you're looping until the end of the word, but each iteration, you're adding a character to the word. That means you'll never be able to reach the end of the word. I'm honestly not sure how you thought that was going to delete a letter. Maybe you thought you were starting over with a blank String?

    Anyway, I think the substring method is your friend for this one.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    hat means you'll never be able to reach the end of the word.
    oh thanks for guiding me through that,
    i never thought that the word keeps on expanding each time im adding on it, i think i should use another string instance, or the substring() method

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    Quote Originally Posted by chronoz13 View Post
    oh thanks for guiding me through that,
    i never thought that the word keeps on expanding each time im adding on it, i think i should use another string instance, or the substring() method
    Those seem like reasonably approaches. Or you could use a StringBuilder.

    But think about the word growing in terms of an int. What you have is basically this:

    int limit = 5;
    for(int i = 0; i < limit; i++){
       limit++
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chronoz13 (June 29th, 2011)

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    Or you could use a StringBuilder.
    thanks again for the suggestion! finally got what i needed


    if (word.length() > 0) {
     
                        StringBuilder strB = new StringBuilder(word);
                        strB.deleteCharAt(word.length() - 1);
                        word = strB.toString();
                    }

    i still dont get what i need using the substring method, but this one, it does all the things i needed with no time

  7. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    you'll never be able to reach the end of the word.
    - your statement really woke me up,
    Or you could use a StringBuilder.
    - thanks for refreshing me back, i forgot that there is a StringBuilder class that can manipulate strings

    if (word.length() > 0) {
     
                        for (int x = 0; x < word.length() - 1; x++) {
     
                            temp = temp + word.charAt(x);
                        }
     
                        word = temp;
                    }
    im not putting my self in a harder way, but im still not satisfied not knowing and solving the problem that i encountered using other things tocompensate with it, well i finally managed to do the logic that i want, i just have to use a separate object (String object) to hold the final word without the last character, thanks man thanks for the help

  8. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    oops, i didnt noticed substring() method returns a string LOL :p

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    Yep, you could have just done word = word.substring(0, word.length()-1); or something like that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    word.substring(0, word.length()-1);
    yeah i was frustrated like, what the hell!? this statement doesnt do anything to cut the letter from the word, i didnt know or i think i really forgot that this method returns a string value, and i should assign(re-assign) it to the string object itself again

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: backspace action(deleting a character in a string) in a paint event, looping for

    Keep in mind that Strings are immutable- that means they never change. You can get certain substrings or characters from a String, but the original remains intact.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chronoz13 (June 29th, 2011)

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. Extracting the " (Double Quote) character from a string
    By jai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 28th, 2011, 08:57 AM
  3. need help looping with a string
    By beandip408 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 29th, 2010, 09:08 AM
  4. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  5. Replies: 1
    Last Post: February 4th, 2010, 04:23 PM