java swing 界面重绘,Java swing-Jpanel不会重新渲染/重新绘制自身

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 macroModelList;// this is the list of all MacroModels

public void addMacroModel(MacroModel macroModel) {

macroModelList.add(macroModel);

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

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值