概述:
当Day
类的start
字段end
或DayPeriod
发生更改时,组合框(即每个字段一个)将对此作出反应,显示由toString()
字段的方法。
问题:
虽然组合“getSelectedItem()
和getSelectedIndex()
确认在使用setSelectedItem
方法后发生了更改,但它不可见,即旧选择仍然可见。
详细说明:
DayPeriod
类包含私有Day start
和Day end
字段及其各自的设置者,实现PropertyChangeListener
。
public class DayPeriod implements PropertyChangeListener {
private Day start, end;
private PropertyChangeSupport propertyChangeSupport;
DayPeriod () {
setStart(new Day());
setEnd(new Day());
propertyChangeSupport = new PropertyChangeSupport(this);
addPropertyChangeListener(this);
}
void setStart (Day newDay) {
Day oldDay = start;
start = newDay;
try {
propertyChangeSupport.firePropertyChange("dayStart", oldDay, start);
}
catch (NullPointerException ex) {
}
}
void setEnd (Day day) {
Day oldDay = end;
end = day;
try {
propertyChangeSupport.firePropertyChange("dayEnd", oldDay, end);
}
catch (NullPointerException ex) {
}
}
public void addPropertyChangeListener (PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
@Override
public void propertyChange (PropertyChangeEvent event) { // listener
if (event.getPropertyName().equals("dayStart")) {
Day day = (Day) event.getNewValue();
if (getEnd().compareTo(day) < 0)
setEnd(day);
}
}
}
最后的propertyChange
方法只是为了确保start
更改end
,如果其值小于start
,则Day
会发生变化,即一段时间不能在它开始之前结束。
equals(Day other)
课程几乎就是工作日。它实现了Comparable
方法,toString()
接口和public class Day implements Comparable<Day> {
private DayOfWeek dayOfWeek;
Day () {
dayOfWeek = DayOfWeek.MONDAY;
}
Day (DayOfWeek day) {
dayOfWeek = day;
}
public String toString () {
return dayOfWeek.getDisplayName(TextStyle.FULL, OpeningHours.locale);
}
@Override
public int compareTo (final Day other) {
if (getDayOfWeek().compareTo(other.getDayOfWeek()) < 0)
return -1;
else if (getDayOfWeek().compareTo(other.getDayOfWeek()) > 0)
return 1;
else
return 0;
}
DayOfWeek getDayOfWeek () {
return dayOfWeek;
}
boolean equals (Day other) {
return getDayOfWeek().equals(other.getDayOfWeek());
}
}
方法;一切按预期工作。
DayCombo
这里有组合框:
我在面板中创建的extends JComboBox
类(propertyChange
)的实例也有public class DayCombo extends JComboBox<Day> implements ItemListener, PropertyChangeListener {
private DayPeriod period;
private boolean isStart;
{
for (DayOfWeek day : DayOfWeek.values())
addItem(new Day(day));
addItemListener(this);
}
DayCombo (DayPeriod period, boolean isStart) {
this.period = period;
this.isStart = isStart;
}
public void itemStateChanged (final ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Day selectedDay = (Day) getSelectedItem();
if (isStart)
period.setStart(selectedDay);
else
period.setEnd(selectedDay);
}
}
@Override
public void propertyChange (PropertyChangeEvent event) {
Day selectedDay = (Day) getSelectedItem();
Day otherDay = (Day) (event.getNewValue());
String propertyName = event.getPropertyName();
if (!isStart && propertyName.equals("dayStart")) {
// main focus, not working as expected
setSelectedItem(otherDay);
}
}
}
方法,因此只要上述事件触发,它们就会做出反应。
在构造函数中,组合框中填充了所有可能的日期,并将其自身添加到要观察的时段。
{{1}}
我想这不是关于任何听众的,而是更多关于组合框没有刷新可见的内容。
如果需要在代码中添加更多评论,请告诉我。