0

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:

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>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);
}}

and another question

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??

Mohammad Olfatmiri
  • 1,605
  • 5
  • 31
  • 57
  • can you show us the code for displaying the JFrame in the first place? – twain249 Apr 13 '12 at 19:05
  • 1) *"another Jframe is shown"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) Please find your shift key and apply it at the start of every sentence, for the word I and class names like JFrame. That mess is painful to try & read. 3) That code seemed to have nothing to do with the problem. 4) *"and another question"* That is a topic for ..another question. Please don't try to jam 2 questions into one. – Andrew Thompson Apr 13 '12 at 19:14

2 Answers2

2

You should set default close operation to HIDE_ON_CLOSE

  this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

As advised: Take note, that with DISPOSE_ON_CLOSE, you can still keep the program running if that is not the last JFrame that is open: (Taken from the javadocs) Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information.

Youssef G.
  • 617
  • 4
  • 10
  • You should edit that comment into your answer then delete the comment. I would advise against multiple frames (chase the link in my comment), but +1 for a technically correct answer. – Andrew Thompson Apr 13 '12 at 19:17
  • ^I do agree with you there, but I just wanted to list that as an alternative since a previous answer-er had posted DISPOSE_ON_CLOSE. – Youssef G. Apr 13 '12 at 19:19
  • *"previous answer"* Yes, I saw it. It was fortunate that it was deleted by the owner. Thanks for clearing that up. :) – Andrew Thompson Apr 13 '12 at 19:26
0

You can simply hide the JFrame.

this.hide();

Edit: For the above: Youssef G's answer is better.

For the 2nd part of the question. Create your tree class and pass the object around in your program so it is the same tree object. Don't create a new one.

For example:

Class A {
     B b;    //B object inside class A
     Tree t;  //Tree object inside class A
}
Class B {
     Tree t;    //Tree object inside class B
}

Now you have a tree object in both classes. You can create a new tree which is class b's tree while code is running in Class A. Then say this.t = b.t;

Hope this helps.

C0D3
  • 6,440
  • 8
  • 43
  • 67
  • I wrote this Chess program in Java and in it, I create a board object inside the main application view and also in AIEngine class. You can do similar thing for you tree object. Here is my code: http://www.kanersan.com/myfiles/ChessSource.zip – C0D3 Apr 13 '12 at 19:43
  • You can also see how I'm dealing with some Views/Frames in the application too. – C0D3 Apr 13 '12 at 19:43