问题描述
我将JPanel与propertyChangeListener结合使用,并希望它根据特定变量model
的更改而重新呈现.我的相同代码如下-
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();
}
}
当我在eclipse中使用调试模式时,我可以看到,只要model
发生更改,它就会触发呼叫propertyChange
,并且它还会在revalidate
和repaint
上运行,但只有JPanel
显示保持不变.它似乎并没有重新呈现自己.我在这里想念的任何基本知识吗?
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 ?
更改属性的示例片段如下-
An example snippet of a property im changing is as follows --
labelMacroModel.addMacroModel(addedMacroModel);
其中labelMacroModel
是LabelMacroModel
类型,而addedMacroModel
是Macro
现在触发属性更改的LabelMacroModel
类的相关部分如下-
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);
}
推荐答案
目前尚不清楚如何更改面板中的组件.如果面板未更新,则重新粉刷/重新验证将无效.我认为,如果不修改组件的布局方式,则无需显式调用revalidate/repaint.例如,JButton.setText应该更改按钮的标签,而无需调用repaint.
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.
这篇关于Java Swing-Jpanel不会重新渲染/重新绘制自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!