Aggh! That is, the code works if I put it in the constructor of my game-initialiser object. However, I would like for the list to pop up when the player presses the “New Game” button, which means I would like to have the list waiting in the wings, then update in the actionPerformed method. As far as I understand the thread issues, this is safe, right?
However, if I do things this way, I get that grey rectangle again, instead of the list within a viewer that I get if the code is in the constructor. I’ve tried putting the initialisation code in the constructor and just adding it in actionPerformed, like this:
constructor () {
(...)
scenList = new JList(scenarionames);
scenViewer = new JScrollPane(scenList);
scenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scenList.setVisibleRowCount(-1);
scenViewer.setBounds(new Rectangle(-1000, -1000, 200, 500));
}
actionPerformed {
getContentPane().add(scenViewer);
repaint();
}
with the same result. Next I tried initialising scenViewer with width and height both zero, like so :
constructor () {
(...)
scenList = new JList(scenarionames);
scenViewer = new JScrollPane(scenList);
scenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scenList.setVisibleRowCount(-1);
scenViewer.setBounds(new Rectangle(100, 100, 0, 0));
getContentPane().add(scenViewer);
}
actionPerformed {
scenViewer.setBounds(new Rectangle(100, 100, 200, 500));
repaint();
}
again with the same result. Finally - success at last! - I initialised it to be off screen, then moved it back in when actionPerformed was called, thus :
constructor () {
(...)
scenList = new JList(scenarionames);
scenViewer = new JScrollPane(scenList);
scenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scenList.setVisibleRowCount(-1);
scenViewer.setBounds(new Rectangle(-1000, -1000, 200, 500));
getContentPane().add(scenViewer);
}
actionPerformed {
scenViewer.setBounds(new Rectangle(100, 100, 200, 500));
repaint();
}
This did have the side effect of suddenly blanking (making white) half my screen, but I was able to fix that in paint(). Could you explain to me why these apparently similar procedures give such different results? You’ve been very helpful so far.