grifis78
12-02-2010, 12:39
Ciao ragazzi,
le ho davvero provate tutte, ma non capisco proprio come fare a validare l'input dell'utente su una cella di JTable. Sulla documentazione ufficiale (http://java.sun.com/docs/books/tuto...ents/table.html) c'è un esempio, ma si utilizza un array bidimensionale per caricare i dati che verrano visualizzati in seguito. Io invece utilizzo semplicemente un AbstractTableModel.
In particolare, vorrei che in una colonna vengano inseriti soltanto dati numerici nell'intervallo 0-100, mentre in un'altra dati double con solo due cifre decimali.
Ho trovato in rete questo codice:
JTable table = new JTable();
// Add some data...
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyTableCellEditor());
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
// This is the component that will handle the editing of the cell value
JComponent component = new JTextField();
// This method is called when a cell value is edited by the user.
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
// 'value' is value contained in the cell located at (rowIndex, vColIndex)
if (isSelected) { // cell (and perhaps other cells) are selected
}
// Configure the component with the specified value
((JTextField)component).setText((String)value);
// Return the configured component return component;
return component;
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
Ad esempio, se volessi validare un numero in ingresso che non debba contenere più di 3 cifre?
Grazie fin d'ora...
le ho davvero provate tutte, ma non capisco proprio come fare a validare l'input dell'utente su una cella di JTable. Sulla documentazione ufficiale (http://java.sun.com/docs/books/tuto...ents/table.html) c'è un esempio, ma si utilizza un array bidimensionale per caricare i dati che verrano visualizzati in seguito. Io invece utilizzo semplicemente un AbstractTableModel.
In particolare, vorrei che in una colonna vengano inseriti soltanto dati numerici nell'intervallo 0-100, mentre in un'altra dati double con solo due cifre decimali.
Ho trovato in rete questo codice:
JTable table = new JTable();
// Add some data...
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyTableCellEditor());
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
// This is the component that will handle the editing of the cell value
JComponent component = new JTextField();
// This method is called when a cell value is edited by the user.
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
// 'value' is value contained in the cell located at (rowIndex, vColIndex)
if (isSelected) { // cell (and perhaps other cells) are selected
}
// Configure the component with the specified value
((JTextField)component).setText((String)value);
// Return the configured component return component;
return component;
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
Ad esempio, se volessi validare un numero in ingresso che non debba contenere più di 3 cifre?
Grazie fin d'ora...