Java swing — Jpanel not rerendering/repainting itself

感情迁移 提交于 2019-12-24 16:04:22

问题


Im using a JPanel with propertyChangeListener and want it to rerender itself based on whenever a particular variable model changes. My code for the same is as follows --

public class LabelMacroEditor extends JPanel implements PropertyChangeListener {

    private static final long serialVersionUID = 1L;
    private LabelMacroModel model;

    public LabelMacroEditor(LabelMacroModel bean) {

        this.model = bean;
        model.addPropertyChangeListener(this);
        setupComponents();
        validate();
        setVisible(true);
    }

    public void setupComponents()
    {

        Box allButtons =  Box.createVerticalBox();
        JScrollPane macroModelScroller =  new JScrollPane(allButtons);
        macroModelScroller.setPreferredSize(new Dimension(300, 200));
        for(MacroModel macroModel : model.getMacroModelList())
        {
            LabelMacroEditorEditableEntity macroEditorEntity =  new LabelMacroEditorEditableEntity(macroModel);
            Box entityBox =  Box.createHorizontalBox();
            entityBox.add(macroEditorEntity.getUpButton());
            entityBox.add(Box.createHorizontalStrut(15));
            entityBox.add(macroEditorEntity.getMacroDetailsButton());
            entityBox.add(Box.createHorizontalStrut(15));
            entityBox.add(macroEditorEntity.getDownButton());

            allButtons.add(entityBox);
        }
        add(macroModelScroller);
    }

    @Override
    public void propertyChange(PropertyChangeEvent arg0) {
        revalidate();
        repaint();
    }
}

When i use the debug mode in eclipse i can see that whenever there is a change to model it triggers off the call propertyChange and it also runs over revalidate and repaint but only the JPanel display remains the same. It does not seem to be rerendering itself. Anything fundamental that I'm missing here ?

EDIT :

An example snippet of a property im changing is as follows --

labelMacroModel.addMacroModel(addedMacroModel);

where labelMacroModel is of the type LabelMacroModel and addedMacroModel is of the type Macro

Now the relevant part of LabelMacroModel class that fires off the property change is as follows --

private List<MacroModel> macroModelList;// this is the list of all MacroModels
public void addMacroModel(MacroModel macroModel) {
        macroModelList.add(macroModel);

        pcs.fireIndexedPropertyChange("LabelMacroModel", macroModelList.size(), null, macroModel);
    }

回答1:


Its not clear how you are changing the components in the panel. If panel is not updated then repaint/revalidate will have no effect. I think you should not need revalidate/repaint to be called explicitly if you are not modifying the way components are laid out. JButton.setText should for example change the label of the button without need of calling repaint.




回答2:


To expand on the answer by AKJ above, I think you should be reconstructing your components on property change. So doing a remove all then readding is one way to do this. Once you get this working you could be more selective about pushing the model update into the GUI eg if a new entry has been added then just add a new component to reflect this. The remove all / readd is fine for a lot of cases though. HTH.



来源:https://stackoverflow.com/questions/9761005/java-swing-jpanel-not-rerendering-repainting-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!