Results 1 to 10 of 10
  1. #1
    satin is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Post Help with deleting string from JTextField

    Hello,

    I have a problem to delete string from JTextfield. I am making RGB model for colors and when I put higher color than 255 I want my program automaticly delete this value. I tried to put null or "0" into the field (like this: jt.setText("0")), but it doesn't work. I tried to select string in the field (that works) and than change the value (like this:jt.replaceSelection("0")), it doesnt work either. Can anybody help me and tell me what is wrong with my code? Thank yooooooouuuuuuuu!!!

    Here is the part of my code

    blue = new JTextField(5);
    getContentPane().add(blue, gbc);
    blue.setDocument(new NumericDocument(3));

    document = blue.getDocument();
    document.addDocumentListener(new DocumentListener() {
    public void changedUpdate(DocumentEvent event) {};

    public void insertUpdate(DocumentEvent event1) {
    b = handleDocument(blue);
    rect.setBackground(new Color(r, g, b));
    }

    public void removeUpdate(DocumentEvent event2) {
    b = handleDocument(blue);
    rect.setBackground(new Color(r, g, b));
    }


    and there is the function handleDocument called from document listener

    public int handleDocument(JTextField jt){
    int c;
    if(jt.getText().length() <1) c = 0;
    else c = Integer.parseInt(jt.getText());

    if(c>255) {

    c = 0;
    jt.selectAll();
    //jt.replaceSelection("0");
    //jt.setText("0");

    return 0;
    }
    Last edited by satin; 11-08-2008 at 03:21 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    tried to put null or "0" into the field (like this: jt.setText("0")), but it doesn't work.
    What does "it doesn't work" mean?
    I would think it would change what's in the JTextField to "0".

  3. #3
    satin is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default

    It means it does nothing. The old value stays there. I thought as well that it will put "0" in the field, but it doesnt.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    You'll have to post the code. setText() will change the text.
    Are you sure the setText() code is being executed? Add some println()s to see what the value of c is and where the execution flow is going.

  5. #5
    satin is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default

    I am sending the code. thanks for looking at it.
    Attached Files Attached Files

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can't update a Document from within a DocumentListener. You may wish to consider using a DocumentFilter instead.

  7. #7
    satin is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default

    Thanks, ill try it.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Another way is to change the textfield's text but wrap it in a Runnable and queue it on the event dispatch thread. This works, but I don't know if it's kosher or not:
    Java Code:
      public int handleDocument(JTextField jt)
      {
        final JTextField finalJt = jt;
        int c;
        if (jt.getText().length() < 1)
          c = 0;
        else
          c = Integer.parseInt(jt.getText());
    
        if (c > 255)
        {
    
          c = 0;
          SwingUtilities.invokeLater(new Runnable()
          {
            public void run()
            {
              finalJt.setText("0");
            }
          });
        }
        return c;
      }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Whoops. Missed that the code was inside a document listener.
    Try this:
    Java Code:
            public void insertUpdate(DocumentEvent e) {
                  ....
                // Clear the Document from inside a Document listener
                final Document doc = e.getDocument();
                  SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                      try {
                        doc.remove(0, doc.getLength());  // clear it
                      }catch(Exception x){x.printStackTrace();}
                   }
                 });

  10. #10
    satin is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

Similar Threads

  1. deleting characters from a String
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 08-29-2008, 12:14 PM
  2. Deleting from an object
    By vitaminz in forum New To Java
    Replies: 7
    Last Post: 08-10-2008, 03:56 AM
  3. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM
  4. Using Columns With JTextField
    By The Evil Genius in forum AWT / Swing
    Replies: 1
    Last Post: 03-17-2008, 01:01 AM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 PM

Posting Permissions

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