gpt4 book ai didi

java - 如何在JTable中使用glaledlist的UndoRedoSupport或undoSupport?

转载 作者:行者123 更新时间:2023-12-01 05:16:51 26 4
gpt4 key购买 nike

我已经使用 GlazedList eventList 创建了一个 JTable。我搜索了 API,但不知道如何向该表添加撤消/重做的可能性。我在 API 中发现了以下类:

UndoRedoSupport undoRedoSupport = new UndoRedoSupport<"what here?">("argument?");
UndoSupport undoSupport = new UndoSupport<"what to write here?">("argument?");

有人知道如何使用它吗?

private void createComponents() {
EventList<Dien> eventList = new BasicEventList<Dien>();
actionList = GenericsUtil.makeList();
table = new WebTable();
searchField = new WebTextField(60);
String[] headers = new String[]{"Code", "Name", "Number"};
String[] properties = new String[]{"Code", "Name", "Number"};
TextFilterator<Dien> dienFilterator = new TextFilterator<Dien>() {
public void getFilterStrings(List baseList, Dien dien) {
baseList.add(dien.getCode());
baseList.add(dien.getName());
baseList.add(dien.getNumber());
}
};
MatcherEditor<Dien> textMatcherEditor = new TextComponentMatcherEditor<Dien>(searchField, dienFilterator);
eventList = toolModel.getDiens();
FilterList<Dien> filterList = new FilterList<Dien>(eventList, textMatcherEditor);
TableFormat tableFormat = GlazedLists.tableFormat(properties, headers, new boolean[]{true,true,true});
model = new EventTableModel<Dien>(filterList, tableFormat);
model.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
if(e.getType()==TableModelEvent.UPDATE){
if(!panel.isPendingChanges())
panel.setPendingChange(true);
}
}
});
selectionModel = new EventSelectionModel<Dien>(filterList);

table.setSelectionModel(selectionModel);
table.setModel(model);

}

最佳答案

GlazedLists 中内置的撤消/重做类没有公共(public)构造函数;相反,您可以通过 UndoRedoSupport.install() 静态方法安装对特定事件列表的支持。

当然,如果您使用 Swing,那么利用 Swing 的 UndoManager 类是有意义的,并且 GlazedLists 提供了一个简单的包装器及其 UndoSupport 类。同样,这只是使用其 install() 方法进行初始化。

我创建了一个简单的 Swing 示例应用程序作为示例来说明如何使用这些类。在我的示例中,我使用了一个由字符串和 JList 组成的简单 EventList。但它同样适用于任何 GlazedList 支持的组件 - UndoRedoSupport 适用于 EventList 本身,而不是 Swing 组件。

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.EventListModel;
import ca.odell.glazedlists.swing.UndoSupport;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.undo.UndoManager;


public class GlazedListsUndoSample {

private JFrame mainFrame;
private JButton addItemButton;
private JButton undoButton;
private JButton redoButton;
private UndoManager undoManager;

private EventList<String> eventList = new BasicEventList<String>();

public GlazedListsUndoSample() {
//populateAvailableBooks();
createGui();
mainFrame.setVisible(true);

}

private void updateButtons() {
//addBookButton.setEnabled(!books.isEmpty());
undoButton.setEnabled(undoManager.canUndo());
redoButton.setEnabled(undoManager.canRedo());
}

private void createGui() {
undoManager = new UndoManager();
UndoSupport.install(undoManager, eventList);

mainFrame = new JFrame("GlazedLists Undo Example");
mainFrame.setSize(600, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

EventListModel model = new EventListModel(eventList);
JList list = new JList(model);

JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(new JScrollPane(list), BorderLayout.CENTER);

JPanel addBookPanel = new JPanel(new BorderLayout());
addBookPanel.add(new JLabel("Item"), BorderLayout.WEST);
final JTextField titleTextField = new JTextField(50);
addBookPanel.add(titleTextField, BorderLayout.CENTER);

addItemButton = new JButton("Add Item");
addItemButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
eventList.add(titleTextField.getText());

updateButtons();
}
});
addBookPanel.add(addItemButton, BorderLayout.EAST);


JPanel buttonPanel = new JPanel();

undoButton = new JButton("Undo");
undoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (undoManager.canUndo()) {
undoManager.undo();
}

updateButtons();
}
});

redoButton = new JButton("Redo");
redoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (undoManager.canRedo()) {
undoManager.redo();
}

updateButtons();
}
});

updateButtons();

buttonPanel.add(undoButton);
buttonPanel.add(redoButton);

mainPanel.add(addBookPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);

mainFrame.getContentPane().setLayout(new BorderLayout());
mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GlazedListsUndoSample();
}
});
}
}

值得注意的是documentation确实强烈暗示了它的功能限制:

Not every change described in a ListEvent results in an undoable edit. Specifically, a mutation of a list element IN PLACE does not produce an undoable edit. For example, an ObservableElementList which observes a change of an element, or a call to List.set(int, E) with the same object at that index produce a ListEvent that does not have a corresponding UndoRedoSupport.Edit object. These ListEvents are ignored because they lack sufficient information to undo or redo the change.

In general UndoRedoSupport only makes sense for use with a BasicEventList or a trivial wrapper around a BasicEventList which does not affect the order or type of the elements, such as an ObservableElementList. Advanced transformations, such as SortedList or FilterList will not work as expected with this UndoRedoSupport class since their contents are controlled by information outside of themselves (Comparators and Matchers).

关于java - 如何在JTable中使用glaledlist的UndoRedoSupport或undoSupport?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11033833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com