Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: JTextField

  1. #1
    gancio is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default JTextField

    Hello is there any method that select/highlight the text of a JtextField?
    I mean without using the mouse ofc :D

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    setSelectionStart and setSelectionEnd.
    JTextComponent (Java Platform SE 6))

  3. #3
    gancio is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    setSelectionStart and setSelectionEnd.
    No, it doesn't highlight the selected text like it could be done with mouse.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Of course it does. Did you try it?
    Post the code you used that did not work.

  5. #5
    gancio is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default

    Which code? Just try yourself, create a simple JtextField and use those methods; you will see that nothing is highlighted even if you use textfield.selectAll().

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    selectAll? The methods suggested were setSelectionStart and setSelectionEnd. Read about them in the API docs and use them. I don't have to create any code to test them. I already know that they work.

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

    Default

    Agree. The onus of creating and posting code that shows that the suggested methods don't work is on the original poster. gancio, the ball's in your court.

  8. #8
    gancio is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default

    argh.. with a sample JTextField code it works but in my program doesn't work.. maybe cause it's associated with a button event.. :confused:
    I tried also with repaint but nothing.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    And we all can't see your code so we can't really say much about it.

  10. #10
    gancio is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default

    Here the code:

    Java Code:
    class MyDocumentListener implements DocumentListener {
            public int x,y;
            public MyDocumentListener(int posx,int posy) {
                x=posx;
                y=posy;
            }
                public void insertUpdate(DocumentEvent e) {
                try {
                    if (cellCheck(x,y)) {
                        errore=false;
                        tf[x][y].setBackground(Color.LIGHT_GRAY);
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "error");
                        error=true;
                        tf[x][y].setBackground(Color.RED); //it works
                        tf[x][y].select(0,1);                     //doesn't works
                    }
    
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null, "error");
                    errore=true;
                    tf[x][y].setBackground(Color.RED);
                }   
            }
    {
    tf are JTextField of course.

    Maybe it doesn't work cause there's already a mouse selection so the caret is already setted. :confused:

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

    Default

    What if you delay the select call slightly by queuing it up on the event queue:

    Java Code:
       else {
           JOptionPane.showMessageDialog(null, "error");
           error=true;
           tf[x][y].setBackground(Color.RED); //it works
           SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                 tf[x][y].select(0,1);  // try it here
              }
           });
           //tf[x][y].select(0,1);   // comment this out
       }
    ?

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

    Default

    Also a suggestion: when posting code for us to analyze and debug, it is almost always better to post a small compilable program that demonstrates the problem, called a "Short, Self Contained, Correct (Compilable), Example" or SSCCE. For example, this is the SSCCE that I created to test your problem:
    Java Code:
    import java.awt.Color;
    import java.util.Random;
    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    
    public class SwingFu {
    
      private static void createAndShowGUI() {
        final Random random = new Random();
        final JTextField textfield = new JTextField(10);
        textfield.getDocument().addDocumentListener(
            new DocumentListener() {
    
              public void insertUpdate(DocumentEvent e) {
                if (random.nextBoolean()) {
                  textfield.setBackground(Color.LIGHT_GRAY);
                } else {
                  JOptionPane.showMessageDialog(null, "error");
                  textfield.setBackground(Color.RED); // it works
                  SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                      textfield.select(0, 1); // does works
                    }
                  });
                  //textfield.select(0, 1); // doesn't works
                }
              }
    
              public void changedUpdate(DocumentEvent arg0) {}
              public void removeUpdate(DocumentEvent arg0) {}
            });
    
        JFrame frame = new JFrame("SwingFu Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(textfield);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    What if you delay the select call slightly by queuing it up on the event queue:
    Not really necessary:
    Java Code:
    //What a funny name for a class!
    class SwingFu {
    
    	private static void createAndShowGUI() {
    		final JTextField textfield = new JTextField("Some text", 10);
    		textfield.getDocument().addDocumentListener(new DocumentListener() {
    			public void insertUpdate(DocumentEvent e) {
    				textfield.select(0, 1); //works
    			}
    
    			public void changedUpdate(DocumentEvent e) {
    			}
    
    			public void removeUpdate(DocumentEvent e) {
    			}
    		});
    
    		JFrame frame = new JFrame("SwingFu Application");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(textfield);
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
    
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    
    	}
    }

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

  15. #15
    gancio is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default

    Eureka!
    It works.
    Thank you very much. ;)

    Quote Originally Posted by Fubarable View Post
    What if you delay the select call slightly by queuing it up on the event queue:

    Java Code:
       else {
           JOptionPane.showMessageDialog(null, "error");
           error=true;
           tf[x][y].setBackground(Color.RED); //it works
           SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                 tf[x][y].select(0,1);  // try it here
              }
           });
           //tf[x][y].select(0,1);   // comment this out
       }
    ?

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    On my system, it is necessary; don't know why.
    You don't think that the random.nextBoolean() has anything to do with it?
    If you look closely at my code you will see that I did not put that weird piece of code in it.

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

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    It's not the next boolean. I don't know why but your program doesn't work for me.
    By it doesn't work you mean that you fire it up and they start to type on the textfield that gets displayed and the first character doesn't get highlighted every time you type in there?

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

    Default

    I'm sorry for not being clear: it's the highlighting. It will not highlight the first char of the String ever if I don't queue the method call on the EDT.

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    I was just asking to check that you are actually typing some text onto the textfield because the highlight will only occur if you insert new text into the textfield.

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem's with JTextField
    By DC% in forum AWT / Swing
    Replies: 4
    Last Post: 03-10-2009, 05:33 PM
  2. AutoComplete for jtextfield
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 02-12-2009, 06:46 AM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM
  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
  •