编辑:我把原来的问题留在了下面。如果您希望使用AnchorFX源代码和下面的代码测试这个问题,您应该能够重新创建这个问题。这种情况在其他情况下也会发生,类似于这两个问题中的问题:调整窗格中的SwingNode大小和如何调整SwingNode中JavaFX8中的Swing控件的大小?,它们的答案都没有证明对我有帮助,但也许我找到的答案将在未来帮助其他人。
我在JTable中有一个JScrollPane,我需要将它嵌入到javafx应用程序中。我正在尝试使用AnchorFX对接框架来完成这个任务。我还需要这个SwingNode在Control (我尝试过的两个是ScrollPane和SplitPane)中,以便向它添加一个与应用程序其他部分一致的ContextMenu。
问题是,当我“停靠”和“卸载”选项卡或者只是调整窗口或窗口内的窗格大小时,带有表的JScrollPane不能正确地调整大小。
我修改了AnchorFX项目中的一个演示,以显示我的问题:
public class AnchorFX_substations extends Application {
@Override
public void start(Stage primaryStage) {
DockStation station = AnchorageSystem.createStation();
Scene scene = new Scene(station, 1024, 768);
DockNode node1 = AnchorageSystem.createDock("Node", generateJTableNode());
node1.dock(station, DockNode.DockPosition.CENTER);
DockNode subNode = AnchorageSystem.createDock("subNode 1", generateJTableNode());
subNode.dock(station, DockNode.DockPosition.LEFT);
subNode.floatableProperty().set(false);
DockNode subNode2 = AnchorageSystem.createDock("subNode 2", generateJTableNode());
subNode2.dock(station, DockNode.DockPosition.LEFT);
AnchorageSystem.installDefaultStyle();
primaryStage.setTitle("AnchorFX SubStation");
primaryStage.setScene(scene);
primaryStage.show();
}
private Control generateJTableNode() {
ScrollPane contextMenuPane = new ScrollPane();
SwingNode swingNode = new SwingNode();
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");
// Append a row
for(int i = 0; i < 200; i++) {
model.addRow(new Object[]{"col 1 row " + i, "col 2 row "+i});
}
JScrollPane scrollPane = new JScrollPane(table);
swingNode.setContent(scrollPane);
contextMenuPane.setFitToHeight(true);
contextMenuPane.setFitToWidth(true);
contextMenuPane.setContent(swingNode);
return contextMenuPane;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}发布于 2017-10-04 17:11:20
因此,SwingNode有一个可以适当调整自身大小的父级,但是SwingNode本身并没有调整大小,而是根据其内容进行调整。解决这个问题的方法是,在DockNode停靠或取消停靠时进行以下调用:
swingNode.setContent(swingNode.getContent());
int width = (int) swingNode.getParent().getBoundsInParent().getWidth();
int height = (int) swingNode.getParent().getBoundsInParent().getHeight();
swingNode.getContent().setPreferredSize(new Dimension(width, height));我不完全确定为什么重置SwingNode的内容有助于解决这个问题,但是它似乎很有帮助,所以我把这条线留在了那里。
稍后我可能会编辑这个问题,以帮助其他被怪异的SwingNode调整大小问题困扰的人,因为我不认为它现在是特别可搜索的。
https://stackoverflow.com/questions/46474641
复制相似问题