|
15.11.2005, 12:19
#33378650
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
|
Участник
Откуда: Гостья из будущего
Сообщения: 441
Рейтинг:
0
/ 0
|
|
|
|
Всем привет!
Помогите разобраться, почему не работает FocusListener?
То есть при переходе с одного поля на другое должно выполняться какое-то действие. А оно не выполняется.
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. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198.
import jcalendar.src.com.toedter.calendar.JDateChooser;
import javax.swing.*;
import javax.swing.border.Border;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.beans.VetoableChangeListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.awt.*;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.*;
public class DateRangeBean extends JPanel implements FocusListener
{
/**
* Компонент с двомя полями типа КАЛЕНДАРЬ (вибор даты)
* для вибора диапазона даты.
*
* @param d <code>JDateChoser</code> object
*/
// public DateRangeBean(String viraz)
public DateRangeBean()
{
locale = Locale.getDefault();
fromData.addFocusListener( this );
fromData = new JDateChooser();
fromData.setLocale(locale);
fromData.setDateFormatString("dd.MM.yyyy");
toData = new JDateChooser();
toData.setLocale(Locale.getDefault());
toData.setDateFormatString("dd.MM.yyyy");
toData.addFocusListener( this );
setLayout( new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets( 5 , 5 , 5 , 5 );
gbc.weightx = 0 ;
gbc.weighty = 0 ;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.BOTH;
Border etched = BorderFactory.createBevelBorder( 1 );
Border titled = BorderFactory.createTitledBorder(etched);
setBorder(titled);
add( new JLabel(povP1+" Від "+povK1),gbc, 0 , 0 , 0 , 0 );
add(fromData,gbc, 1 , 0 , 0 , 0 );
add( new JLabel(povP1+" до "+povK1),gbc, 2 , 0 , 0 , 0 );
add(toData,gbc, 3 , 0 , 0 , 0 );
fromData.addVetoableChangeListener( new VetoableChangeListener()
{
public void vetoableChange(PropertyChangeEvent event)
throws PropertyVetoException
{
Date v = ((Date)event.getNewValue());
if (v.after(toData.getDate()))
throw new PropertyVetoException("from > to",event);
}
});
toData.addVetoableChangeListener( new VetoableChangeListener()
{
public void vetoableChange(PropertyChangeEvent event)
throws PropertyVetoException
{
Date v = ((Date)event.getNewValue());
if (v.before(fromData.getDate()))
throw new PropertyVetoException("to < from",event);
}
});
//addWindowListener(new ExitDataListener());
}
public void focusGained(FocusEvent e)
{
if (e.equals(fromData))
{
Date f = (Date) fromData.getDate();
System.out.println("f="+f);
}
else if (e.equals(toData))
{
Date t = (Date) toData.getDate();
System.out.println("t="+t);
}
}
public void focusLost(FocusEvent e) {
if (e.equals(fromData))
{
Date f = (Date) fromData.getDate();
System.out.println("f1="+f);
}
else if (e.equals(toData))
{
Date t = (Date) toData.getDate();
System.out.println("t1="+t);
}
}
public void add(Component c, GridBagConstraints gbc,
int x, int y, int w, int h)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = w;
gbc.weighty = h;
add(c, gbc);
}
public void setFromData(Date v) //throw PropertyVetoException
{
locale = Locale.getDefault();
formatter = new SimpleDateFormat("dd.MM.yyyy", locale);
try
{
valueDate = formatter.parse(v.toString()); //Перевіряється чи може введена дата форматуватись у дату
}
catch (ParseException e)
{
e.printStackTrace();
}
fromData.setDate(valueDate);
}
public Date getFromData()
{
return fromData.getDate();
}
public void setToData(Object v) //throw PropertyVetoException
{
locale = Locale.getDefault();
formatter = new SimpleDateFormat("dd.MM.yyyy", locale);
try
{
valueDate = formatter.parse(v.toString().trim()); //Перевіряється чи може введена дата форматуватись у дату
}
catch (ParseException e)
{
e.printStackTrace();
}
toData.setDate(valueDate);
}
public Date getToData()
{
return toData.getDate();
}
public Dimension getPreferredSize()
{
return new Dimension (X,Y);
}
public static void main(String[] args)
{
final DateRangeBean form = new DateRangeBean();
submit.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Date from = form.getFromData();
Date to = form.getToData();
locale = Locale.getDefault();
formatter = new SimpleDateFormat("dd.MM.yyyy", locale);
String povP="";
String povK="";
if (from.after(to))
{
String pov1 = povP+"Введено невірний діапазон дат: "+povK;
String pov2 = povP+formatter.format(from)+" > "+formatter.format(to)+povK;
JOptionPane.showMessageDialog( null ,pov1+"\n"+pov2,
"Помилка діапазону дат",JOptionPane.WARNING_MESSAGE);
}
}
});
JFrame f = new JFrame(" Виберіть діапазон дат ");
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
f.getContentPane().add(form, BorderLayout.NORTH);
JPanel p = new JPanel();
p.add(submit);
f.getContentPane().add(p, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
private static JButton submit = new JButton("Ok");
private static final int X = 300 ;
private static final int Y = 80 ;
public static JDateChooser fromData = new JDateChooser();
public static JDateChooser toData = new JDateChooser();
private static Locale locale;
private static SimpleDateFormat formatter;
private Date valueDate;
public static String viraz = "";
private static String povP1="<html><div align='center'><font color='2655' face='Verdana' size='3'>";
private static String povK1="</font></b></div></html>";
}
|
|
|