自己写了一个ListModel在初始化的时候传递集合
//所有要展示的数据
List<String> lst=null;
public cListModel(List<String> lst) {
super();
this.lst = lst; }
@Override
public Object getElementAt(int index) {
return lst.get(index);
} @Override
public int getSize() {
if(lst!=null)
return lst.size();
return 0;
}
然后再界面上
scrollPane.setBounds(27, 71, 407, 221);
panel.add(scrollPane);
lst=new ArrayList<String>();//数据源
lst.add("chonger");
lm=new cListModel(lst);//模型对象
list=new JList(lm);//实例化控件
list.setBounds(27, 71, 407, 221);
scrollPane.setViewportView(list);
默认显示的是对的 现在 我想手动的向Jlist中添加值
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lm.getLst().add("jiaojiao");
list.invalidate();
list.repaint();
// scrollPane.invalidate();
// scrollPane.repaint();
}
});scrollPane显示不出来数据,就会变成白色的。。 为什么哦?
我如果不讲JList放在JScrollPane中就能正常的添加删除。。
请问下 为什么放在JScrollPane中不能正常显示呢? 我该怎么做? 谢谢了
//所有要展示的数据
List<String> lst=null;
public cListModel(List<String> lst) {
super();
this.lst = lst; }
@Override
public Object getElementAt(int index) {
return lst.get(index);
} @Override
public int getSize() {
if(lst!=null)
return lst.size();
return 0;
}
然后再界面上
scrollPane.setBounds(27, 71, 407, 221);
panel.add(scrollPane);
lst=new ArrayList<String>();//数据源
lst.add("chonger");
lm=new cListModel(lst);//模型对象
list=new JList(lm);//实例化控件
list.setBounds(27, 71, 407, 221);
scrollPane.setViewportView(list);
默认显示的是对的 现在 我想手动的向Jlist中添加值
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lm.getLst().add("jiaojiao");
list.invalidate();
list.repaint();
// scrollPane.invalidate();
// scrollPane.repaint();
}
});scrollPane显示不出来数据,就会变成白色的。。 为什么哦?
我如果不讲JList放在JScrollPane中就能正常的添加删除。。
请问下 为什么放在JScrollPane中不能正常显示呢? 我该怎么做? 谢谢了
public void invalidate()使容器失效。该容器及其之上的所有父容器被标记为需要重新布置。此方法经常被调用,所以必须快速执行它。
如果在此容器上安装的 LayoutManager 是一个 LayoutManager2 实例,则在该实例上调用 LayoutManager2.invalidateLayout(Container),并提供此 Container 作为参数。
覆盖:
类 Component 中的 invalidate
另请参见:
validate(), layout(), LayoutManager, LayoutManager2.invalidateLayout(Container)