Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subclass of JTextField

Status
Not open for further replies.

crmayer

Programmer
Nov 22, 2002
280
US
I have created a sub class of JTextField called Edit.
I would like to do is add a method in there that is called:
setCaption(String)

What this would do is add a JLabel or "caption" to a text field.

textField.setCaption("Name:"); - Which would produce something like this:

Name: ____________

Is this done already in Java?
If not, can somebody point me in the right direction to making this happen?

Thanks in advance
 
It seems you do not have much experience using Java Swing.
To do what you want, people may use a JLabel to show "Name:" and use another JTextField to accept input from user.
Please read more in the tutorials in sun.

To align JLabel on the left and JTextField on the right, you may use java.awt.GridLayout
 
You are correct, my experience is limited with Java.
The way you are listing above is how I currently do this, but I wanted to created a Text Field that I can add a caption to if I need one.

I am trying to write my first java application and I thought it would be nice to just be able to add this functionality to a text field.

Thanks
 
If that's your objective, maybe you can subclass JPanel and add the two components.

Cheers,
Dian
 
There is a lot of information you need to know before you customerize the user interface of a JTextField.
The following is a link to do little customerization to JTextField from someone.
You may also need to read the source code of JTextField.
The source code is stored in src.zip file in your jdk directory.
 
I wouldn't solve the problem by inheritance, but by composition.

A LabeledJTextField (which seems to be a better name for a labeled textfield than "edit") is not a JTextField, but has a JTextField.

setFont, setWidth and a lot of methods which you inherit from JTextField may have sense for the JTextField, for the JLabel, and for the compound object.
Inheriance will probably lead to a fast solution, which bites you in the long run.

don't visit my homepage:
 
I am not that familar with composition, so I will take a look at that.

Thanks for the insite, I will let you know what I come up with.

Thanks again for everybodys input.

Since I am new to Java, I look forward to anybody elses ideas or solutions to this.
Maybe leaving it along and just using a JLabel and JTextField is the best solution.
I just thought that down the road having the ability to have both in one convient solution would make life easier.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top