Sign in to follow this  
someboddy

[java] JDialog and icons

Recommended Posts

How do I set an icon for JDialog? For my JFrames, I have done it using Frame.setIconImage(Image). JDialogs don't have this method, so I thought they will have the same icon as their owner JFrame. However, when I set the icons for my JFrames, the JDialogs still have the JAVA logo as an icon. What can I do to solve this problem? By the way, the Icons are actualy gif files, but I don't think that's the problem, because they will turn into java.awt.Image objects anyways.

Share this post


Link to post
Share on other sites
a quick casual look thu the docs reveals only a setTitle method. mebe hava look at the platform look nd feel classes nd methods ?

hmmm .. furthr search on sun's developer forums yields this. ifn that's still not write perhaps post your query to sun's swing forum yourself?

[Edited by - ops on June 25, 2005 8:06:51 PM]

Share this post


Link to post
Share on other sites
Well, nothing helps, and I keep getting the JAVA logo icon no metter if my JDialog is resizable or not.
I decided I will just use JFrames. Now, does anyone knows how to hide the maximize&minimize buttons so my frames will look like dialogs?

Share this post


Link to post
Share on other sites
Please post your code for the JDialog issue... it should have worked using a JFrame with custom icon as a parent.

The only method you can call to hide window-top buttons is setUndecorated(false); It will hide the window's title bar.

Son Of Cain

Share this post


Link to post
Share on other sites
Well, my code is pretty long, so I wrote a shorter test code:

import java.awt.*;
import javax.swing.*;

public class JDialogTest extends JFrame
{
private static Image theIcon;
public JDialogTest()
{
super("");
setSize(100,100);
}
public static void main(String[] args)
{
JDialogTest mainFrame=new JDialogTest();
MediaTracker mdtr=new MediaTracker(mainFrame);
Image img=Toolkit.getDefaultToolkit().createImage("testicon.GIF");
mdtr.addImage(img,0);
try
{
mdtr.waitForID(0);
}
catch (InterruptedException e)
{
System.out.println(e);
System.exit(0);
}

mainFrame.setIconImage(img);
mainFrame.setVisible(true);

JDialog dialog=new JDialog(mainFrame,"dialog");
dialog.setLocation(150,0);
dialog.setSize(50,50);
dialog.setVisible(true);
}
}


The dialog still have the coffy cup icon.


By the way, I don't want to hide the border of my JFrames. I just want to hide the minimize and maximize buttons on the title. The title, the close buttons, and the border - I want them visible.

Share this post


Link to post
Share on other sites
Yeah, I understood what you wanted, but that is not possible (I never came across it, anyway). The only Component who allows that is the JInternalFrame, when you declare it not closable, and not resizable.

About the JDialog, I copied and pasted your code (changing only the directory and name of the image, of course) and it worked. Are you using an image larger than 32 x 32 pixels? I'm not sure if that is the limit (or even if it exists), but check it out.

Son Of Cain

Share this post


Link to post
Share on other sites
Well, I resized my image and I still get the coffy cup icon in the dialog. I guess I should try that JInternalFrame of yours.

Share this post


Link to post
Share on other sites
Well, JInternalFrame is not what I need. Since my code works for you, and my JDK is pretty updated, the problem is probebly on my JVM.

Share this post


Link to post
Share on other sites
Edit:

This works for me (Java 1.5)


public class Test{
public Test(){
JFrame testFrame = new JFrame();
Image icon = Toolkit.getDefaultToolkit().createImage("test.gif");

testFrame.setSize(200, 200);
testFrame.setIconImage(icon);
testFrame.setVisible(true);

JDialog dialog = new JDialog(testFrame, "Hi");
dialog.setSize(100, 100);
dialog.setVisible(true);
}

public static void main(String[] args){
new Test();
}
}

Share this post


Link to post
Share on other sites
Yeah, I forgot to mention that JInternalFrames are to be used in a JDesktopPane. I didn't know if it would be of use for you, but that's the only component that hides (instead of disabling) the window-top icons.

Try this out, dude:


import javax.swing.JFrame;
import javax.swing.JDialog.

public class JDTest {

public static final void main(String args[]) {

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setIconImage(new ImageIcon("res/img/icon.gif").getImage());

JDialog diag = new JDialog(frame, false);
diag.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

frame.setVisible(true);
diag.setVisible(true);

}

}




Hope it works ;)

Son Of Cain

Edit: I'm also using Java 1.5

Share this post


Link to post
Share on other sites
Still don't work for me. Maby there is a way to just not show the icon? If there isn't, I will just publish the game as is, and see if other people can see the proper icons.

Share this post


Link to post
Share on other sites
While we're at the subject of internal windows, when I override it's paint() method and draw a rectangle from (0,0) then the title bar gets painted over. So how can I know what the OS dependent height of the title bar is (I'm using getSystemLookAndFeel())?

Share this post


Link to post
Share on other sites
Well I wrote this program which uses insets, but still it paints over the title bar. Can you see what am I doing wrong?

Main frame:
/*
* Created on Jun 29, 2005
*/

import java.awt.*;
import javax.swing.*;

public class MainFrame extends JFrame
{
private final JDesktopPane gc = new JDesktopPane();
private final MainFrame self = this;

public MainFrame()
{
super("Internal frame test");
setContentPane(gc);
setSize(new Dimension(600,500));

InternalFrame intern = new InternalFrame("The internal frame");
gc.add(intern);
intern.setLocation(50,50);
intern.show();

setVisible(true);
}

public static void main(String[] args)
{
MainFrame m = new MainFrame();
}
}

Internal frame:
/*
* Created on Jun 29, 2005
*/

import java.awt.*;
import javax.swing.*;

public class InternalFrame extends JInternalFrame
{
public InternalFrame(String title)
{
super(title,true,true,true,false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(new Dimension(200,150));
}

public void paint(Graphics g)
{
super.paint(g);

Insets insets = getInsets();

g.setColor(Color.BLUE);
// will draw blue rect over the title bar
g.fillRect(insets.left + 10,insets.top + 10, 50, 50);
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this