Привет всем!
Хочу написать редактор для ячейки типа "сума" в JTable.
Но что-то он меня не слушается.
Я установила ему максимальное число знаков после десятичной точки =2.
А он рисует сколько хочешь...
Может кто знает в чем дело?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
public DoubleEditor( double min, double max)
{
super ( new JFormattedTextField());
suma = (JFormattedTextField)getComponent();
minimum = new Double (min);
maximum = new Double (max);
NumberFormat formatSuma;
formatSuma = NumberFormat.getInstance(Locale.UK);
formatSuma.setMinimumFractionDigits( 1 );
formatSuma.setMaximumFractionDigits( 2 );
NumberFormatter fSuma = new NumberFormatter(formatSuma);
fSuma.setMinimum(minimum);
fSuma.setMaximum(maximum);
suma.setFormatterFactory( new DefaultFormatterFactory(fSuma));
suma.setValue(minimum);
suma.setHorizontalAlignment(JTextField.TRAILING);
suma.setFocusLostBehavior(JFormattedTextField.PERSIST);
suma.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0 ),"check");
suma.getActionMap().put("check", new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if (!suma.isEditValid())
{
if (userSaysRevert())
{
suma.postActionEvent();
}
}
else
try
{
suma.commitEdit();
suma.postActionEvent();
catch (java.text.ParseException exc) { }
}
});
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected,
int row, int column)
{
JFormattedTextField suma =
(JFormattedTextField) super .getTableCellEditorComponent
(table, value, isSelected, row, column);
suma.setValue( new Double (value.toString()));
return suma;
}
public Object getCellEditorValue()
{
JFormattedTextField suma = (JFormattedTextField)getComponent();
Object o = suma.getValue().toString().trim();
System.out.println("getCellEditorValue() o= "+o);
return o;
}
public boolean stopCellEditing()
{
JFormattedTextField suma = (JFormattedTextField)getComponent();
if (suma.isEditValid())
{
try
{
suma.commitEdit();
} catch (java.text.ParseException exc) { }
}
else
{ //text is invalid
if (!userSaysRevert())
{
return false;
}
}
return super .stopCellEditing();
}
protected boolean userSaysRevert()
{
Toolkit.getDefaultToolkit().beep();
suma.selectAll();
Object[] options = {"Редагувати",
"Повернутись"};
int answer = JOptionPane.showOptionDialog(
SwingUtilities.getWindowAncestor(suma),
"Величниа повинна бути цілим числом між "
+ minimum + " і "
+ maximum + ".\n"
+ "Ви можете продовжити редагування "
+ "або повернути правильне занчення велични.",
"Invalid Text Entered",
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE,
null ,
options,
options[ 1 ]);
if (answer == 1 )
{ //Revert!
suma.setValue(suma.getValue());
return true;
}
return false;
}