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 4 of 4

Thread: Swing - windowStateChanged event updates window size after it completes

  1. #1
    Junior Member Venom's Avatar
    Join Date
    Feb 2014
    Location
    Croatia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Swing - windowStateChanged event updates window size after it completes

    I have a JFrame where some elements (one, for now) have to be centered manually on the contentPane when resizing the window or changing the window state. Resizing event (componentResized) works fine but the windowStateChanged event is causing problems because it doesn't seem to "update" the contentPane's new size properly - it keeps the previous value although the window's size is obviously changed. This can be seen by simply printing the result of getSize called on contentPane.

    Centering is done by programmatically changing the constraint of the component (using putConstraint, SpringLayout). The issue is that getWidth used in that method returns "wrong" values which results in an uncentered component. Maybe another listener is needed here?

    Additional info: Eclipse with WindowBuilder, Linux Mint 15, Java 1.7

    Any kind of advice is appreciated although changing the layout manager isn't what I'm looking for here. I've asked the same question on Stack Overflow and that is what someone proposed as a solution.

    I know about SSCCE guidelines but I cannot make this particular example both ready to compile and short.

    For unknown reasons, I am unable to post the code example. I keep getting this - "Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words."
    Extremely annoying.

    EDIT: Managed to do it somehow, weird.

    --- Update ---

    addWindowStateListener(new WindowStateListener()
    {
        // no "@Override" was generated but it is the same with it
        public void windowStateChanged(WindowEvent e)
        {
            System.out.println("EVENT: " + contentPane.getSize() + ", "
                    + getExtendedState() + ", " + e.getOldState()); // amusingly, states are actually correct - interchanging between 0 (Frame.NORMAL) and 6 (Frame.MAXIMIZED_BOTH) when I maximize and "unmaximize"
            tfArrayPanelCenter();
        }
    });
     
    public void tfArrayPanelCenter()
    {
        int padding = (contentPane.getWidth()
                - tfArrayPanel.getPreferredSize().width - HGAP) / 2;
        sl_contentPane.putConstraint(SpringLayout.WEST, tfArrayPanel, padding,
                SpringLayout.WEST, contentPane);
    }


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

    Default Re: Swing - windowStateChanged event updates window size after it completes

    Quote Originally Posted by Venom View Post
    Centering is done by programmatically changing the constraint of the component (using putConstraint, SpringLayout). The issue is that getWidth used in that method returns "wrong" values which results in an uncentered component. Maybe another listener is needed here?
    Just one question .... why are you complicating your life with complex/unusual programmatic resizes? And why are you using a very complex layout manager like SpringLayout? There are other better solutions.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member Venom's Avatar
    Join Date
    Feb 2014
    Location
    Croatia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Swing - windowStateChanged event updates window size after it completes

    Actually, I'm not resizing the component but simply centering it on the contentPane.

    But I believe this has very little if anything to do with my choice of layout manager.
    The culprit is most probably the event handler that doesn't properly update the size of the window - instead it keeps the old value (previous state of the window).

    What I don't know is if that is the expected behavior or is it some kind of a design flaw.
    On a second thought, Java could be having some problems with KWin - who knows?

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

    Default Re: Swing - windowStateChanged event updates window size after it completes

    Quote Originally Posted by Venom View Post
    Actually, I'm not resizing the component but simply centering it on the contentPane.
    In general I had to say "programmatic relayout". But the concept doesn't change and I repeat: there are better solutions.

    If you want to have a component X (can be e.g. a JButton, JLabel or a JPanel with complex layout, this doesn't matter) that always stays centered on the content pane and is always sized at its "preferred" size, you can use for example GridBagLayout. Other solutions can use a mix of other simple layout managers.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Jtable only updates at the end of a buton event.
    By covi454 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 2nd, 2013, 11:05 AM
  2. 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, 07:16 AM
  3. how to open a popup window of specific size in asp
    By btebo in forum Other Programming Languages
    Replies: 5
    Last Post: May 26th, 2012, 06:49 PM
  4. SWING GUI CONSOLE WINDOW DISPLAY
    By Khadafi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 10th, 2012, 09:15 AM
  5. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM

Tags for this Thread