about using icons

I am trying to display an icon in a window. The code is give below

publicclass LoginHandler{

JFrame mainWindow;

Container contentPane;

JPanel panel;

public LoginHandler()

{

//JFrame.setDefaultLookAndFeelDecorated( true );

createMainWindow();

addComponentsToMainWindow();

}

privatevoid createMainWindow()

{

mainWindow =new JFrame("Login" );

mainWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

mainWindow.setExtendedState( JFrame.MAXIMIZED_BOTH );

mainWindow.setResizable(false );

mainWindow.setVisible(true );

}

privatevoid addComponentsToMainWindow()

{

contentPane = mainWindow.getContentPane();

panel =new JPanel();

panel.add(new JLabel(new ImageIcon("C:\\test\\DMS.gif" ) ) );

}

publicstaticvoid main( String [] args )

{

LoginHandler obj =new LoginHandler();

}

}

The icon to be display and the source file are both located in the same directory, still this code is not working. Please tell me what can be the possible reason.

When I display the the same icon using NetBeans GUI Builder same path is generated (for the icon) by the IDE, and the application works fine. But manual coding does not display any icon.

Thanks

[2250 byte] By [BitsLovera] at [2007-11-27 10:16:54]
# 1

You added the label to the panel and did not add the panel to the contentPane.

If you're going to add components after realization of the JFrame (after pack or

setVisible is called) you will have to validate the contentPane.

private void addComponentsToMainWindow()

{

contentPane = mainWindow.getContentPane();

contentPane.add(new JLabel(new ImageIcon("C:\\test\\DMS.gif")), "Center");

contentPane.validate();

}

Or call setVisible after adding the label to the contentPane.

crwooda at 2007-7-28 15:48:20 > top of Java-index,Desktop,Core GUI APIs...