Results 1 to 20 of 21
Thread: JTextField
- 08-24-2009, 04:36 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 11
- Rep Power
- 0
- 08-24-2009, 04:39 PM #2
setSelectionStart and setSelectionEnd.
JTextComponent (Java Platform SE 6))
- 08-24-2009, 04:57 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 11
- Rep Power
- 0
- 08-24-2009, 08:55 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Of course it does. Did you try it?
Post the code you used that did not work.
- 08-25-2009, 09:22 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 11
- Rep Power
- 0
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().
- 08-25-2009, 10:04 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
-
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.
- 08-25-2009, 03:34 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 11
- Rep Power
- 0
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.
- 08-25-2009, 03:51 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
And we all can't see your code so we can't really say much about it.
- 08-25-2009, 04:19 PM #10
Member
- Join Date
- Aug 2009
- Posts
- 11
- Rep Power
- 0
Here the code:
tf are JTextField of course.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); } } {
Maybe it doesn't work cause there's already a mouse selection so the caret is already setted. :confused:
-
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 }
-
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(); } }); } }
- 08-25-2009, 07:44 PM #13
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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(); } }); } }
-
On my system, it is necessary; don't know why.
- 08-26-2009, 09:44 AM #15
Member
- Join Date
- Aug 2009
- Posts
- 11
- Rep Power
- 0
- 08-26-2009, 11:39 AM #16
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
-
It's not the next boolean. I don't know why but your program doesn't work for me.
- 08-26-2009, 01:34 PM #18
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
-
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.
- 08-26-2009, 02:53 PM #20
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Problem's with JTextField
By DC% in forum AWT / SwingReplies: 4Last Post: 03-10-2009, 05:33 PM -
AutoComplete for jtextfield
By pinks_70986 in forum New To JavaReplies: 2Last Post: 02-12-2009, 06:46 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM -
help with JTextfield
By gary in forum New To JavaReplies: 4Last Post: 07-11-2007, 01:58 PM


LinkBack URL
About LinkBacks


Bookmarks