Bring JPanel to front of other objects in java (SWING) -
i want make loading when app process, used jpanel on jtree. when user clicks on jpanel jtreewill selected , jpanel go back. after hiding jpanel never shows again(i don't know why! seems never go forward of jtree).
i need way bring jpanel forward of anything. how can this?
also must mention don't want jdialog. want use jpanel top of element show loading until process finish.
so here have @ least 2 solutions. either go @geoff , @sthupahsmaht suggesting. btw possible use joptionpane automatically creates dialog you.
the other option use glasspane frame.
or yet option use jlayeredpane @jzd suggests.
edit: example showing how use glasspane capture user selections. try following steps:
1.left clicking on glass pane visible @ start. see output.
2.right click it. hides glass pane.
3.left clicking on content pane. see output.
4.right click it. go point 1. enjoy.
import java.awt.color; import java.awt.dimension; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; public class overpanel extends jpanel { private static void createandshowgui() { final jframe f = new jframe(); f.setpreferredsize(new dimension(400, 300)); f.setdefaultcloseoperation(jframe.exit_on_close); jpanel glasspanel = new jpanel(); glasspanel.setbackground(color.red); glasspanel.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { super.mousepressed(e); system.out.println("f.getglasspane() mousepressed"); if(e.getbutton() == mouseevent.button3) f.getglasspane().setvisible(false); } }); f.setglasspane(glasspanel); f.getcontentpane().setbackground(color.green); f.getcontentpane().addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { super.mousepressed(e); system.out.println("f.getcontentpane() mousepressed"); if(e.getbutton() == mouseevent.button3) f.getglasspane().setvisible(true); } }); f.getglasspane().setvisible(true); f.pack(); f.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { createandshowgui(); } }); } }
edit2: if want have effect of dialog, can achieve incorporating appropriately code example.
jpanel panel = new jpanel(new gridlayout(0, 1)); panel.setborder(borderfactory.createlineborder(color.black, 2)); panel.setbackground(color.yellow); panel.add(new jlabel("i message label")); panel.add(new jbutton("close")); jpanel glasspanel = new jpanel(new gridbaglayout()); glasspanel.setopaque(false); glasspanel.add(panel);
Comments
Post a Comment