Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2004

Tip: Looking for answers? Try searching our database.

JFormattedTextField not retaining input.

Thread view: 
Stuart Leonard - 29 May 2004 18:03 GMT
When I enter keyboard input into the JFormattedTextField object, the input
is not being retained when the field has lost focus.

I am using SDK/JRE 1.4.0, Windows XP Professional.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.text.MaskFormatter;
import java.text.ParseException;
public class TestFrame extends JFrame
{
public JFormattedTextField low;
public JFormattedTextField high;
public JFormattedTextField incr;

public TestFrame() {

 JPanel panel = new JPanel(new BorderLayout());

 // add selection panel
 MaskFormatter mf1 = null;
 MaskFormatter mf2 = null;
 try
 {
  mf1 = new MaskFormatter("####");
     mf1.setPlaceholderCharacter('_');
  mf2 = new MaskFormatter("###");
     mf2.setPlaceholderCharacter('_');
     }
     catch (ParseException e){
    }

 low = new JFormattedTextField(mf1);
 high = new JFormattedTextField(mf1);
 incr = new JFormattedTextField(mf2);

 JPanel select = new JPanel(new GridBagLayout());
 GridBagConstraints gbc = new GridBagConstraints();
 gbc.fill = GridBagConstraints.NONE;
 addgbc(select, new JLabel("Low Temperature Range . . ."),gbc,0,0);
 addgbc(select, low,gbc,1,0);
 addgbc(select, new JLabel("High Temperature Range  . ."),gbc,0,1);
 addgbc(select, high,gbc,1,1);
 addgbc(select, new JLabel("Increment Degrees . . . . ."),gbc,0,2);
 addgbc(select, incr,gbc,1,2);
 panel.add(select, BorderLayout.NORTH);
 JFrame frame = new JFrame("Test JFormattedTextField");
 frame.getContentPane().setLayout(new BorderLayout());
 frame.getContentPane().add(panel, BorderLayout.NORTH);
 frame.pack();
 frame.setVisible(true);

}
private void addgbc(Container cont, JComponent comp, GridBagConstraints
gbc, int x, int y) {
 gbc.gridx=x; gbc.gridy=y;
 cont.add(comp,gbc);
}
public static void main (String[] args) {

 new TestFrame();
}

}
Gerard H. Pille - 29 May 2004 20:41 GMT
> When I enter keyboard input into the JFormattedTextField object, the input
> is not being retained when the field has lost focus.
[quoted text clipped - 62 lines]
>
> }

The input IS being retained in your example.

That is, when the input conforms to the mast.

Try providing four digits in the first two fields, input will be
retained.  The default behaviour of JFormattedTextField kicks in when
you provide less digits then required by the mask: COMMIT_OR_REVERT.

It reverts.

Maybe you want different masks?  Or a numberformat iso. a maskformat?

Geert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.