Results 1 to 4 of 4
  1. #1
    patr1c1a is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default my JDialog doesn't want to resize as I say :P

    Hi,
    I'm having trouble with a JDialog as I can't seem to be able to set its size.
    My application has a JFrame and a JDialog, and I've tried resizing the JDialog using the setSize() method and setMinimumSize(Dimension), but it still resizes automatically according to the components it has inside.

    My code goes like this:

    Java Code:
    public class Main {
    	public static void main(String[] args) throws Exception{
    
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.RavenSkin");
    		
    		JFrame frame = new JFrame();
    				
    		JDialog dialog = new JDialog(frame,"title");
    		HistorialDialog historial = new HistorialDialog(h);   [COLOR="SeaGreen"] //a class that extends from JPanel and has all the components that will fill my JDialog[/COLOR]
    		[COLOR="DarkRed"]dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    		dialog.getContentPane().add(historial);
    		dialog.setSize(450,200);
    		
    		Ventana ventana = new Ventana(dialog);[/COLOR]     [COLOR="SeaGreen"]//a class that extends from JPanel and has all the components that will fill my JFrame[/COLOR]
    
    	    [COLOR="DarkRed"]t.addObserver(ventana);  [/COLOR]     [COLOR="SeaGreen"]//I was required to use the Observer pattern on this project[/COLOR]		
    	    [COLOR="DarkRed"]frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(ventana); 
    		frame.setSize(600, 350);
    
    	    Dimension dim = frame.getToolkit().getScreenSize();[/COLOR]      [COLOR="SeaGreen"]//something to center my stuff on the screen...[/COLOR]
    [COLOR="DarkRed"]		Rectangle abounds = frame.getBounds();
    		frame.setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2);
    		dialog.setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2);
    	     
    		 BufferedImage image = null;[/COLOR]     [COLOR="SeaGreen"]//an icon for my JFrame...[/COLOR]
    		 try {
    			 image = ImageIO.read(frame.getClass().getResource("/iconito.png"));
    		 } catch (IOException e) {
    			 e.printStackTrace();
    		 } finally {
    		 	 frame.setIconImage(image);
    		 }
    		 		 
    
    		 dialog.pack();
    		 frame.pack();
    		 frame.setVisible(true);
    		  		 
    	}
    }
    Why would my dialog.setSize(450,200) code do nothing? I'm suspecting it has something to do with the pack() method, but I don't know how to fix it. Any clues?

    Thanks a lot in advance!!

    Moderator Edit: Code tags added
    Last edited by Fubarable; 11-25-2010 at 01:56 PM. Reason: Moderator Edit: Code tags added

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by patr1c1a View Post
    Why would my dialog.setSize(450,200) code do nothing? I'm suspecting it has something to do with the pack() method, but I don't know how to fix it. Any clues?
    You're pack()ing your dialog later so it'll resize itself again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Better to go with setPreferredSize and then call pack().

    Better still to let the components size themselves and call pack().

    Also, calling setLocationRelativeTo(null) after calling pack will center the window.
    Last edited by Fubarable; 11-25-2010 at 01:58 PM.

  4. #4
    patr1c1a is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you so much!! That did it and now it works perfectly ;)

Similar Threads

  1. JDialog
    By Nuclear Mosquito in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2010, 03:27 PM
  2. JDialog
    By frenk_castle in forum AWT / Swing
    Replies: 3
    Last Post: 05-11-2010, 12:28 PM
  3. Close JDialog
    By sky in forum AWT / Swing
    Replies: 7
    Last Post: 11-16-2009, 06:01 PM
  4. setLocation on a JDialog is ignored
    By ScottVal in forum AWT / Swing
    Replies: 7
    Last Post: 01-13-2009, 07:35 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 05:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •