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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maskformatting HELP 1

Status
Not open for further replies.

yigit

Technical User
Jun 28, 2006
14
0
0
TR
Hi everyone,
I read the info on: about masking a textfield. Unfortunately i didn't plan to mask the textfields beforehand so i created the textfields as such; The last 2 textfields(fields[3]and fields[4]) will have dates in them, In the database i created those as 'nvarchar'. Anyway how do i mask the textfield as that it accepts inputs like 'xx/xx/xxxx' only in the last 2 textfields...Is it possible to do this in this code; I made several attempts but it seems that Java hates me;). And thats the end of my essay:p thanks for your time.
private void placeLabelsAndFields(String[] labels) {
int[] widths = {10, 15, 15, 15, 15};

fields = new JTextField[labels.length];

for (int i = 0; i < labels.length; i++) {
fields = new JTextField();
if (i < widths.length) {
fields.setColumns(widths);
}
JLabel lab = new JLabel(labels, JLabel.RIGHT);
lab.setLabelFor(fields);
labelPanel.add(lab);
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));

if (i == labels.length - 1) {
isOnlineCheckbox = new JCheckBox();
p.add(isOnlineCheckbox);
} else {
p.add(fields);
}
fieldPanel.add(p);
}
}

Mille grazie
yigit


Don't just do something, stand there!
 
I don't see any attempt to use MaskFormatter in the code.
You need JFormattedTextFields, not JTextField, and something like this:
Code:
 MaskFormatter mf = null;
try
{
	mf = new MaskFormatter (labels[i]);
}
catch (Exception e)
{
	System.err.println (e.getMessage ());
}
fields [i] = new JFormattedTextField ();
mf.install (fields [i]);

seeking a job as java-programmer in Berlin:
 
It doesn't really matter that you used JTextField objects in the first place. The JFormattedTextField that Stefan correctly tells you to use is derived from the JTextField class. So you should be okay to simply change the fields you want to mask from JTextFields to JFormattedTextFields.

You should then be able to take a MaskFormatter created with a mask of "##/##/####" and install it on your JFormattedTextFields.

Use the java.text.SimpleDateFormat class, suitably configured, to turn the text input from your text fields into fully fledged java.util.Date classes.

You probably should change your database fields to be Dates too. You'll need to create java.sql.Date objects from your java.util.Date ones, and these can be squirted directly into the database (with PreparedStatements for instance).

... and it often feels like Java hates me too. Just keep at it and know that it will get easier with time.

Tim
 
thanks ppl but i tried a million ways and still, nothing. I have been trying to do the masking in this code piece but do you think i should rewrite how i create the texfields? And btw just a small question;
i created a normal Jbutton, then tried to add a .gif image to it...isnt't this how its done?

insert = new JButton(new ImageIcon("images/insert.gif"));
or
ImageIcon icon = new ImageIcon("images/insert.gif", "");
insert.setIcon(icon);

can u see anything wrong

mille grazie
yigit

Don't just do something, stand there!
 
Either way for the icon should work, as long as the gif is in the correct location relative to where you are executing your program from.

As for the formatting, can you post the code as you've got it with the JFormattingTextFields in?

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top