Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > CocoaJava NSTextField?

CocoaJava NSTextField?
Thread Tools
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Fluidic Space
Status: Offline
Reply With Quote
Jan 2, 2002, 06:45 AM
 
I'm new to cocoa programming and I have a small cocoa java problem. By default a NSTextField can be made to send an action when pressing the enter key. What I want is the NSTextField to send an action every time I type a letter or number into the textfield. How can I make it do this?
     
Fresh-Faced Recruit
Join Date: Nov 2001
Location: London, UK
Status: Offline
Reply With Quote
Jan 2, 2002, 07:17 AM
 
I can't remember how to do this off the top of my head but there is an example in the Learning Cocoa book that does exactly this. I'm in the process of converting all the examples in the book from Obj-C into Java and I know I've done this one so if no one answers your question before I get home this evening I'll post the converted version.

S.
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Jan 2, 2002, 11:15 AM
 
Originally posted by Species 8472:
<STRONG>I'm new to cocoa programming and I have a small cocoa java problem. By default a NSTextField can be made to send an action when pressing the enter key. What I want is the NSTextField to send an action every time I type a letter or number into the textfield. How can I make it do this?</STRONG>
no idea about java, in cocoa all you have to do is to register for the NSControlTextDidChangeNotification for that text field.
should be fairly similar in java.
     
Fresh-Faced Recruit
Join Date: Nov 2001
Location: London, UK
Status: Offline
Reply With Quote
Jan 2, 2002, 01:38 PM
 
Assuming you have a text field called textField the following code uses the Java version of the NSControlTextDidChangeNotification that seb2 refers to, to print out a line to the console each time you type a character in that field.:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
public class MyController extends NSObject {

NSTextField textField = new NSTextField(); <font color = brown>/* IBOutlet */</font>

public void awakeFromNib() {
NSNotificationCenter nCenter = NSNotificationCenter.defaultCenter();
nCenter.addObserver(this,
new NSSelector(<font color = red>"textDidChange"</font>, new Class[] {
NSNotification.class
}),
NSControl.ControlTextDidChangeNotification,
textField);
}

public void textDidChange(NSNotification notification) {
System.out.print(notification.name() + <font color = red>"\n"</font>);
}
}
</font>[/code]

Hope this helps

S.
     
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Fluidic Space
Status: Offline
Reply With Quote
Jan 2, 2002, 03:32 PM
 
Originally posted by Synaptic:
<STRONG>Assuming you have a text field called textField the following code uses the Java version of the NSControlTextDidChangeNotification that seb2 refers to, to print out a line to the console each time you type a character in that field.:

&lt;BLOCKQUOTE&gt;&lt;font size="1"face="Geneva, Verdana, Arial"&gt;code:&lt;/font&gt;&lt;HR&gt;&lt;pre&gt;& amp;lt;font size=1 face=courier&gt;
public class MyController extends NSObject {

NSTextField textField = new NSTextField(); &lt;font color = brown&gt;/* IBOutlet */&lt;/font&gt;

public void awakeFromNib() {
NSNotificationCenter nCenter = NSNotificationCenter.defaultCenter();
nCenter.addObserver(this,
new NSSelector(&lt;font color = red&gt;"textDidChange"&lt;/font&gt;, new Class[] {
NSNotification.class
}),
NSControl.ControlTextDidChangeNotification,
textField);
}

public void textDidChange(NSNotification notification) {
System.out.print(notification.name() + &lt;font color = red&gt;"\n"&lt;/font&gt; );
}
}
&lt;/font&gt;&lt;/pre&gt;&lt;HR&gt;&lt;/BLOCKQUOTE&gt;

Hope this helps

S.</STRONG>
Thanks, this helped me much!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:27 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2