Results 1 to 8 of 8
Thread: Global Procedure/Functions
- 05-06-2010, 08:29 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Global Procedure/Functions
Hi, before in VB6.0 when we want our porcedure to be called in different forms we usually declare them
PUBLIC and place them to modules.
Here is a sample procedure that I would like to be Global.
Right now the HighLightText() is placed in frmLogin.java and I am trying to call it from frmMain.java.
Thanks in advance,Java Code:public void HighLightText(javax.swing.JTextField ctr){ ctr.setSelectionColor(Color.BLUE); ctr.setSelectedTextColor(Color.WHITE); ctr.setSelectionStart(0); ctr.setSelectionEnd(ctr.getText().length()); }
geje
- 05-06-2010, 09:04 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Learn how to code OO?
This is not VB, so you shouldn't be trying to code like VB. And I seriously doubt a global method is the correct solution here.
Presumably your main class (classes should start with a capital, FrmMain) has an instance of FrmLogin, which it should then call the highlightText (methods and variable names should be camelCase) code? Or are you thinking this is generic code?
- 05-06-2010, 09:26 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I am not trying to code like VB. I am aware that VB is lot different with JAVA.Learn how to code OO?
This is not VB, so you shouldn't be trying to code like VB
I just use VB in my question because for me this is the best way to express myself,
for the other members to understand what I am trying to say. English is not my native language.
Then what do you think is the solution?And I seriously doubt a global method is the correct solution here.
I am sorry but I cannot fully understand what you are trying to clarify...Presumably your main class (classes should start with a capital, FrmMain) has an instance of FrmLogin, which it should then call the highlightText (methods and variable names should be camelCase) code? Or are you thinking this is generic code?
Thanks,
geje
- 05-06-2010, 11:36 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
OK.
In OO you essentially bolt objects together.
So, in your case, it appears that frmMain has a frmLogin in it?
I can't entirely tell from the description given.
So why not simply call the highlight method from the frmLogin object you have?
Or better yet, post some code for the frmMain to show how it is using frmLogin.
- 05-07-2010, 03:46 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I will try to explain it more. I have this code/method called HighLightText().
This method is temporarily in frmLogin.java which I know is not right because when
I close frmLogin () it will be dispose (its defaultcloseoperation is DISPOSE).
So I would like to put HighLightText() in a Class/Module/Library
(I dont know where to put it, In vb we put it in module. Module does not have form or any controls.)
Modules just contain Function/Method/Procedure) and call HighLightText() whenever a JTextField receive the focus.
JtextField could be in different forms or JFrame. Temporarily, HighlightText() is place in frmLogin.
Here is the code where I place the HighLightText() temporarily
Java Code:public class frmLogin extends javax.swing.JFrame { public void HighLightText(javax.swing.JTextField ctr){ ctr.setSelectionColor(Color.BLUE); ctr.setSelectedTextColor(Color.WHITE); ctr.setSelectionStart(0); ctr.setSelectionEnd(ctr.getText().length()); } }
I try to call it but I have no idea how to do it. I try to type "frmLoagin." so that it
will show what are the available methods, functions, procedures etc. and read how to use them.
Hope this make me clearer.
Thanks for taking time on my post.
geje
-
This smells of a library or utility method since it does not appear that your method will change the state of the class that it resides in. I guess what I'm saying is that perhaps it would be best to make this method static and call it off of the class that it resides in as it does not seem to need an object to call it off of -- just like the methods of the Math class such as Math.sin(...) and Math.sqrt(...). Also, I see no need for the class that this method resides in to subclass JFrame and in fact it probably shouldn't subclass anything at all.
Also, please learn to use standard Java naming convention -- class names begin with a capital letter and method names do not, and both use camel case for the inner portion of their names. Thus consider naming things:
edit: if anyone disagrees, please let me know. I by no means claim to be an expert in this and as always am open to improvement.Java Code:public class FormUtilities { public static void highLightText(javax.swing.JTextField ctr){ ctr.setSelectionColor(Color.BLUE); ctr.setSelectedTextColor(Color.WHITE); ctr.setSelectionStart(0); ctr.setSelectionEnd(ctr.getText().length()); } }Last edited by Fubarable; 05-07-2010 at 03:57 AM.
- 05-07-2010, 04:24 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Fubarable,
Thanks, that was actually what I wanted to do.
Thank you for correcting me on that. I will do it.Also, please learn to use standard Java naming convention -- class names begin with a capital letter and method names do not, and both use camel case for the inner portion of their names
- 05-07-2010, 09:04 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Except that this affects only JTextFields whenever they gain focus, so wouldn't it make sense to extend JTextField to include this method which gets called whenever it gains focus? The fact it only happens to JTextFields, and all it is doing is setting the state of the JTextField implies to me that it is not a helper method, but an extension of that class.
Similar Threads
-
Procedure to start Thread?
By greatmajestics in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2010, 05:05 PM -
call a pl/sql procedure through a jsp page
By mudit222 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-20-2010, 08:02 PM -
migration of Ms-sql procedure to My-sql Procedure
By kriss in forum New To JavaReplies: 0Last Post: 02-03-2010, 08:20 AM -
Debug mySql stored procedure
By ulix83 in forum NetBeansReplies: 0Last Post: 10-29-2009, 11:48 AM -
stored procedure
By sankarigopi in forum JDBCReplies: 1Last Post: 11-13-2008, 04:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks