0

I want to implement a code that dipose the JFrame that is open. Searched through multiple questions here in stackoverflow, but I still get an error message for this code

Returns_Show_Data.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

Error: Non-static method setDefaultCloseoperation(int) cannot be referenced from a static context… I also tried:

Returns_Show_Data.dispose();

And it also says the same.

Thanks in advance.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Fran Martinez
  • 677
  • 4
  • 15
  • 36
  • possible duplicate of [calling non-static method in static method in Java](http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java) – Reimeus May 11 '14 at 20:07
  • 1
    What's with the [tag:c#] tag? I've removed it as it is very misleading to anyone using this site in the future. – Hovercraft Full Of Eels May 11 '14 at 20:08

1 Answers1

1

You're making this method call:

Returns_Show_Data.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

on a class, Returns_Show_Data, and this is not allowed, as the error message tells you. Instead you must call it on the valid Returns_Show_Data instance that your application is displaying.

Having said that, it smells as if you're planning on opening and closing various JFrames, and if so, please don't as this can be very annoying to the user to have windows flung at them. Instead, consider swapping views in one main GUI JFrame via a CardLayout.

If you do need to show a temporary window, consider using a JDialog and not a JFrame, and then creating the dialog in such a way that binds it to your JFrame.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for the advice, but I really want to implement in this way and since I have to put the dispose code in multiple jframes I need to fix this error. – Fran Martinez May 11 '14 at 20:16
  • @Marve: `"I have ... multiple jframes"` is a terrible design. If this were my project, I'd start over. Serious. – Hovercraft Full Of Eels May 11 '14 at 21:13
  • If you were in finals, and the professor gave you less than a week to complete the project...you wouldn't start over ;) but thank you anyways. – Fran Martinez May 11 '14 at 21:16