Blinking when swapping JPanel content

北战南征 提交于 2019-12-11 02:54:54

问题


I'm working on a project using Java3D and Jmol (it's a viewer for chemical structures in 3D). I have to create view that is able to switch between Java3D and Jmol representation of structures.

I have managed to do that, but when I swap JPanels with JmolPanel and Canvas3D in them, I get blinking of the swapped area.

I'm swapping panels simply by doing:

public static void changeView(JPanel c) {
     c.removeAll();
    if (var) {
        c.add(canvas);
    } else {
        c.add(jmolPanel);
    }
    c.revalidate();
    var = !var;
}

An example of code that creates frame with button for swapping panels can be found here: http://pastebin.com/3F2gKBgb

To run this example you need Jmol.jar (it can be found here http://jmol.sourceforge.net/download/) and Java3D (http://java3d.java.net/binary-builds.html)

I tried setting double buffering in JPanels, but it doesn't help. Do you have any idea how the blinking problem could be resolved?


回答1:


If by blinking you mean it's getting repainted while you are removing and then adding sub-panel then I would try to stop repainting, do all the swapping and then enable repainting of the parent panel.

May be the c.setVisible(false); <your swapping code>; c.setVisible(true); will help?

or create a subpanel sc, that you will make invisible to which you will add your swappable components, so that the parent panel stays visible at all times. It may be a hack around double-buffering problem that should have solved your problem to begin with, but it's worth a try.



来源:https://stackoverflow.com/questions/13630978/blinking-when-swapping-jpanel-content

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