Domanda

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?

È stato utile?

Soluzione

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);
    }  
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top