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

    Default kind of Component being return? and Conversion

    1. Is there anyway to know the kind of Component is being return? At focusGained event I would like to know it kind of control.
    2. Convert component or Object to a JTextField?

    highlightText has a parameter of JTextField:
    Java Code:
    public static void highlightText(javax.swing.JTextField ctr){
            ctr.setSelectionColor(Color.BLUE);
            ctr.setSelectedTextColor(Color.WHITE);
            ctr.setSelectionStart(0);
            ctr.setSelectionEnd(ctr.getText().length());
     }

    so if I get the source or component that has focus and pass it to highlightText
    Java Code:
    private void focusGained(java.awt.event.FocusEvent evt){
        // Here: if evt.getSource() is JTextField then
        // evt.getSource() should be converted or validated as JTextField
         if (evt.getSource().getClass().getComponentType() == javax.swing.JTextField){
            FormUtilities.highlightText(evt.getSource());
        }
    }
    then Error will occur because parameter and passed value are not the same kind
    Java Code:
        method highlightText cannot be applied to given types

    Also, I get error in IF statement
    Java Code:
    Cannot find symbol
    symbol: class swing
    location: package javax
    Thanks for always helping me,
    geje
    Last edited by mine0926; 05-07-2010 at 07:14 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    instanceof is the cookie that you want
    Java Code:
    if (evt.getSource() instanceof JTextField) {
    	JTextField textField = (JTextField) evt.getSource();
    }

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

    Default

    Thanks for you reply.

    I got new error now, it says
    frmMain is not abstract and does not override abstract method focusLost(java.awt.event.FocusEvent)
    in java.awt.event.FocusListener


    here is the code
    Java Code:
    public class [b][u]frmMain[/u][/b] extends javax.swing.JFrame implements FocusListener {
        public frmMain() {
            initComponents();
            ControlProperties();
            ControlResize();
        }
        public void focusGained(java.awt.event.FocusEvent e) {
            if (e.getSource() instanceof JTextField){
                JTextField source = (JTextField) e.getSource();
                System.out.println(source);
                FormUtilities.highLightText(source);
            }
        }
    Thanks,
    geje

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

    Default

    ok I already solve it. I just add focusLost. Thanks again

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

    Default

    This is why I think the highlight code ought to be on an extended JTextField.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Unless if they are going to need similar functionality with other text components.

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

    Default

    Possibly, but it seems a bit untidy to me.
    As does the hard-coded colours, but that's another thing entirely.

    It just doesn't look like a utility method...but maybe that's just me.

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

    Default

    Hi,

    Thanks for you help but highLightText() does not highlight JTextField whenever they got the focus.

    Possibly, but it seems a bit untidy to me.
    As does the hard-coded colours, but that's another thing entirely.

    It just doesn't look like a utility method...but maybe that's just me.
    Can you suggest code that will make it more cleaner?


    Thanks for pointing me on my errors.
    Last edited by mine0926; 05-12-2010 at 06:36 AM.

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

    Default

    The code you posted implied that you had JTextFields that would get their text highlighted on gaining focus.

    To do this you've created a static method, which takes a JTextField and highlights the text.

    Now, my argument is that it is specific to JTextFields (or at least certain JTextFields) and it makes no sense to detach it from them like that. r035198x has a good point about simply extending JTextField possibly not being a good idea (I forgot there were child classes)...but I still argue that it should be encapsulated in some way.

    So...some sort of HiglightJTextField, which is simply a wrapper around a JTextField, that holds both the FocusListener and the highlightField() method.

    Java Code:
    public class HiglightField {
        private JTextField field;
        // some constructor(s), which create the field and attach a focus listener that calls our own
        // highlight method.
        
        private void highlightText() {
        }
        
        public JTextField getField() {
            return field;
        }
    }
    
    // useage.
    {
        HiglightField myField = new HiglightField();
        myPanel.add(myField.getField());
    }
    What you gain is code that is tied to the thing it is supposed to be working on, and not scattered around the place.
    Your panel or form doesn't care about highlighting text, so it shouldn't have to actually deal with it.
    And since "highlighting text" is (in my mind) something that the field does, then the code for that should be with the field.

    Now I sit back and wait for the above to be picked apart...:)

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

    Default

    Hi,

    I am sorry if I reply to this thread too long. Anyway, I cannot try the given
    code by yours because the focusGained event does not fire. I try to put
    System.out.println(e.getSource()); under focusGained event to know if it
    is working. Everytime I click a JTextField it does not do anything.
    Java Code:
        public void focusGained(FocusEvent e) {
            System.out.println(e.getSource());
            if (e.getComponent() instanceof JTextField){
                //JTextField source = (JTextField) e.getSource();
                //FormUtilities.highLightText(source);
                System.out.println(e.getSource());
                //HiglightField myField = new HiglightField();
                //frmMaterials.add(myField.getField());
            }
        }

    focusGained event is place at:
    Java Code:
    public class frmMain extends javax.swing.JFrame implements FocusListener {
        public frmMain() {
            initComponents();
            ControlProperties();
            ControlResize();
        }
        public void focusGained(FocusEvent e) {
            System.out.println(e.getSource());
            if (e.getComponent() instanceof JTextField){
                //JTextField source = (JTextField) e.getSource();
                //FormUtilities.highLightText(source);
                System.out.println(e.getSource());
                //HiglightField myField = new HiglightField();
                //frmMaterials.add(myField.getField());
            }
        }
    
        public void focusLost(FocusEvent e) {
            //do nothing
        }

    Thanks,
    geje

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

    Default

    I got it working. Sometimes it is good to share your problems because it help you analyze the code more. I've been solving it for days but after I post it I notice that I did not put jTextField1.addFocusListener(this); that is why it does not fire focusGained event.

    TO Toll:
    If you dont mind can you give me a link where it explains what you did on your code?

    I got errors at your given code and I want to debug it and learn it.


    Thanks again,
    geje

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

    Default

    The code as posted is only an example.
    I'm not surprised it doesn't compile...it's not supposed to.
    You'll have to figure out how it wires together, I'm afraid...shouldn't be difficult, it's just a wrapper.

Similar Threads

  1. ';' expected, not of the common kind..
    By Addez in forum New To Java
    Replies: 6
    Last Post: 09-02-2009, 04:37 AM
  2. Conversion from wav to vox
    By bozovilla in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2008, 05:54 AM
  3. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 07:11 AM
  4. Total Newbie, Be Kind :)
    By dazza-s in forum New To Java
    Replies: 11
    Last Post: 04-26-2008, 10:54 PM
  5. Will make a pyramid of some kind out of a number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •