Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I always need to resize my window to see my programs

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I always need to resize my window to see my programs

    Whenever I make something in Swing, I always need to resize the window for me to see the graphics on the window. I have been told to use the pack() method on it, which I have, but the problem with it is that it just sets the window to a minimal size, only displaying the x + and -.
    For one of the "games" I made, it at first didn't work until resize, then I added a repaint() after the constructor and it worked for me, but when my friends download it they cannot see it

    Does anyone know why this happens? What can I do to fix this bug?

    EDIT: If you guys want example code, just ask


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,050
    My Mood
    Mellow
    Thanks
    184
    Thanked 650 Times in 637 Posts

    Default Re: I always need to resize my window to see my programs

    Does your paintComponent() method include the statement,

    super.paintComponent( g );

    ?

    If that doesn't solve it, post a simple program that demonstrates what always happens with your Swing code.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I always need to resize my window to see my programs

    Hi! Thanks for your reply
    Unfortunately, that didn't solve it for me
    Here is an example program:
    import javax.swing.*;
    import java.awt.*;
     
    class SwingT extends JComponent {
    	public void setUp() {
    		JFrame f = new JFrame("..");
    		f.setSize(500, 500);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		f.add(this);
    		f.repaint();
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.fillRect(20, 20, 40, 60);
    	}
     
    	public static void main(String[] args) {
    		SwingT t = new SwingT();
    		t.setUp();
    	}
    }

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: I always need to resize my window to see my programs

    Quote Originally Posted by MW130 View Post
    Unfortunately, that didn't solve it for me
    Your code technically works, in the sense that it opens a JFrame with a good/sufficient size and shows the rectangle.
    However it's not a good thing to extend a JComponent and during the setup create a JFrame, all this in that "component". For your example is ok but don't do this in more real scenarios.

    Quote Originally Posted by MW130 View Post
    I have been told to use the pack() method on it
    From javadoc for pack(): "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."
    Note the "preferred size". If you set the preferred size ( setPreferredSize(Dimension) method) on a component, put that component for example in the CENTER area of the content pane (default BorderLayout) and then do a pack() on the frame .... you have the final effect to automatically size the frame to "fit" the component.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,050
    My Mood
    Mellow
    Thanks
    184
    Thanked 650 Times in 637 Posts

    Default Re: I always need to resize my window to see my programs

    I agree with all andbin says, but I also accept that you may have created this odd construction just as a simple example, and it's useful. It shows that the order in which you do things matters. For example, try this instead:
    public void setUp()
    {
    	JFrame f = new JFrame("..");
    	f.setSize(500, 500);
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	f.add(this);
    	f.setVisible(true);
    }
    It also demonstrates that it's useful to understand how Swing's painting mechanism works at a deeper level which you can explore by completing the Custom Painting Tutorial.

Similar Threads

  1. Applet viewer window is diplaying in fron of the current window every time
    By jsreddy99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 24th, 2013, 06:16 AM
  2. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  3. Replies: 1
    Last Post: December 17th, 2011, 02:32 AM
  4. Resize a window without native decorators
    By supertreta in forum AWT / Java Swing
    Replies: 0
    Last Post: January 11th, 2011, 01:06 PM
  5. image scaling on window resize
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 27th, 2010, 03:09 PM