- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个带有 rootNode 的 Jtree,然后创建另一个异步更新根节点的线程。
如果我在某个 JPanel 中独立运行这个 Jtree,它的效果会非常好,它甚至可以在项目中的某个地方工作,但我被要求在一些新的 swing 组件中使用这个 Jtree。
在新的 Swing Panel 中,它不会完全填充,它仅填充在 Jtree 在屏幕上呈现之前插入的节点(在开始时几毫秒)。一旦 Jtree 被渲染,它就不会被更新。现在有趣的部分是我还在节点上创建了一个鼠标监听器,以便我可以通过右键单击创建节点功能来创建一个新节点,并创建该新节点并将其添加到 Jtree 根节点上。
要添加的重要一点是,我使用 newThread(){void run}).start() 方法创建一个线程来在 Jtree 上添加节点,因为我之前从未觉得需要 SwingUtilities.invokeLater 方法。但现在,如果我使用 SwingUtilities.invokeLater 方法,主窗口也不会打开,它只是在启动过程中停止,我刚刚检查了 SwingUtilities.invokeLater 也可以与旧组件一起正常工作,当然独立工作也很好。
我确实调用 model.nodeStructureChanged(changedNode);添加节点后,这就是它之前工作正常的原因。
请帮忙,代码很难提取,并且 Jtree 代码之前工作正常,可能是某些组件阻止包含的小部件异步刷新自身?
编辑更新以包含一些代码,我正在使用 Nick 提供的 Temp 类:-
public BasicGraphEditor(String appTitle, mxGraphComponent component)
{
// Stores and updates the frame title
this.appTitle = appTitle;
// Stores a reference to the graph and creates the command history
graphComponent = component;
final mxGraph graph = graphComponent.getGraph();
undoManager = createUndoManager();
// Do not change the scale and translation after files have been loaded
graph.setResetViewOnRootChange(false);
// Updates the modified flag if the graph model changes
graph.getModel().addListener(mxEvent.CHANGE, changeTracker);
// Adds the command history to the model and view
graph.getModel().addListener(mxEvent.UNDO, undoHandler);
graph.getView().addListener(mxEvent.UNDO, undoHandler);
// Keeps the selection in sync with the command history
mxIEventListener undoHandler = new mxIEventListener()
{
@Override
public void invoke(Object source, mxEventObject evt)
{
List<mxUndoableChange> changes = ((mxUndoableEdit) evt
.getProperty("edit")).getChanges();
graph.setSelectionCells(graph
.getSelectionCellsForChanges(changes));
}
};
undoManager.addListener(mxEvent.UNDO, undoHandler);
undoManager.addListener(mxEvent.REDO, undoHandler);
// Creates the graph outline component
graphOutline = new mxGraphOutline(graphComponent);
// Creates the library pane that contains the tabs with the palettes
libraryPane = new JTabbedPane();
/////////////////////////////////////////////////
// Only change i have done here: start
////////////////////////////////////////////////
Temp tempExplorer = new Temp();
libraryPane.add("new Explorere", tempExplorer);
/////////////////////////////////////////////////
// Only change i have done here: End
////////////////////////////////////////////////
// Creates the inner split pane that contains the library with the
// palettes and the graph outline on the left side of the window
JSplitPane inner = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
libraryPane, graphOutline);
inner.setDividerLocation(320);
inner.setResizeWeight(1);
inner.setDividerSize(6);
inner.setBorder(null);
// Creates the outer split pane that contains the inner split pane and
// the graph component on the right side of the window
JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inner,
graphComponent);
outer.setOneTouchExpandable(true);
outer.setDividerLocation(200);
outer.setDividerSize(6);
outer.setBorder(null);
// Creates the status bar
statusBar = createStatusBar();
// Display some useful information about repaint events
installRepaintListener();
// Puts everything together
setLayout(new BorderLayout());
add(outer, BorderLayout.CENTER);
add(statusBar, BorderLayout.SOUTH);
installToolBar();
// Installs rubberband selection and handling for some special
// keystrokes such as F2, Control-C, -V, X, A etc.
installHandlers();
installListeners();
updateTitle();
}
上面的类来自 Jgraph 库 https://github.com/jgraph/jgraphx我只是像上面一样添加 jtree 组件,没有其他更改。请帮忙。
最佳答案
除非明确声明,否则 Swing 不是线程安全的。在 the JavaDocs for JTree ,它明确表示这不是线程安全的。如果您在 EDT 之外的线程中更新它,则无法保证任何内容都能正常工作。因此,如果您想从不同的线程更新 JTree,则需要使用 SwingUtilities.invokeLater(Runnable run);
以便将请求放在 EDT 上。
我建议使用一个数据结构来存储 JTree 的信息,并且仅使用 JTree 进行用户交互(而不是数据存储)。
编辑
下面是在组件模型中使用 SwingUtilities.invokeLater()
更新 JTree 的示例。无需您发布任何代码,这是我必须使用的最好的方法。请尝试使用它来重现您的问题(将您的代码片段添加到此示例中,直到您缩小问题范围)。
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Temp extends JPanel{
JTree tree = new JTree();
public Temp(){
JScrollPane jsp = new JScrollPane(tree);
// Creates the library pane that contains the tabs with the palettes
JTabbedPane libraryPane = new JTabbedPane();
libraryPane.add("new Explorere", jsp);
// Creates the inner split pane that contains the library with the
// palettes and the graph outline on the left side of the window
JSplitPane inner = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
libraryPane, new JPanel());
inner.setDividerLocation(320);
inner.setResizeWeight(1);
inner.setDividerSize(6);
inner.setBorder(null);
// Creates the outer split pane that contains the inner split pane and
// the graph component on the right side of the window
JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inner,
new JPanel());
outer.setOneTouchExpandable(true);
outer.setDividerLocation(200);
outer.setDividerSize(6);
outer.setBorder(null);
// Puts everything together
setLayout(new BorderLayout());
add(outer, BorderLayout.CENTER);
}
public static void main(String[] args) {
final Temp temp = new Temp();
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(temp);
frame.pack();
frame.setVisible(true);
}});
Thread updater = new Thread(temp.new CustomThread());
updater.start();
}
public class CustomThread implements Runnable{
@Override
public void run() {
for(int i = 0; i < 1000; i++){
updateTree("New Item "+ i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void updateTree(final String nodeToAdd){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeToAdd);
model.insertNodeInto(child, root,root.getChildCount());
tree.scrollPathToVisible(new TreePath(child.getPath()));
}});
}
}
}
关于java - JTree 异步节点创建与 JGraph 库不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13975810/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Recreating a Dictionary from an IEnumerable 在 Dictiona
是否可以使用命令行版本的 ImageMagick 修剪图像(比如带有 alpha 的 PNG),使输出图像的宽度和高度都是偶数(不是奇数)? 准确地说,应该先修剪输出图像,然后用透明像素填充。我需要这
我有一个订单的Map,可以由许多不同的线程访问。我想控制访问,所以考虑以下简单的数据结构+包装器。 public interface OrderContainer { boolean cont
我有以下代码,现在只是 div 中的一个 Logo ,但我正在尝试添加一些导航单元格,稍后我将对其进行样式设置。问题是,我似乎无法让它们与(除此之外) Logo “一致”,它们总是下降到下一行。我做错
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
有没有办法将种子值传递给 d3-cloud 或其他基于 javascript 的标签云,以使其在页面加载之间保持一致? 我们的客户希望使用标签云作为导航/发现辅助工具,但由于 d3-cloud 会在每
我有一条由用户使用 D3.js 绘制的路径。 我想在我的用户绘制路径上定义一个破折号数组,但是,随着它改变其形状和长度,破折号的行为不一致并且间隙在移动并变得越来越小。 这是一个代码笔: https:
只是为了研究UINavigationBar和UIStatusBar的UI,我把Navigation Bar Style改成了Black,并且取消勾选Bar visibility,即Shows Navi
我最近在我的家用机器 (OSX 10.9) 和我的远程服务器 (Ubuntu 12.04 64 位) 上安装了 unison。 我在这两个地方都安装了 2.40.102 版本。我在我的 Mac 上使用
我正在使用 migrate 创建 SQL 数据库模式并用初始数据填充它。后来使用 SQLAlchemy 来处理这个数据库。 我如何测试我的 SQLAlchemy 模型是否与 migrate 生成的真实
道歉对这一切来说还是新鲜事。我正在创建一个网页,并在两个单独的 div 中将图像和文本并排放置。我已经设法将它们放在页面上我想要的位置,但是当我调整页面大小时,文本会调整大小,但图像不会。我希望文本底
在翻阅Cassandra和HBase的阅读资料时,我发现Cassandra并不一致,但HBase是一致的。没有找到任何合适的阅读 Material 。 有人可以提供有关此主题的任何博客/文章吗? 最佳
我需要计算 MacOS 中文件夹的大小。该尺寸值必须与 Finder 一致。我尝试了几种方法来做到这一点。但结果总是与Finder不同。 以下方法是我尝试过的。 typedef struct{
问:我可以使用 C++ 中的任何编译时机制来自动验证模板类方法集是否从类特化到特化相匹配? 示例:假设我想要一个类接口(interface),它根据模板值专门化具有非常不同的行为: // forwar
我想使用 SelectKBest 选择前 K 个特征并运行 GaussianNB: selection = SelectKBest(mutual_info_classif, k=300) data_t
我想要一个位于页面中央的 div,其中包含一行(两个单词)的 h1 文本,并且该文本与 div 的长度对齐;意思是,字母留出空间(同时保持它们的大小)以占据 div 的整个宽度,并且不要超出 div。
我试图更新我的服务器,所以我通过 ssh 运行以下命令: sudo do-release-upgrade 我收到以下错误: Errors were encountered while processi
我想验证单应矩阵会给出好的结果,而这个 this answer 有答案 - 但是,我不知道如何实现答案。 那么谁能推荐我如何使用 OpenCV 计算 SVD 并验证第一个奇异值与最后一个奇异值的比率是
我最近更新到 cocoapods 0.36 并对内部规范做了一些更改,现在 podspec 不再有效。我用 0.35 验证了此规范的先前版本 (0.3.8),但使用 0.36 失败。很明显 cocoa
我有两个并排设置的 TableView ,我需要它们同时滚动。因此,当您滚动一个时,另一个也会同时滚动。 我进行了一些搜索,但找不到任何信息,但我认为这一定是有可能的。 我的 TableView 都连
我是一名优秀的程序员,十分优秀!