AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

customizing a JTextField question

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
dev null
Guest





PostPosted: Fri Nov 07, 2003 11:50 pm    Post subject: customizing a JTextField question Reply with quote



I'd like to create a custom JTextField that
always has a certain character, say a percent
sign, at the end of it. (Yes, I realize that
I could just add a JLabel with a "%" after the
JTextField, but I want to learn how to solve
this problem.) Also, I'd like to always have
the Caret positioned on the left side of the
percent sign and not allow the user to be able
to move over/right of it.

I've extended JTextField and supplied getText
and setText methods to add/strip off the trailing
percent sign, and I've tried playing around with
customizing a Document; but my problem is that
when the user highlights all the text in the
textfield (via mouse-drag) and deletes it, the
textfield stays empty. What I'd like to have
happen is for a percent sign to be added to an
otherwise empty field and the Caret positioned
just before it. Over-riding the setCaretPosition
didn't seem to help.

What method(s) am I missing? Do I have to
supply an extra mouseListener to handle mouse
events? I was hoping that there's be a common
entry point where text & Carets are entered or
cleared that I could over-ride which would then
handle everything that the user did to this
textfield, whether via keyboard, mouse, or
by having other widgets on screen update the
text (via setText).

Thanks in advance for any advice you can offer,
Back to top
Dave Glasser
Guest





PostPosted: Sat Nov 08, 2003 12:39 pm    Post subject: Re: customizing a JTextField question Reply with quote



[email]spam_to_dev_null (AT) yahoo (DOT) com[/email] (dev null) wrote on 7 Nov 2003 15:50:39
-0800 in comp.lang.java.gui:

Quote:
I'd like to create a custom JTextField that
always has a certain character, say a percent
sign, at the end of it. (Yes, I realize that
I could just add a JLabel with a "%" after the
JTextField, but I want to learn how to solve
this problem.) Also, I'd like to always have
the Caret positioned on the left side of the
percent sign and not allow the user to be able
to move over/right of it.

I've extended JTextField and supplied getText
and setText methods to add/strip off the trailing
percent sign, and I've tried playing around with
customizing a Document; but my problem is that
when the user highlights all the text in the
textfield (via mouse-drag) and deletes it, the
textfield stays empty. What I'd like to have
happen is for a percent sign to be added to an
otherwise empty field and the Caret positioned
just before it. Over-riding the setCaretPosition
didn't seem to help.

What method(s) am I missing? Do I have to
supply an extra mouseListener to handle mouse
events? I was hoping that there's be a common
entry point where text & Carets are entered or
cleared that I could over-ride which would then
handle everything that the user did to this
textfield, whether via keyboard, mouse, or
by having other widgets on screen update the
text (via setText).

I think you have to write an implementation of
javax.swing.event.DocumentListener and have it listen for events on
your JTextFields underlying Document object. As the JTextField Javadoc
says:

"In the JTextComponent based components, changes are broadcasted from
the model via a DocumentEvent to DocumentListeners. The DocumentEvent
gives the location of the change and the kind of change if desired.
The code fragment might look something like:


DocumentListener myListener = ??;
JTextField myArea = ??;
myArea.getDocument().addDocumentListener(myListener);


--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

Back to top
dev null
Guest





PostPosted: Sun Nov 09, 2003 8:17 pm    Post subject: Re: customizing a JTextField question Reply with quote



Dave Glasser <dglasser (AT) pobox (DOT) com> wrote

Quote:

I think you have to write an implementation of
javax.swing.event.DocumentListener and have it listen for events on
your JTextFields underlying Document object.

Dave:

Thanks for your reply. Below is a quick program that tosses an
Exception I've never heard of before when it calls setText ();
"java.lang.IllegalStateException: Attempt to mutate in notification".
Am I misunderstanding your suggestion? The program seems to run fine
until you do something like delete the percent sign in the JTextField.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

class TryMe extends JFrame
{
static JTextField jtf;

public TryMe ()
{
super ("JTextField Monitor");

jtf = new JTextField ("%");
jtf.setPreferredSize (new Dimension (250, 25));
jtf.getDocument ().addDocumentListener (new DocumentListener ()
{
public void insertUpdate (DocumentEvent de)
{
String s = jtf.getText ();
if (s.indexOf ("%") == -1)
jtf.setText (s + "%");
}
public void removeUpdate (DocumentEvent de)
{
String s = jtf.getText ();
if (s.indexOf ("%") == -1)
jtf.setText (s + "%");
}
public void changedUpdate (DocumentEvent de)
{
String s = jtf.getText ();
if (s.indexOf ("%") == -1)
jtf.setText (s + "%");
}
});

JPanel panel = new JPanel ();
panel.add (new JLabel ("JTextField + %"));
panel.add (jtf);

Container c = getContentPane ();
c.add (panel);

setSize (new Dimension (400, 100));
setVisible (true);
}

public static void main (String args[])
{
new TryMe ();
}
}

Back to top
Dave Glasser
Guest





PostPosted: Mon Nov 10, 2003 2:26 am    Post subject: Re: customizing a JTextField question Reply with quote

[email]spam_to_dev_null (AT) yahoo (DOT) com[/email] (dev null) wrote on 9 Nov 2003 12:17:13
-0800 in comp.lang.java.gui:

Quote:
Dave Glasser <dglasser (AT) pobox (DOT) com> wrote


I think you have to write an implementation of
javax.swing.event.DocumentListener and have it listen for events on
your JTextFields underlying Document object.

Dave:

Thanks for your reply. Below is a quick program that tosses an
Exception I've never heard of before when it calls setText ();
"java.lang.IllegalStateException: Attempt to mutate in notification".
Am I misunderstanding your suggestion? The program seems to run fine
until you do something like delete the percent sign in the JTextField.

I think you're on the right track, but apparently you're not supposed
to change the Document's contents while there's a writeLock on it.
(See the Javadoc for javax.swing.text.AbstractDocument.writeLock().)
You'll need to wait until the remove operation has completed before
modifying the contents with setText(). To do this, you can use
SwingUtilities.invokeLater(). I changed your program (shown below) to
implement this fix. I would advise you to read the Javadoc to
understand what is going on and why this works.

I also found that your program lets people type characters after the
percent sign without moving it to the end of the string. Is that what
you want?

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

class TryMe extends JFrame
{
static JTextField jtf;

class PercentCheck implements Runnable {
public void run() {
String s = jtf.getText ();
if (s.indexOf ("%") == -1) {
jtf.setText (s + "%");
}
}
};

public TryMe ()
{
super ("JTextField Monitor");

jtf = new JTextField ("%");
jtf.setPreferredSize (new Dimension (250, 25));
jtf.getDocument ().addDocumentListener (new DocumentListener ()
{
public void insertUpdate (DocumentEvent de)
{
SwingUtilities.invokeLater(new PercentCheck());
}
public void removeUpdate (DocumentEvent de)
{
SwingUtilities.invokeLater(new PercentCheck());
}
public void changedUpdate (DocumentEvent de)
{
SwingUtilities.invokeLater(new PercentCheck());
}
});

JPanel panel = new JPanel ();
panel.add (new JLabel ("JTextField + %"));
panel.add (jtf);

Container c = getContentPane ();
c.add (panel);

setSize (new Dimension (400, 100));
setVisible (true);
}

public static void main (String args[])
{
new TryMe ();
}
}


--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.