Code example for JTextComponent

Methods: addKeyListenergetTextsetText

0
			input=new JTextField(8);
			add(input);			
			// enough room for "couldn't understand" 
			add(feedback);
		} 
		input.setText(startValue==null ? "" : parser.format(startValue));
		input.addKeyListener(new KeyAdapter() {
			@Override 
			public void keyReleased(KeyEvent e) { 
				parse(); 
			} 
		}); 
		parse(); 
	} 
 
	void parse() 
	{ 
		try 
		{ 
			parser.parse(input.getText());
		} 
		catch (Exception e)
		{} 
		feedback.setText("  "+parser.feedback());
		feedback.setForeground(parser.isUnderstood() ? Color.gray : Color.red);