Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,330 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,836 people online right now. Registration is fast and FREE... Join Now!




How to Make the TextField Focus?

 

How to Make the TextField Focus?

fsloke

2 May, 2008 - 03:08 PM
Post #1

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 311



Thanked: 9 times
My Contributions
Just say, My TextField now appear word "Hello World".

JTextField txt1 = new JTextField("Hello World");

It in gray color,

txt1.setForeground(Color.GRAY);

When the user click on the JTextField The word change to none. You can only see the curser blinking....

How to do this?

Thanks.

User is offlineProfile CardPM
+Quote Post


pbl

RE: How To Make The TextField Focus?

2 May, 2008 - 03:19 PM
Post #2

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1123 times
Dream Kudos: 450
My Contributions
QUOTE(fsloke @ 2 May, 2008 - 04:08 PM) *

Just say, My TextField now appear word "Hello World".

JTextField txt1 = new JTextField("Hello World");

It in gray color,

txt1.setForeground(Color.GRAY);

When the user click on the JTextField The word change to none. You can only see the curser blinking....

How to do this?

Thanks.

Focus/not focus only changes background color.

If it is gray and set foreground color to gray... sure you wont see the text anymore the foreground and background colors are the same.

Don't understand your question.... "The word change to none"

"How to do this?" How to do what ?

User is offlineProfile CardPM
+Quote Post

fsloke

RE: How To Make The TextField Focus?

2 May, 2008 - 03:46 PM
Post #3

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 311



Thanked: 9 times
My Contributions
Initialization state.

txt1 is set a Text "Hello World" and it in GRAY foreground color.

After the user click on the txt1 field

The txt1 become Black froreground color, then the "Hello World" text disappear. The txt1 waiting for user to input new string.

-thank
User is offlineProfile CardPM
+Quote Post

pbl

RE: How To Make The TextField Focus?

2 May, 2008 - 04:14 PM
Post #4

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1123 times
Dream Kudos: 450
My Contributions
CODE

import java.awt.*;
import javax.swing.*;

public class TField extends JFrame {

   TField() {
    super("TextField test");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(300, 150);
    JTextField t = new JTextField("Hello World");
    t.setForeground(Color.GRAY);
    add(t, BorderLayout.CENTER);
    // just to have 2 elements who can have focus
    add(new JButton("OK"), BorderLayout.SOUTH);
    setVisible(true);
   }
    
   public static void main(String[] args) {
       new TField();
   }
}


TextField appears with ForegroundColor gray
When we click on it
Hello World stays there
the cursor appears in the JTextField

User is offlineProfile CardPM
+Quote Post

fsloke

RE: How To Make The TextField Focus?

3 May, 2008 - 02:38 AM
Post #5

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 311



Thanked: 9 times
My Contributions
Only have one component...

No need the Button appear there....

I think have to implements MouseListener.....

Remember only one Component ie ONE TEXTFIELD

Thank you
User is offlineProfile CardPM
+Quote Post

pbl

RE: How To Make The TextField Focus?

3 May, 2008 - 07:18 AM
Post #6

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1123 times
Dream Kudos: 450
My Contributions
QUOTE(fsloke @ 3 May, 2008 - 03:38 AM) *

Only have one component...

No need the Button appear there....

I think have to implements MouseListener.....

Remember only one Component ie ONE TEXTFIELD

Thank you

If also works without the button. I just added the button so I can change to see if what happens when the focus changes. Could not reproduce the behaviour you are experiencing. May be you will have to post your code.
User is offlineProfile CardPM
+Quote Post

fsloke

RE: How To Make The TextField Focus?

3 May, 2008 - 05:48 PM
Post #7

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 311



Thanked: 9 times
My Contributions
Now it still researching for solution yet.

My coding a bit similar as yours.

Now I thinking how the Frame can detect which components are actived?

Do you know how?

Just say, the frame has JTEXTFIELD, JBUTTON, CHECKBOX....

When user click or tab either component, we will see the active message by using System.out.println("MSG");,

Example "USer click on JButton", "USer Click on CHeckBOX1", "USer Click on JTExtField".

Last time I got saw this tutorial. But now I have to retrieve it back....At that time I not save the URL down. smile.gif

Thanks.....
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 04:04PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month