School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,049 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,524 people online right now. Registration is fast and FREE... Join Now!



How to Make the TextField Focus?

How to Make the TextField Focus? Rate Topic: -----

#1 fsloke  Icon User is offline

  • D.I.C Regular
  • PipPipPip
  • Group: Members
  • Posts: 379
  • Joined: 19-December 07


Dream Kudos: 0

Posted 02 May 2008 - 03: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.
Was This Post Helpful? 0
  • +
  • -


#2 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 02 May 2008 - 03:19 PM

View Postfsloke, on 2 May, 2008 - 04:08 PM, said:

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 ?
Was This Post Helpful? 0
  • +
  • -

#3 fsloke  Icon User is offline

  • D.I.C Regular
  • PipPipPip
  • Group: Members
  • Posts: 379
  • Joined: 19-December 07


Dream Kudos: 0

Posted 02 May 2008 - 03:46 PM

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
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 02 May 2008 - 04:14 PM

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
Was This Post Helpful? 0
  • +
  • -

#5 fsloke  Icon User is offline

  • D.I.C Regular
  • PipPipPip
  • Group: Members
  • Posts: 379
  • Joined: 19-December 07


Dream Kudos: 0

Posted 03 May 2008 - 02: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
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 03 May 2008 - 07:18 AM

View Postfsloke, on 3 May, 2008 - 03:38 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 fsloke  Icon User is offline

  • D.I.C Regular
  • PipPipPip
  • Group: Members
  • Posts: 379
  • Joined: 19-December 07


Dream Kudos: 0

Posted 03 May 2008 - 05:48 PM

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. :)

Thanks.....
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month