Java GUI Framework Question:
Download Questions PDF

How to replace the icon in the title bar (window decoration) of a [J]Dialog?

Java GUI Framework Interview Question
Java GUI Framework Interview Question

Answer:

There is only a partial solution to this problem, and it is not recommended.

A dialog gets its icon from its parent frame. You can create a dummy frame, set the icon of that dummy frame, and use it in the constructor of the dialog as the dialog's owner:

JFrame dummy = new JFrame();
Image icon = ...
dummyFrame.setIconImage(icon);
JDialog dialog = new JDialog(dummy);

However, this is dangerous. Certain GUI behavior depends on a correct [J]Frame (parent window) <-> [J]Dialog (child window) relation. Introducing a dummy parent breaks this relation. Things which can go wrong include (de)iconising of all windows of an application, and ensuring a modal dialog is always placed on-top of the main window.

Download Java GUI Framework Interview Questions And Answers PDF

Previous QuestionNext Question
How to replace/remove the icon in the title bar (window decoration) of a [J]Frame?How to get the screen size? How Do I center a window on the screen?