Question

I`m planning to use a panel as a view controler container.

//variables
CustomerSearch cs;
CardLayout cl;
string BUTTONPANEL="Page1";

Setup of variables

private void InitViews(){
    cl = new CardLayout(); 

    cs = new CustomerSearch();
    cl.addLayoutComponent(cs, BUTTONPANEL);
    ViewPanel.setLayout(cl);

    //cl.show(ViewPanel, BUTTONPANEL);   
}

the button click for updating the view:

    private void SearchBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          

    cl.show(ViewPanel, BUTTONPANEL);
    cs.repaint();
    ViewPanel.repaint();
}  

But when the event is fired, nothing happens? Can anyone tell me what I'm doing wrong?

Was it helpful?

Solution

Normally you do not add the components to the layout. They are added to the parent component, the one using the layout:

    private void initViews() {
        cl = new CardLayout(); 
        viewPanel.setLayout(cl);

        cs = new CustomerSearch();
        viewPanel.add(cs, BUTTONPANEL);
    }

This tutorial should help: How to Use CardLayout

It also is convention that variables, fields and methods are named starting with a lowercase. Classes and Interfaces start with uppercase.

EDIT
repaint is not needed in this case, the following listener should do:

    private void SearchBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        cl.show(viewPanel, BUTTONPANEL);
    }  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top