如果添加新组件,则滚动窗格不会更新。
我可以不创建新的JScrollPane来更新它吗?
public void start(){
getBox_Topics().setBorder(new TitledBorder(new EtchedBorder(),"Topics of vote"));
add(new JScrollPane(getBox_Topics(),
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
pack();
setHandler(new ClientHandler_Thread(this));
getHandler().start();
setVisible(true);
}
按钮可在Box中添加新组件:
最佳答案
Box / JPanel必须为固定尺寸。
否则它必须具有MaximumSize。
public void start(){
getBox_Topics().setBorder(new TitledBorder(new EtchedBorder(),"Topics of vote"));
setScrollPane(new JScrollPane(getBox_Topics()));
add(getScrollPane());
getScrollPane().setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
getBox_Topics().addContainerListener(new ContainerListener() {
@Override
public void componentAdded(ContainerEvent containerEvent) {
getScrollPane().revalidate();
getScrollPane().repaint();
}
@Override
public void componentRemoved(ContainerEvent containerEvent) {
}
});
pack();
setHandler(new ClientHandler_Thread(this));
getHandler().start();
setVisible(true);
}