How to close a jframe without closing the main program? <p>i designed java desktop application in that application when i press a button another Jframe is shown that draws a tree but when i close the Jframe whole operation is close but i only want to close that Jfarme what should i do? here is the jframe codes:</p> <pre><code>public class DrawTree extends JFrame{ public int XDIM, YDIM; public Graphics display; @Override public void paint(Graphics g) {} // override method // constructor sets window dimensions public DrawTree(int x, int y) { XDIM = x; YDIM = y; this.setBounds(0,0,XDIM,YDIM); this.setVisible(false); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); display = this.getGraphics(); // draw static background as a black rectangle display.setColor(Color.black); display.fillRect(0,0,x,y); display.setColor(Color.red); try{Thread.sleep(500);} catch(Exception e) {} // Synch with system } // drawingwindow public static int depth(BinaryNode N) // find max depth of tree { if (N==null) return 0; int l = depth(N.left); int r = depth(N.right); if (l&gt;r) return l+1; else return r+1; } // internal vars used by drawtree routines: private int bheight = 50; // branch height private int yoff = 30; // static y-offset // l is level, lb,rb are the bounds (position of left and right child) private void drawnode(BinaryNode N,int l, int lb, int rb) { if (N==null) return; try{Thread.sleep(100);} catch(Exception e) {} // slow down display.setColor(Color.green); display.fillOval(((lb+rb)/2)-10,yoff+(l*bheight),20,20); display.setColor(Color.red); display.drawString(N.element+"",((lb+rb)/2)-5,yoff+15+(l*bheight)); display.setColor(Color.blue); // draw branches if (N.left!=null) { display.drawLine((lb+rb)/2,yoff+10+(l*bheight),((3*lb+rb)/4),yoff+(l*bheight+bheight)); drawnode(N.left,l+1,lb,(lb+rb)/2); } if (N.right!=null) { display.drawLine((lb+rb)/2,yoff+10+(l*bheight),((3*rb+lb)/4),yoff+(l*bheight+bheight)); drawnode(N.right,l+1,(lb+rb)/2,rb); } } // drawnode public void drawtree(BinaryNode T) { if (T==null) return; int d = depth(T); bheight = (YDIM/d); display.setColor(Color.white); display.fillRect(0,0,XDIM,YDIM); // clear background drawnode(T,0,0,XDIM); }} </code></pre> <p>and another question</p> <p>when i new a object from my tree class,i want to access that object in all my button codes so where i should define that or better to say , how i should define that object that can access in all my codes??</p>