Re: Problem with JTable and DefaultCellEditor
On Fri, 26 May 2006 00:25:37 +0800, bparanj wrote
(in article <1148574337.708553.261140@j73g2000cwa.googlegroups.com>):
Thanks. Actually two of my cells in my table allow numeric or hyphen
characters only. So I have been experimenting with your code. It seems
like that since the default implementation of JTable does not provide
the validation that checks if the entered value is numeric or hyphen, I
have to write my own custom editor right?
So if I do that, I am back to square one again. It is showing the first
character even if is not a legal character and then it starts beeping
and prevents entering of illegal characters. How do I resolve this
issue? Thanks for your time again!
BP
http://www.ProblemSolvingSkill.net
no the way to do it is to pass in a reg expression, that sets the formatting.
here is an example i use for formatting part numbers.
where you see the regexpression , is where you set the formatting option.
all you need to do is , change 1 line and that forces what the user may enter
into the text area.
just link the class into the table, and you will be forced to enter 1-15
characters.
Then all you need to do is play about to get the actual characters you want,
(just look at reg expression on google)
package utils.Validation;
import java.awt.Component;
import java.awt.event.ComponentListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultFormatterFactory;
import utils.RegexFormatter;
//this class validates a model number when it is edited in a table
//it is used both by the shipment shedule screen & the lc details screen!!
public class validateModelInTable extends DefaultCellEditor {
JFormattedTextField ftf;
public validateModelInTable(){
super(new JFormattedTextField());
ftf = (JFormattedTextField)getComponent();
//ensure every model number is atleast 1 char & no more than 15
//TODO
//we need to get this from the database in the calling class!!, instead of
getting it from a const.
//this should also be "Graph", not "Print" because we do not want to allow
spaces
RegexFormatter formatter =new RegexFormatter("\\p{Graph}{1,15}");
formatter.setOverwriteMode(false); //set it so shit inserts!!
ftf.setFormatterFactory(new DefaultFormatterFactory(formatter));
}
//Override to invoke setValue on the formatted text field.
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected,
int row, int column) {
JFormattedTextField ftf =
(JFormattedTextField)super.getTableCellEditorComponent(
table, value, isSelected, row, column);
ftf.setValue(value);
return ftf;
}
//Override to ensure that the value remains an Integer.
public Object getCellEditorValue() {
JFormattedTextField ftf = (JFormattedTextField)getComponent();
Object o = ftf.getValue();
return o;
}
public boolean stopCellEditing() {
JFormattedTextField ftf = (JFormattedTextField)getComponent();
if (ftf.isEditValid()) {
try {
ftf.commitEdit();
} catch (java.text.ParseException exc) { }
return super.stopCellEditing();
}else {return false;
}
}
}