JFrame initially appears with a maximum width limited to my monitor width











up vote
1
down vote

favorite












I'm experiencing a strange problem with the java JFrame class in one of my programs. Thankfully I'm able to duplicate this issue using the FrameDemo.java example taken from Oracle's website:



https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java



I change the dimension of emptyLabel to (3005, 100) and then compile and launch the program. On my dual monitor display, I expect the frame to almost span both monitors (they're each 1920 pixels wide).



Instead, what I'm seeing is that the JFrame is sized to fit the width of a single monitor. I can't seems to get the window to come up spanning both monitors. Am I doing something wrong here? I'm running on Centos 7.5 and am using jdk 1.8.0_60.



The exact code appears below. It's mostly a copy/paste from Oracle's demo, with some comments cut to keep it short.



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

/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel emptyLabel = new JLabel("blahblah");
emptyLabel.setPreferredSize(new Dimension(3005, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}









share|improve this question
























  • This "may" be a limitation of the OS
    – MadProgrammer
    Nov 8 at 20:57










  • I'm starting to think this might be the case. I have learned that a JFrame is associated with a GraphicsConfiguration (gc). As all my gc devices are 1920x1080 in size, I'm starting to think that the JFrame may be limited to this size.
    – user2643390
    Nov 8 at 21:13










  • Interestingly, if I call frame.setMinimumSize(new Dimension 3500,100)); just prior to the pack statement, the window comes up spanning both monitors. I don't want to do this in a real program though.
    – user2643390
    Nov 8 at 21:14






  • 1




    Yes and no. You can have window larger than the physical screen, although on MacOS you'll be disappointed by the results. After a "lot" of digging, it appears that the call to setVisible is actually the culprit. pack will honour the preferred size of the contents, but for some reason after calling setVisible, the window is constrained to the size of the monitor and I can't find exactly "where" this is getting done. "A" somewhat "nasty" solution would to call pack (again) after calling setVisible 🤮
    – MadProgrammer
    Nov 8 at 21:16










  • That's not nasty - it's a beautiful workaround for a nasty problem! How did you figure that out?
    – user2643390
    Nov 8 at 21:19















up vote
1
down vote

favorite












I'm experiencing a strange problem with the java JFrame class in one of my programs. Thankfully I'm able to duplicate this issue using the FrameDemo.java example taken from Oracle's website:



https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java



I change the dimension of emptyLabel to (3005, 100) and then compile and launch the program. On my dual monitor display, I expect the frame to almost span both monitors (they're each 1920 pixels wide).



Instead, what I'm seeing is that the JFrame is sized to fit the width of a single monitor. I can't seems to get the window to come up spanning both monitors. Am I doing something wrong here? I'm running on Centos 7.5 and am using jdk 1.8.0_60.



The exact code appears below. It's mostly a copy/paste from Oracle's demo, with some comments cut to keep it short.



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

/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel emptyLabel = new JLabel("blahblah");
emptyLabel.setPreferredSize(new Dimension(3005, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}









share|improve this question
























  • This "may" be a limitation of the OS
    – MadProgrammer
    Nov 8 at 20:57










  • I'm starting to think this might be the case. I have learned that a JFrame is associated with a GraphicsConfiguration (gc). As all my gc devices are 1920x1080 in size, I'm starting to think that the JFrame may be limited to this size.
    – user2643390
    Nov 8 at 21:13










  • Interestingly, if I call frame.setMinimumSize(new Dimension 3500,100)); just prior to the pack statement, the window comes up spanning both monitors. I don't want to do this in a real program though.
    – user2643390
    Nov 8 at 21:14






  • 1




    Yes and no. You can have window larger than the physical screen, although on MacOS you'll be disappointed by the results. After a "lot" of digging, it appears that the call to setVisible is actually the culprit. pack will honour the preferred size of the contents, but for some reason after calling setVisible, the window is constrained to the size of the monitor and I can't find exactly "where" this is getting done. "A" somewhat "nasty" solution would to call pack (again) after calling setVisible 🤮
    – MadProgrammer
    Nov 8 at 21:16










  • That's not nasty - it's a beautiful workaround for a nasty problem! How did you figure that out?
    – user2643390
    Nov 8 at 21:19













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm experiencing a strange problem with the java JFrame class in one of my programs. Thankfully I'm able to duplicate this issue using the FrameDemo.java example taken from Oracle's website:



https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java



I change the dimension of emptyLabel to (3005, 100) and then compile and launch the program. On my dual monitor display, I expect the frame to almost span both monitors (they're each 1920 pixels wide).



Instead, what I'm seeing is that the JFrame is sized to fit the width of a single monitor. I can't seems to get the window to come up spanning both monitors. Am I doing something wrong here? I'm running on Centos 7.5 and am using jdk 1.8.0_60.



The exact code appears below. It's mostly a copy/paste from Oracle's demo, with some comments cut to keep it short.



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

/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel emptyLabel = new JLabel("blahblah");
emptyLabel.setPreferredSize(new Dimension(3005, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}









share|improve this question















I'm experiencing a strange problem with the java JFrame class in one of my programs. Thankfully I'm able to duplicate this issue using the FrameDemo.java example taken from Oracle's website:



https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java



I change the dimension of emptyLabel to (3005, 100) and then compile and launch the program. On my dual monitor display, I expect the frame to almost span both monitors (they're each 1920 pixels wide).



Instead, what I'm seeing is that the JFrame is sized to fit the width of a single monitor. I can't seems to get the window to come up spanning both monitors. Am I doing something wrong here? I'm running on Centos 7.5 and am using jdk 1.8.0_60.



The exact code appears below. It's mostly a copy/paste from Oracle's demo, with some comments cut to keep it short.



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

/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel emptyLabel = new JLabel("blahblah");
emptyLabel.setPreferredSize(new Dimension(3005, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}






java swing jframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 7:36







user10614001

















asked Nov 8 at 20:50









user2643390

63




63












  • This "may" be a limitation of the OS
    – MadProgrammer
    Nov 8 at 20:57










  • I'm starting to think this might be the case. I have learned that a JFrame is associated with a GraphicsConfiguration (gc). As all my gc devices are 1920x1080 in size, I'm starting to think that the JFrame may be limited to this size.
    – user2643390
    Nov 8 at 21:13










  • Interestingly, if I call frame.setMinimumSize(new Dimension 3500,100)); just prior to the pack statement, the window comes up spanning both monitors. I don't want to do this in a real program though.
    – user2643390
    Nov 8 at 21:14






  • 1




    Yes and no. You can have window larger than the physical screen, although on MacOS you'll be disappointed by the results. After a "lot" of digging, it appears that the call to setVisible is actually the culprit. pack will honour the preferred size of the contents, but for some reason after calling setVisible, the window is constrained to the size of the monitor and I can't find exactly "where" this is getting done. "A" somewhat "nasty" solution would to call pack (again) after calling setVisible 🤮
    – MadProgrammer
    Nov 8 at 21:16










  • That's not nasty - it's a beautiful workaround for a nasty problem! How did you figure that out?
    – user2643390
    Nov 8 at 21:19


















  • This "may" be a limitation of the OS
    – MadProgrammer
    Nov 8 at 20:57










  • I'm starting to think this might be the case. I have learned that a JFrame is associated with a GraphicsConfiguration (gc). As all my gc devices are 1920x1080 in size, I'm starting to think that the JFrame may be limited to this size.
    – user2643390
    Nov 8 at 21:13










  • Interestingly, if I call frame.setMinimumSize(new Dimension 3500,100)); just prior to the pack statement, the window comes up spanning both monitors. I don't want to do this in a real program though.
    – user2643390
    Nov 8 at 21:14






  • 1




    Yes and no. You can have window larger than the physical screen, although on MacOS you'll be disappointed by the results. After a "lot" of digging, it appears that the call to setVisible is actually the culprit. pack will honour the preferred size of the contents, but for some reason after calling setVisible, the window is constrained to the size of the monitor and I can't find exactly "where" this is getting done. "A" somewhat "nasty" solution would to call pack (again) after calling setVisible 🤮
    – MadProgrammer
    Nov 8 at 21:16










  • That's not nasty - it's a beautiful workaround for a nasty problem! How did you figure that out?
    – user2643390
    Nov 8 at 21:19
















This "may" be a limitation of the OS
– MadProgrammer
Nov 8 at 20:57




This "may" be a limitation of the OS
– MadProgrammer
Nov 8 at 20:57












I'm starting to think this might be the case. I have learned that a JFrame is associated with a GraphicsConfiguration (gc). As all my gc devices are 1920x1080 in size, I'm starting to think that the JFrame may be limited to this size.
– user2643390
Nov 8 at 21:13




I'm starting to think this might be the case. I have learned that a JFrame is associated with a GraphicsConfiguration (gc). As all my gc devices are 1920x1080 in size, I'm starting to think that the JFrame may be limited to this size.
– user2643390
Nov 8 at 21:13












Interestingly, if I call frame.setMinimumSize(new Dimension 3500,100)); just prior to the pack statement, the window comes up spanning both monitors. I don't want to do this in a real program though.
– user2643390
Nov 8 at 21:14




Interestingly, if I call frame.setMinimumSize(new Dimension 3500,100)); just prior to the pack statement, the window comes up spanning both monitors. I don't want to do this in a real program though.
– user2643390
Nov 8 at 21:14




1




1




Yes and no. You can have window larger than the physical screen, although on MacOS you'll be disappointed by the results. After a "lot" of digging, it appears that the call to setVisible is actually the culprit. pack will honour the preferred size of the contents, but for some reason after calling setVisible, the window is constrained to the size of the monitor and I can't find exactly "where" this is getting done. "A" somewhat "nasty" solution would to call pack (again) after calling setVisible 🤮
– MadProgrammer
Nov 8 at 21:16




Yes and no. You can have window larger than the physical screen, although on MacOS you'll be disappointed by the results. After a "lot" of digging, it appears that the call to setVisible is actually the culprit. pack will honour the preferred size of the contents, but for some reason after calling setVisible, the window is constrained to the size of the monitor and I can't find exactly "where" this is getting done. "A" somewhat "nasty" solution would to call pack (again) after calling setVisible 🤮
– MadProgrammer
Nov 8 at 21:16












That's not nasty - it's a beautiful workaround for a nasty problem! How did you figure that out?
– user2643390
Nov 8 at 21:19




That's not nasty - it's a beautiful workaround for a nasty problem! How did you figure that out?
– user2643390
Nov 8 at 21:19












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Many thanks to MadProgrammer. Unfortunately, in my more complicated program, the second call to pack didn't seem to do the trick.



The workaround I eventually settled on was to compute the cumulative width of all my components (they were loaded into a JSplitPane). This is equal to the widths of the left pane, the right pane and the divider. If this cumulative width exceeds the width of a monitor, I set frame minimum size to this cumulative width. This seems to ensure that the frame can span two monitors when it appears. Then after making things visible, I reset the minimum size to a value I saved at startup time. This allows my JFrame to come up on two screens but still be resizable to something smaller.



This truly is a nasty workaround. I have reported this as a bug to http://bugs.java.com. It's listed as the following bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8213620






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215927%2fjframe-initially-appears-with-a-maximum-width-limited-to-my-monitor-width%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Many thanks to MadProgrammer. Unfortunately, in my more complicated program, the second call to pack didn't seem to do the trick.



    The workaround I eventually settled on was to compute the cumulative width of all my components (they were loaded into a JSplitPane). This is equal to the widths of the left pane, the right pane and the divider. If this cumulative width exceeds the width of a monitor, I set frame minimum size to this cumulative width. This seems to ensure that the frame can span two monitors when it appears. Then after making things visible, I reset the minimum size to a value I saved at startup time. This allows my JFrame to come up on two screens but still be resizable to something smaller.



    This truly is a nasty workaround. I have reported this as a bug to http://bugs.java.com. It's listed as the following bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8213620






    share|improve this answer



























      up vote
      0
      down vote













      Many thanks to MadProgrammer. Unfortunately, in my more complicated program, the second call to pack didn't seem to do the trick.



      The workaround I eventually settled on was to compute the cumulative width of all my components (they were loaded into a JSplitPane). This is equal to the widths of the left pane, the right pane and the divider. If this cumulative width exceeds the width of a monitor, I set frame minimum size to this cumulative width. This seems to ensure that the frame can span two monitors when it appears. Then after making things visible, I reset the minimum size to a value I saved at startup time. This allows my JFrame to come up on two screens but still be resizable to something smaller.



      This truly is a nasty workaround. I have reported this as a bug to http://bugs.java.com. It's listed as the following bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8213620






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Many thanks to MadProgrammer. Unfortunately, in my more complicated program, the second call to pack didn't seem to do the trick.



        The workaround I eventually settled on was to compute the cumulative width of all my components (they were loaded into a JSplitPane). This is equal to the widths of the left pane, the right pane and the divider. If this cumulative width exceeds the width of a monitor, I set frame minimum size to this cumulative width. This seems to ensure that the frame can span two monitors when it appears. Then after making things visible, I reset the minimum size to a value I saved at startup time. This allows my JFrame to come up on two screens but still be resizable to something smaller.



        This truly is a nasty workaround. I have reported this as a bug to http://bugs.java.com. It's listed as the following bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8213620






        share|improve this answer














        Many thanks to MadProgrammer. Unfortunately, in my more complicated program, the second call to pack didn't seem to do the trick.



        The workaround I eventually settled on was to compute the cumulative width of all my components (they were loaded into a JSplitPane). This is equal to the widths of the left pane, the right pane and the divider. If this cumulative width exceeds the width of a monitor, I set frame minimum size to this cumulative width. This seems to ensure that the frame can span two monitors when it appears. Then after making things visible, I reset the minimum size to a value I saved at startup time. This allows my JFrame to come up on two screens but still be resizable to something smaller.



        This truly is a nasty workaround. I have reported this as a bug to http://bugs.java.com. It's listed as the following bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8213620







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 14:43

























        answered Nov 8 at 22:26









        user2643390

        63




        63






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215927%2fjframe-initially-appears-with-a-maximum-width-limited-to-my-monitor-width%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)