(coming out of my PieceWindow question from earlier this week)
This is likely not the best way to get the information I’m looking for, so if the answer is “it just doesn’t work”, that’s ok. It’s just this is the easies way for me without asking the module maintainer to change things, so I wanted to explore it fully!
I’ve written some recursive code to drill into the PieceWindow Swing components and try to find the particular one I’m looking for. The goal is to find the title of a specific tabbed panel, and then find a specific list item on a JList somewhere within it.
The problem is that when I get to the JTabbedPane I want, I’m unable to go deeper. getComponentAt (which should be correct) returns an empty JPanel, and getTabComponentAt returns null.
Thanks! And again, it’s ok if this can’t be easily solved, I’m half-way to deserting this feature already.
Some details:
Get the PieceWindow, start a recursive search for a certain thing.
for (final PieceWindow window : GameModule.getGameModule().getAllDescendantComponentsOf(PieceWindow.class)) {
if("Game pieces".equals(window.getConfigureName())){
var component = window.getComponent();
if(component instanceof Container)
searchContainers("Acclamator I-class Assault Ship", (Container) component, null);
}
}
The recursive search, when it finds a tab with a specific name it does the rest of the search with that name.
private void searchContainers(String searchFor, Container container, String lastFoundFactionName) {
// getTotalCount and getComponent count abstract away the differences between JTabbedPane and Container,
// I don't think they are important for the example.
for (var k = 0; k < getTotalCount(container); k++) {
var component = getComponent(container, k);
if (component instanceof JTabbedPane) {
var tabbedPane = (JTabbedPane) component;
for (int i = 0; i < tabbedPane.getComponentCount(); i++) {
var tabTitle = tabbedPane.getTitleAt(i);
var tabComponent = tabbedPane.getComponentAt(i);
// TODO: Repro problem:
// 1. Put a breakpoint on the next line.
// 2. Continue until tabbedPane.getTitleAt(i) == "Republic"
// 3. Check both tabbedPane.getComponentAt(i) and tabbedPane.getTabComponentAt(i)
// Expected: One of them to be a JSplitPane, as seen in tabbedPane.pages
// Actual: getComponentAt returns an empty JPanel, getTabComponentAt returns null
if(tabComponent instanceof Container) {
if (Arrays.stream(FactionNames).anyMatch(name -> name.equals(tabTitle)))
searchContainers(searchFor, (Container) tabComponent, tabTitle);
else
searchContainers(searchFor, (Container) tabComponent, lastFoundFactionName);
}
}
} else if(component instanceof JList) {
// not important for this example
} else if (component instanceof Container)
searchContainers(searchFor, (Container) component, lastFoundFactionName);
}
}
I’ve attached a screenshot of the tabbedPane, along with the component list I actually want to get access to. There is a private field pages that I would have thought would be returned by getComponantAt(i).