Results 1 to 8 of 8
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default 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.
    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());
    }
    Thanks in advance,
    geje

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    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?

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Learn how to code OO?
    This is not VB, so you shouldn't be trying to code like VB
    I am not trying to code like VB. I am aware that VB is lot different with JAVA.
    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.


    And I seriously doubt a global method is the correct solution here.
    Then what do you think is the solution?


    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?
    I am sorry but I cannot fully understand what you are trying to clarify...


    Thanks,
    geje

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    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.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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

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

    Default

    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:

    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());
        }
      }
    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.
    Last edited by Fubarable; 05-07-2010 at 03:57 AM.

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Fubarable,
    Thanks, that was actually what I wanted to do.

    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
    Thank you for correcting me on that. I will do it.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    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

  1. Procedure to start Thread?
    By greatmajestics in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2010, 05:05 PM
  2. call a pl/sql procedure through a jsp page
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-20-2010, 08:02 PM
  3. Replies: 0
    Last Post: 02-03-2010, 08:20 AM
  4. Debug mySql stored procedure
    By ulix83 in forum NetBeans
    Replies: 0
    Last Post: 10-29-2009, 11:48 AM
  5. stored procedure
    By sankarigopi in forum JDBC
    Replies: 1
    Last Post: 11-13-2008, 04:53 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
  •