• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Ascii Validation in Swing.

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,


I have to validate given end user data compare it with ascii value.
I dont know how to do this.

Regards,
Amardeep
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this related to a swing component, or just a general comparison problem

[edit]
just saw the title - perhaps a JTextField with an InputVerifier.

if you want to prevent anything other than an ascii value - perhaps a DocumentFilter.

the more info you can provide on exactly what you're trying to do will
probably get you better responses
[ November 11, 2008: Message edited by: Michael Dunn ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're trying to validate data in a table, you could use something like this class:

import org.apache.log4j.Logger;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

/**
* Implements a cell editor that uses a text field to validate and edit Double values.
*/
public class DoubleEditor extends DefaultCellEditor {

private static final Logger log = Logger.getLogger(DoubleEditor.class);

private JTextField textField;
private double min, max;
private static final Color RED_COLOR = new Color(240, 175, 175);
private static final Border BLACK_BORDER = new LineBorder(Color.BLACK);

public DoubleEditor(double min, double max) {
this(new JTextField(), min, max);
}

public DoubleEditor(JTextField jt, double min, double max) {
super(jt);
textField = (JTextField) getComponent();
this.min = min;
this.max = max;

textField.setHorizontalAlignment(JTextField.TRAILING);

textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
textField.getActionMap().put("check", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
log.debug("text:" + textField.getText());
textField.postActionEvent();
}
});
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JTextField textField = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
textField.setText(value == null ? null : value.toString());
textField.setBackground(Color.WHITE);
textField.setBorder(BLACK_BORDER);
return textField;
}

public Object getCellEditorValue() {
JTextField ftf = (JTextField) getComponent();
return ftf.getText() == null || ftf.getText().trim().equals("") ? null : Double.parseDouble(ftf.getText());
}

/**
* Override to check whether the edit is valid, setting the value if it is and complaining if it isn't. If it's OK
* for the editor to go away, we need to invoke the superclass's version of this method so that everything gets
* cleaned up.
*/
public boolean stopCellEditing() {
JTextField textField = (JTextField) getComponent();
if (textField.getText().trim().equals("")) { // if it's the empty string, set it to null
textField.setText(null);
return super.stopCellEditing();
} else if (textField.getText() == null || isValid(textField.getText())) { // otherwise, if it's valid, set it

textField.setBackground(Color.WHITE);
return super.stopCellEditing();
} else { // otherwise, it's not valid
textField.selectAll();
textField.setBackground(RED_COLOR);
return false;
}
}

private boolean isValid(String text) {
try {
double num = Double.parseDouble(text);
return num >= min && num < max;
} catch (NumberFormatException nfe) {
log.debug("bad number: " + text);
}

return false;
}

}
[ November 15, 2008: Message edited by: J. Noah ]
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic