Results 1 to 12 of 12
- 05-07-2010, 07:11 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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
then Error will occur because parameter and passed value are not the same kindJava 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()); } }
Java Code:method highlightText cannot be applied to given types
Also, I get error in IF statement
Thanks for always helping me,Java Code:Cannot find symbol symbol: class swing location: package javax
gejeLast edited by mine0926; 05-07-2010 at 07:14 AM.
- 05-07-2010, 07:42 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
instanceof is the cookie that you want
Java Code:if (evt.getSource() instanceof JTextField) { JTextField textField = (JTextField) evt.getSource(); }
- 05-07-2010, 08:41 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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
Thanks,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); } }
geje
- 05-07-2010, 08:43 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
ok I already solve it. I just add focusLost. Thanks again
- 05-07-2010, 09:09 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
This is why I think the highlight code ought to be on an extended JTextField.
- 05-07-2010, 09:11 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Unless if they are going to need similar functionality with other text components.
- 05-07-2010, 09:20 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 05-12-2010, 06:00 AM #8
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Hi,
Thanks for you help but highLightText() does not highlight JTextField whenever they got the focus.
Can you suggest code that will make it more cleaner?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.
Thanks for pointing me on my errors.Last edited by mine0926; 05-12-2010 at 06:36 AM.
- 05-12-2010, 09:53 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
What you gain is code that is tied to the thing it is supposed to be working on, and not scattered around the place.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()); }
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...:)
- 05-17-2010, 08:34 AM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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
- 05-17-2010, 09:04 AM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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
- 05-17-2010, 10:11 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
';' expected, not of the common kind..
By Addez in forum New To JavaReplies: 6Last Post: 09-02-2009, 04:37 AM -
Conversion from wav to vox
By bozovilla in forum Advanced JavaReplies: 1Last Post: 07-31-2008, 05:54 AM -
how client know what kind of server
By lemur in forum NetworkingReplies: 3Last Post: 05-31-2008, 07:11 AM -
Total Newbie, Be Kind :)
By dazza-s in forum New To JavaReplies: 11Last Post: 04-26-2008, 10:54 PM -
Will make a pyramid of some kind out of a number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks