EDIT: I've left my original question as it was, below. If you wish to test the issue using the AnchorFX sourcecode and my code below, you should be able to recreate the problem. It happens in some other circumstances as well, and is similar to the issues in these two questions: Resize SwingNode in Pane and How to resize Swing control which is inside SwingNode in JavaFX8? Neither of whose answers proved helpful to me, but maybe the answer I found will help someone else in the future.
I have a JTable
inside a JScrollPane
and I need to embed it into a javafx application. I am trying to do this using the AnchorFX docking framework. I also need this SwingNode to somehow be inside a Control
(the two I have tried are ScrollPane
and SplitPane
) so that I can add a ContextMenu
to it which is consistent with the rest of the application.
The problem is, when I 'dock' and 'undock' tabs or simply resize the window or the panes inside the window, the JScrollPane
with the table in it does not resize properly.
I have modified one of the demos from the AnchorFX project to show my problem:
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);
}
}
So it turns out, SwingNode has a Parent who resizes itself appropriately, but SwingNode itself wasn't resizing, it is sized based on its contents. I solved this by making the following calls when a DockNode was docked or undocked:
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));
I'm not entirely sure why resetting the SwingNode's contents helped solve it, but it seemed to help so I'm leaving that line there.
I may edit the question later to help anyone else stymied by weird SwingNode resizing issues, since I don't think it's particularly searchable right now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With