4

We are using the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) method of JFrame.

I want to support the native look and feel, and thus I have to use AWT instead of Swing. So what is the AWT method equivalent to setDefaultCloseOperation?

Am I correct in thinking that in order to support the native look and feel we should use AWT instead of Swing?

4
  • Swing definitely supports native look and feel
    – ChssPly76
    Aug 6, 2009 at 4:36
  • @ ChessPly76:but I think we just create a look and feel which can be fixed for all platforms!
    – Johanna
    Aug 6, 2009 at 4:38
  • If by "fixed" you mean "the same for all platforms", you can do that with Swing - that l&f is called "Metal". If you mean "native for each platform" you can do that as well. I've linked to Sun's tutorial that has a demo in my answer.
    – ChssPly76
    Aug 6, 2009 at 4:51
  • Go with swing. AWT is a nightmare--It was their first cut at an object model and they were able to fix a lot of it in swing. I have to use AWT, but you can still save yourself!
    – Bill K
    Aug 6, 2009 at 8:54

3 Answers 3

6

There isn't a one method equivalent in AWT, but you can build it yourself.

myFrame.addWindowListener(
  new WindowAdapter(){
    public void windowClosed(WindowEvent e) { System.exit(0); }
  }
);

You can get close to native fidelity without using AWT. Instead, set the default Look & Feel using UIManager.

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeel());

You must do this before displaying any UI, or things can get a little hairy.

1

Java provides an interface in awt as well as in swing to handle window events naming window listener We use setDefaultCloseOperation() method just to escape from lengthy coding in swing Window listener overrides 7 methods Naming

  public void windowOpened(WindowEvent e)        {   }
  public void windowClosed(WindowEvent e)         {   }
  public void windowActivated(WindowEvent e)     {   }
  public void windowDeactivated(WindowEvent e) {   }
  public void windowIconified(WindowEvent e)      {   }
  public void windowDeiconified(WindowEvent e)   {   }
 public void windowClosing(WindowEvent e)

{ }

We have to put our exit code in last method as it deals with window closing

Alternatively we can use adapter classes. Using an adapter class is advantageous over a listener that allows us to override only one or two abstract methods we require and not compulsorily all the abstract methods of a listener. But the problem with adapters is they are designed as abstract classes and thereby we cannot extend to our class as the class extends already Frame (Java does not support multiple inheritance).

For example use following code in your program

Frame f = new Frame();
f.addWindowListener(new WindowListener ()
{
    public void windowClosing(WindowEvent we)
    {
        System.exit(0);
    }
});
3
  • you should add some more context and describe why this might solve the problem
    – Rune FS
    Oct 19, 2014 at 12:08
  • That you should provide more context to the answer. Don't just show a solution. Provide information on why and how it solves the problem and explain how to use the snippet otherwise there's great chance that people won't understand what you are saying
    – Rune FS
    Oct 19, 2014 at 16:12
  • way better with the additional context
    – Rune FS
    Oct 19, 2014 at 17:20
0

Alternatively to setting up L'n'F in the code, one could use java/javaw parameter -Dswing.defaultlaf.

For example, under Windows one could specify -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

More information can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.