0

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.

NyaSol
  • 537
  • 1
  • 5
  • 21
  • I'm not sure how your abstract class relates to your problem. Side note: CardLayout will give the container a preferred size that allows all cards to fit, and so I'm not sure why you even need the "FormPanel". Side note 2: you will want to learn and use [Java naming conventions](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. – Hovercraft Full Of Eels Jul 12 '19 at 14:22
  • 1
    Also: you don't show the JPanel that uses CardLayout, and your posted code is not a valid [mre], code that we can compile and run, yet. Please help make your question easier to answer. – Hovercraft Full Of Eels Jul 12 '19 at 14:24
  • Ok, so your suggesting to implement one CardLayout class and set the layout, and then add the different JPanel forms on top of it? * will review your comments, but I cant provide minimal reproducible code, I will have to add all different components code as well, maybe a snapshot will help? – NyaSol Jul 12 '19 at 14:36
  • _If you think there is a better way to implement the different Forms, I will be happy to learn._ I suggest [JTabbedPane](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTabbedPane.html). If you aren't required to use _Swing_, have you considered [JavaFX](https://en.wikipedia.org/wiki/JavaFX) ? – Abra Jul 12 '19 at 14:37
  • I'm suggesting that you use CardLayout as is recommended in the [CardLayout tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). If you have not read this link, you should do so asap. – Hovercraft Full Of Eels Jul 12 '19 at 14:38
  • And no, not a snapshot. Please read the [mre] link as it will explain what I am requesting, something I strongly believe that you *can* create and provide. This isn't the full program but rather a new small example demo program. – Hovercraft Full Of Eels Jul 12 '19 at 14:39
  • [Here is a decent answer with some CardLayout best practices](https://stackoverflow.com/questions/36782236/using-cardlayout-for-multiple-jpanels-and-nothing-displays/36782403) – Hovercraft Full Of Eels Jul 12 '19 at 14:51
  • None of the examples seems to help me, I took the time to properly read(and run) the documents, above is the working code. lesson learned - Read the docs!. thank you for your help! – NyaSol Jul 12 '19 at 15:34

0 Answers0