I'm learning Swing. I have a JFrame
which JPanel
containers - layout is a CardLayout
. Right now, when you fill the user and password fields, the panel switches and the "working" one is displayed.
I want to add one more panel for selecting a few options before you get into the working pane.
CardLayout cl = new CardLayout();
setLayout(cl);
loginPane = new LoginPanel();
devicePane = new DevicePanel();
workingPane = new WorkingPanel();
add(loginPane,"1");
add(devicePane,"2");
add(workingPane, "3");
LoginPanel
, DevicePanel
and WorkingPanel
are all Java classes that extend JPanel
.
Is this a good practice to follow or should I consider to do something else? Because I'm starting to see that I'll have to create listener from these panels to communicate the frame what to do.
Is good to use a
CardLayout
to switch between a login pane and a working pane?"
Yes. In fact, unless it makes sense to include on or more of the panels in a dialog (JDialog
or JOptionPane
), a CardLayout
is the way to go.
I'm starting to see that I'll have to create listener from these panels to communicate the frame what to do.
The problem here is extending components that likely do not need to be extended. By that I mean, LoginPanel
, DevicePanel
& WorkingPanel
should all be plain JPanel
instances. By extending panel, the code likely has encapsulated components & listeners which should all be part of one class.
Doing it as one class would negate the problem, as all the components and listeners would have the same scope (& reachability).