Im trying to dynamically change a JPanel containing a form, as the user clicks different tabs the JPanel should present different forms fields.
I've Read the best way to implement dynamic changes on JPanel is by using a cardLayout that will control all Jpanel inside a Stack.
I don't know if this is the best way to implement this, but what I did was to make an abstract class called FormPanel that will only implement the size of the Form, and then extends this class as needed, with different fields.
public class MainFrame extends JFrame {
// private JTextArea textArea;
private TextPanel textPanel;
private ToolBar toolBar;
private JPanel cards;
private SearchPanel searchPanel;
private AddNewCarPanel addNewCarPanel;
public MainFrame(){
super("CarShop");
setLayout(new BorderLayout());
//textArea = new JTextArea();
toolBar = new ToolBar();
textPanel = new TextPanel();
searchPanel = new SearchPanel();
addNewCarPanel = new AddNewCarPanel();
cards = new JPanel(new CardLayout());
cards.add(searchPanel,"search");
cards.add(addNewCarPanel,"addNewCar");
toolBar.setPanelListener(new PanelListeners(){
@Override
public void PanelChange(String text) {
//textPanel.appendText(text);
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards,text);
}
});
add(cards,BorderLayout.WEST);
add(textPanel,BorderLayout.CENTER);
add(toolBar,BorderLayout.NORTH);
//add(searchPanel,BorderLayout.WEST);
this.setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
So my main goal is to toggle between searchPanel and addNewCarPanel using a CardLayout, I've tried different solutions but im struggling with the implementation.
If you think there is a better way to implement the different Forms, I will be happy to learn.