提问人:interesting 提问时间:5/17/2022 更新时间:5/17/2022 访问量:90
等待事件在另一个事件中完成
Wait for event to finish within another event
问:
我有一个 JTable,其中一个单元格是 LocalDate,我可以通过单击它来更新它,它会打开一个日期选择器。问题在于没有考虑修改,因为日期选取器事件等待用户的输入,但不会阻止。因此,处理编辑的事件在收到编辑之前结束。
我已经检查了这个问题:在程序继续在 java 中运行之前,如何等待事件完成,并尝试使用 CountDownLatch 阻止事件子项,但它冻结了应用程序。我不确定,但我想这是关于另一个事件中的事件阻塞导致麻烦的事情。
下面是一些要测试的代码:
对于编辑单元格:
package com.swing.datepicker;
import com.github.lgooddatepicker.components.DatePicker;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
public class DateOfBirthCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private LocalDate curValue;
private final JButton button;
public DateOfBirthCellEditor() {
button = new JButton();
button.addActionListener(this);
button.setBorderPainted(false);
}
@Override
public void actionPerformed(ActionEvent e) {
DatePicker datePicker = new DatePicker();
datePicker.setDate(curValue);
button.add(datePicker);
datePicker.openPopup();
datePicker.addDateChangeListener(dateChangeEvent -> {
curValue = dateChangeEvent.getNewDate();
System.out.println("should happens before - curValue : " + curValue);
});
System.out.println("should happens after - curValue : " + curValue);
button.remove(datePicker);
fireEditingStopped();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
curValue = (LocalDate) value;
return button;
}
@Override
public Object getCellEditorValue() {
return curValue;
}
}
主课 :
package com.swing.datepicker;
import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.time.LocalDate;
import java.util.List;
public class TableTest extends JFrame {
public TableTest() throws Exception {
super("TEST");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
initUI();
}
static class Dude {
String name;
LocalDate dateofbirth;
public Dude(String name, LocalDate dateofbirth) {
this.name = name;
this.dateofbirth = dateofbirth;
}
}
public void initUI() {
// Initial Settings
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
JPanel contentPane = (JPanel) this.getContentPane();
TableModel tableModel = new TableModel() {
final List<Dude> data = List.of(new Dude("John", LocalDate.of(1998, 6,30)));
final String[] headers = {"name", "date of birth"};
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return headers.length;
}
@Override
public String getColumnName(int columnIndex) {
return headers[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 1) {
return LocalDate.class;
}
return Object.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return switch (columnIndex) {
case 0 -> data.get(rowIndex).name;
case 1 -> data.get(rowIndex).dateofbirth;
default -> null;
};
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (aValue != null) {
switch (columnIndex) {
case 0 -> data.get(rowIndex).name = (String) aValue;
case 1 -> data.get(rowIndex).dateofbirth = (LocalDate) aValue;
}
}
}
@Override
public void addTableModelListener(TableModelListener l) {}
@Override
public void removeTableModelListener(TableModelListener l) {}
};
JTable table = new JTable(tableModel);
table.setDefaultEditor(LocalDate.class, new DateOfBirthCellEditor());
JPanel tablePanel = new JPanel();
tablePanel.add(table);
contentPane.add(tablePanel);
}
public static void main(String[] args) throws Exception {
TableTest window = new TableTest();
window.setVisible(true);
}
}
我在代码的其他部分使用了该日期选择器,这就是为什么我想保留它并在编辑单元格时执行一些奇怪的代码。我的pom中有这个:
<dependency>
<groupId>com.github.lgooddatepicker</groupId>
<artifactId>LGoodDatePicker</artifactId>
<version>11.2.1</version>
</dependency>
提前致谢。
答: 暂无答案
评论
fireEditingStopped
DateChangeListener
DateTableEditor
com.github.lgooddatepicker.tableeditors.DateTableEditor
TableEditorDemo
DateTableEditor