我在netbeans平台项目中遇到了一些麻烦,我把swing和javafx混合起来。
我有两个标签,其中一个标签有一个旋钮,另一个标签包含一个javafx TreeTableView。当按下按钮时,应该在TreeTableView中添加一个新行,但我无法实现这一点。
在添加新节点之前和之后,我会递归打印树。
Root before add:
Root
-Child 1
-Child 2
Root after add:
Root
-Child 1
-Child 2
-New child
因此,基础数据模型发生了变化,但UI并未反映这些变化。
任何指向我所缺少的内容都会有所帮助。
Code download (netbeans project (rar archive))
Code download (netbeans project (zip archive))
类之间的通信接口:
public interface XAddItem {
public void add();
}
UI topcomponent(持有treetableview)
import java.awt.BorderLayout;
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.scene.control.TreeTableView;
import javax.swing.JPanel;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.ServiceProvider;
@TopComponent.Description(
preferredID = "UITopComponent",
//iconBase="SET/PATH/TO/ICON/HERE",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "com.mycompany.ui.UITopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
displayName = "#CTL_UIAction",
preferredID = "UITopComponent"
)
@Messages({
"CTL_UIAction=UI",
"CTL_UITopComponent=UI Window",
"HINT_UITopComponent=This is a UI window"
})
@ServiceProvider(service = XAddItem.class)
public final class UITopComponent extends TopComponent implements XAddItem {
TreeItem<String> root = new TreeItem<>("Root");
final JFXPanel jfxPanel = new JFXPanel();
TreeTableView ttv = null;
public UITopComponent() {
initComponents();
setName(Bundle.CTL_UITopComponent());
setToolTipText(Bundle.HINT_UITopComponent());
init();
}
private void init() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(jfxPanel);
Platform.runLater(new Runnable() {
@Override
public void run() {
setup();
}
});
this.add(panel, BorderLayout.NORTH);
}
private void setup() {
TreeItem<String> child1 = new TreeItem<>("Child 1");
TreeItem<String> child2 = new TreeItem<>("Child 2");
root.getChildren().addAll(child1, child2);
TreeTableColumn<String,String> column = new TreeTableColumn<>("Column");
column.setPrefWidth(150);
//Defining cell content
column.setCellValueFactory((CellDataFeatures<String, String> p) -> new ReadOnlyStringWrapper(p.getValue().getValue()));
ttv = new TreeTableView(root);
ttv.getColumns().add(column);
jfxPanel.setScene(new Scene(ttv));
}
private void printTree(TreeItem<String> i, String level) {
System.out.println(level + i.getValue());
for (TreeItem<String> c : i.getChildren()) {
printTree(c, level + "-");
}
}
private void initComponents() {
setLayout(new java.awt.BorderLayout());
}
@Override
public void add() {
Platform.runLater(new Runnable() {
@Override
public void run() {
System.out.println("Root before add: ");
printTree(root, "");
root.getChildren().add(new TreeItem<>("New child"));
System.out.println("Root after add: ");
printTree(root, "");
}
});
}
}
UI topcomponent(保留按钮)
package com.mycompany.ui;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
@TopComponent.Description(
preferredID = "AddTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "com.mycompany.ui.AddTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
displayName = "#CTL_AddAction",
preferredID = "AddTopComponent"
)
@Messages({
"CTL_AddAction=Add",
"CTL_AddTopComponent=Add Window",
"HINT_AddTopComponent=This is a Add window"
})
public final class AddTopComponent extends TopComponent {
XAddItem add = Lookup.getDefault().lookup(XAddItem.class);
public AddTopComponent() {
initComponents();
setName(Bundle.CTL_AddTopComponent());
setToolTipText(Bundle.HINT_AddTopComponent());
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
setLayout(new java.awt.BorderLayout());
org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(AddTopComponent.class, "AddTopComponent.jButton1.text")); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
add(jButton1, java.awt.BorderLayout.CENTER);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
add.add();
}
private javax.swing.JButton jButton1;
}