Results 1 to 12 of 12
  1. #1
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Question How to make a resizable JFrame using a drop down list with different resolutons?

    I want to make a window where in the options menu I will have a drop down menu with different resolutions to use. Also I would like fullscreen and windowed mode to be options in a different drop down menu

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    So you can set the size of a window at runtime by changing the preferred size of the top level component and then calling pack() on the container. What have you tried?

  3. #3
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    top level component being the JFrame? I dont have any other components other than a canvas

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    No, top level being the component you put in the jframe. You generally don't do much of anything to a jframe itself, rather, you put gui components in panels which reside in the JFrame. The top level JPanel would be the one you set the size on, and then you pack the JFrame.

  5. #5
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    In my main method I am using:

    Game game = new Game();
    game.frame.add(game);
    game.frame.pack();

    the Game constructor is:

    public Game() {
    setPreferredSize(size);
    frame = new JFrame();
    }

    the Game class extends Canvas implements Runnable

    so I'm not using a panel or anything

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    That might work because Canvas extends Component - but canvas is a leftover from the AWT days, and I usually try to avoid using non-swing stuff if possible. The technique I described might still work, but it would also be trivial to make your game extend JPanel instead of Canvas.

  7. #7
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    Alright I got it to work. What I need now is to make it so it is fullscreen and borderless, no matter what the resolution is (so it will scale up to the monitor size)

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    Thats not too hard to do - you do it through a series of calls which get the current graphics device and then set it's display mode. I use something like this boiler plate code:

    Java Code:
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    GraphicsDevice gd = gs[0];
    gd.setFullScreenWindow(this);
    The last line assumes the current class extends JFrame, but if your jFrame is separate, then just reference it there instead of 'this'. You can leave fullscreen by doing:

    Java Code:
    gd.setFullScreenWindow(null);

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    FYI, that code only makes the app occupy the entire screen, it does not change the resolution of the display. You probably don't want to do that anyway. Look at the API for GraphicsDevice for ways to find the current screen resolution so that you can use it in your environment!

  10. #10
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    So to change the resolution of the fullscreen window, I actually have to change the resolution of the monitor?

  11. #11
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    Alright I set it so it gets all the available DisplayModes for the display, and put them in a combo box using this:
    Java Code:
    public Dimension defaultSize = getDefaultScreenSize();
    	
    public static DisplayMode[] DisplayModes = device.getDisplayModes();
    Java Code:
    for (int i = 0; i < DisplayModes.length; i++) {
    			if(DisplayModes[i].getWidth() <= defaultSize.width && DisplayModes[i].getHeight() <= defaultSize.height) {
    				cBox.addItem(DisplayModes[i]);
    			}
    		}
    For one, it doesn't include my monitors native resolution (the list starts at 1440x900 - my monitors native is 1600x900) even though i used:
    Java Code:
    if(DisplayModes[i].getWidth() <= defaultSize.width && DisplayModes[i].getHeight() <= defaultSize.height)
    Also if its in certain resolutions it wont scale up to fit the monitor. 800x600 does, but 1280x720 wont.

    Another thing, how do I change the text of the combo box without changing what that selection does? The DisplayModes aren't readable.

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to make a resizable JFrame using a drop down list with different resolutons?

    So to change the resolution of the fullscreen window, I actually have to change the resolution of the monitor?
    Depends on what you are trying to do. If you do not change the display resolution and your screen area is set to something small like 640x480, then the display window will just be a small viewport in the center of the screen with lots of black margin around it.

    If your display resolution is the same size as the screen resolution, then making to go fullscreen will just display the viewport fullscreen, no borders.

    If you want a small viewport like 640x480 to fill the entire screen on a large display, then the resolution of the display MUST be changed. You can do this programmatically by inspecting the list of supported resolutions on the GraphicsDevice and picking one that fits your needs. In order for your app to work fullscreen on many different monitors and setups, you'll need to support several common resolutions, usually in two or three different aspect ratios - 4:3 (traditional non-widescreen monitors and TVs), 16:9 (modern widescreen TVs and some monitors), 16:10 (most modern widescreen monitors).

Similar Threads

  1. drop down list
    By nour in forum Android
    Replies: 2
    Last Post: 11-20-2011, 10:25 PM
  2. what code for searching in drop down list?
    By Harmesh Goyal in forum Advanced Java
    Replies: 1
    Last Post: 03-03-2011, 05:43 AM
  3. Populating a drop down list from a database
    By matpj in forum New To Java
    Replies: 0
    Last Post: 01-19-2009, 01:14 PM
  4. JComboBox and drop-down list.....HELP!!!!!
    By Anna in forum AWT / Swing
    Replies: 1
    Last Post: 06-18-2008, 06:16 AM

Tags for this Thread

Posting Permissions

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