Hi folks,
My question boils down to this: is it standard structure in Swing programming to give listeners control over new components (e.g a new JPanel) for display and input, and to give that new component's listeners control over new components for display and input, and so on to infinity? Or does Java need to revert back to some sort of unifying class that ties all Swing components together in a procedural order?
At present, in my application that uses one JFrame only, in my listeners, my initial JFrame object is being passed as a parameter to all my JPanels so their listeners can call removeall() to clear the frame for a new JPanel. For example, short code as follows
public class MainFrame {
JFrame jfrm;
public MainFrame() {
jfrm = new JFrame("Main Frame");
JPanel mainPanel = new MainPanel(jfrm);
}
}
public class MainPanel extends JPanel {
public MainPanel(final JFrame mainFrame) {
JButton example = new JButton("Example");
example.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
mainFrame.removeall();
JPanel 2ndPanel = new 2ndPanel(mainFrame);
mainFrame.add(2ndPanel);
mainFrame.validate();
}
});
}
}
Is this the right structure - where it's the listeners that generate the new panels and not some unifying class? But if that's the case, how does Java's compiler ever get to mainFrame.validate() if there's a cascading infinity of listeners? I'm an old-school procedural programmer trying to program a Swing application in Java, and I reckon I might not have grasped the basic concepts of Swing programming. Look forward to any helpful answers, and thanks in advance!