12

Now what I am doing in my program is that I am using setundecorated = true and MAXIMIZED_BOTH So it makes it go full screen and the display looks very nice, But the problem is that there are images (border) on the left and the right side of my screen and also a blue background. What happens is that in changing screens and resolutions these get disturbed and are not shown properly. Those grey patches come up again

enter image description here

History:

I have a java program which I wanted to always open in full screen; I was not able to find a way to do it properly so I had adjusted the minimum to (1370, 727) and maximum size. Thus, it started opening properly on my laptop, but when I changed my laptop's display to LCD, it started giving problems:

It opens in a smaller window: Login Window

If I then click on the maximize button, a grey area comes on the side and bottom (I wanted the items on screen to get stretched or center themselves): enter image description here

And here for example, there is a grey patch at the bottom. Instead, I want the background to cover the whole screen. enter image description here

Update 1 If I change to stretchable gridbaglayout, this is the code I used and what happens:

Menu.setExtendedState(MAXIMIZED_BOTH);
GridBagLayout gbl = new GridBagLayout();
Menu.setLayout(gbl);
JButton component = new JButton("1");
gbl.layoutContainer(Menu);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbl.setConstraints(component, gbc);
Menu.add(component);
Menu.pack();
Menu.setVisible(true);

enter image description here

Question

  1. How do I set "this" frame to setExtendedState(MAXIMIZED_BOTH) as I have done to others? (if I do this in main function, I get an error; even if I make a function for this and call it in main I get an error)

  2. How do I get everything to stretch/rearrange themselves according to the extra grey space?

Update 2 My files in this project:

enter image description here

Update 3 This is the current file I am working on "FormTTS.java" Search for "MAXIMIZED_BOTH" in there and you will find the code I think you will want to check.

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • I know this isn't an answer, and therefore I put it as a comment. But have you considered using JavaFX instead? It is the new GUI kid on the block and its both easy to develop in and pure sugar for the eyes to look at. Making an app in JavaFX go full-screen is a [kids play](http://docs.oracle.com/javase/8/javafx/api/javafx/stage/Stage.html). Oracle have long said that they dropped the development of Swing and goes all in with JavaFX instead. You should definitely consider a switch. – Martin Andersson Mar 30 '14 at 07:11
  • @MartinAndersson Could you please elaborate it a bit more for me, what do you mean? Do i have to make the whole program again? Sorry i did not get you completely maybe cauz i am a newbie sorry :P – Daksh Shah Mar 30 '14 at 07:17
  • I don't want to put this as an answer, because I'm not sure if it'll help your situation, but calling `frame.setUndecorated(true)` will remove the top frame border (where title goes) and side border, filling the rest of the space up with whatever is inside of it (if your panel is meant to fill the screen) – Vince Mar 30 '14 at 07:23
  • @VinceEmigh I will just try putting that once. – Daksh Shah Mar 30 '14 at 07:26
  • In short, Oracle stopped updating Swing like ten years ago. If you have ever used a program like Eclipse or NetBeans, you know how ugly it looks and how slow it can be. AWT and Swing has nothing compared with other GUI frameworks like WPF (Windows Presentation Foundation). So Oracle figured they had to do something drastically to make Java popular besides application servers and console programs. JavaFX was once a scripting language for GUI stuff. Oracle bought it and since some update in the Java 1.7 series, they have fully incorporated JavaFX in the JDK. Meaning (to be continued).. – Martin Andersson Mar 30 '14 at 08:44
  • ..that you need no plugins, dependencies or a new language to use JavaFX and write applications using the framework. A JavaFX application is hardware accelerated when the running platform has such support. You'll also get touch capabilities and all kinds of cool stuff. Best of all, the programming model is awesome. You have tons of great classes and features to use from the JDK (including the ability to go full-screen!) and you may chose how you like to write GUI programs. You may do so fully programmatically, or be more declarative using FXML files (to be compared with XAML in WPF). – Martin Andersson Mar 30 '14 at 08:48
  • Sorry, never answered your question but basically, yes. You would have to rewrite a hole lot of stuff. You **can** use Swing components in your JavaFX application, but for what cause? – Martin Andersson Mar 30 '14 at 08:52
  • @MartinAndersson Thankq I will see more about Javafx later, cauz the problem is that i want to complete the project ASAP so i cannot make it again right now :P – Daksh Shah Mar 30 '14 at 09:00
  • @MartinAndersson Eclipse does not use AWT/Swing, instead it uses SWT. That is the reason why it looks different than Netbeans. SWT uses native parts and should integrate seamlessly into your OS-specific look (e.g. Eclipse IDE look&feel is not distinguishable from native application on Windows 7). Furthermore the slow part is a problem of the size of the program not of the graphical framework, a "JavaFX Eclipse/Netbeans" would be equally slow. – Absurd-Mind Apr 02 '14 at 10:34
  • @Absurd-Mind So what are you suggesting? Change to eclipse? Now? After completing almost all of the project? will i need to make it again :( ? – Daksh Shah Apr 02 '14 at 10:39
  • @DakshShah i'm not suggesting to change to eclipse. I wanted to clarify some statements that were made. I would suggest considering to change to JavaFX/SWT/Eclipse RCP, in the *long* run. If you implemented your classes using MVC you would "only" have to implement a new GUI. If you are using Swing JavaFX would be the easier choice since the idea behind that is kinda similar to Swing. – Absurd-Mind Apr 02 '14 at 10:46
  • Please correct me if I am wrong, but Eclipse is not hardware accelerated and cannot be. JavaFX with the new Prism engine is hardware accelerated on platforms that support such acceleration. Also, Eclipse might use native calls, but the components he build is from the earlier century anyways. The visuals of Eclipse has nothing to compare with the visuals of a modern .NET or JavaFX application. – Martin Andersson Apr 02 '14 at 15:46
  • 1
    I used to use Swing to develop GUIs (nothing professional, just for myself). The latest thing I'm doing, I realized how much of a pain it was going to be and started to use JavaFX, but then realized, why use that when I can just use CSS and Javascript (things like JQuery). I honestly am beginning to think that a WebApp is probably the best method for deployment (and it's not complicated to use Java for the backend) nor is it complicated to get the frontend talking to Java (i.e. servlets and JSPs) and it's extremely portable (the user only needs a browser)--just my two cents. – Jared Apr 05 '14 at 22:54

11 Answers11

3

Usually, as far as games go, it's preferable to use full screen mode instead of using a maximized window. You can do this in Java by using:

GraphicsEnvironment gfxEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gfxDev = gfxEnv.getDefaultScreenDevice();

Window window = new GameWindow();
gfxDev.setFullScreenWindow(window);

If you still want to use a regular frame and center the content panel, you need to define some of the GridBagLayout constraints. It's impossible to tell which without out seeing the code for the rest of the components on that screen, but consider the following:

  1. GridBagConstraints.fill
  2. GridBagConstraints.anchor
  3. GridBagConstraints.weightx
  4. GridBagConstraints.weighty

And finally, regarding setting the screen to the largest size, it is already addressed here: Java JFrame Size according to screen resolution

Community
  • 1
  • 1
ethanfar
  • 3,733
  • 24
  • 43
  • For me it does not matter wheter it goes maximized or full screen, the only problem is that the grey patches should not come... everything should be maximized or adjusted according to full screen – Daksh Shah Apr 03 '14 at 14:13
  • This has to do with the GridBagLayout constraints that you defined. If you could post more of your code (specifically the code that builds the UI components and containers) it would probably be easier to help you. – ethanfar Apr 03 '14 at 16:18
  • could you please check from the files i have and tell me which all do you want me upload, i will just put the snip of the files in my project – Daksh Shah Apr 04 '14 at 09:58
  • It's quite hard to understand from the file names. I'd say the file which code you quote in the "Updae 1" block. What's needed is the code that creates the frame, and the panels within it. – ethanfar Apr 04 '14 at 12:41
  • From first glance it seems that the problem might be that you set the layout of the frame's content pane to null, this means that there's no layout manager to take care of component location and size on resize. – ethanfar Apr 04 '14 at 13:12
  • Now how can i change layout? I tried to change and everything messed up on its own and i even could not move it to place it properly – Daksh Shah Apr 04 '14 at 13:27
  • It's pretty hard to fix things when all the code is in one place. Try refactoring (specifically "extract method"), after that it should be simpler. Also, you can try getting the layout working with place holder panels (in order to reduce the complexity of the fix), and only once it works put in the real components. – ethanfar Apr 04 '14 at 13:35
  • Sorry i did not get you :P – Daksh Shah Apr 04 '14 at 13:52
2

I am also having same requirement as you have, below code works for me.

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,d.width,d.height); // i assume you have extended JFrame
Anchit Pancholi
  • 1,174
  • 4
  • 14
  • 40
0

try this, hope it works for you as well.

MyFrame mFrame= new MyFrame();
mFrame.setVisible(true);
mFrame.setExtendedState(mFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
ajitksharma
  • 4,523
  • 2
  • 21
  • 40
0

I know this is a terrible answer because I don't have time to write any code. Have you tried creating a listener so you can get the proper maximum size once the window is actually created, and then setting the GridBagConstraints weightx and weighty properties accordingly?

Cory Smith
  • 27
  • 1
  • 7
  • Hey, I am new to java programming and I make approximately everything from Netbeans GUI. So I do not know much how to code all that, care to explain? – Daksh Shah Mar 31 '14 at 06:17
0

Did you try this code

JFrame frame = new JFrame();
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setVisible(true);

You can get full screen size of any device by "Toolkit.getDefaultToolkit().getScreenSize()" in java. Above code I set frame size to fullscreen.

int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;

You can get hight and width of screen to your code by using above codes. I think this will be a help.

  • Does your code make all the elements go full screen? I mean that would this code still produce grey patches? – Daksh Shah Apr 02 '14 at 10:28
  • If you can change element size by setwidth() and setheight() methods, then you can assign thise width and height values to it. Then It will display your software full screen without gray patches. – codethilina Apr 02 '14 at 10:35
  • I cannot for each one of them, I have way too many fields. I want something which will set for all of it... Sorry if i misunderstood it :P – Daksh Shah Apr 02 '14 at 10:40
  • If you loading elements to a jpanel just simply chage jpanel size to like that in above. I think it will works acording to your needs. – codethilina Apr 02 '14 at 10:47
  • sorry I don`t have any more ideas. – codethilina Apr 02 '14 at 10:53
0

You can easily call

setExtendedState(MAXIMIZED_BOTH); on jframe or

use bellow code to set screen size to any PC.

//size of the screen

 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

//height of the task bar

Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
    int taskBarSize = scnMax.bottom;

//available size of the screen

 setSize(screenSize.width, screenSize.height - taskBarSize);
    setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight());

if u want you can remove taskbar size to get full screen anyway this is the code and this will help you.

Harsha
  • 377
  • 1
  • 8
  • 21
  • @HarsMadushan The thing is that i know how to set it through maximized both, but that causes the grey patches to come, rather i want everything to adjust the position or stretch – Daksh Shah Apr 03 '14 at 14:15
0

Try setting image as a background to you JFrame. So it will adjust with frame size

How to set Jframe Background Image in GroupLayout Java

so even in full screen it will be adjusting..

Community
  • 1
  • 1
sampopes
  • 2,646
  • 1
  • 22
  • 34
0

if you use panel then you can resize according to panel, it shows in full panel size

yourinternalframe.setSize(mainPanel.getSize());
yourinternalframe.show();

this may be not seem as your real need, you may do something according to this

engtuncay
  • 867
  • 10
  • 29
0

I took a look at the code that you attached for FormTTS.java, what I found out is that your screen was set as using the absolute layout hardcoded to some numbers of pixels. Look at the following code:

Menu.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

Menu.getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 230, 530, 320));

Your JFrame is not using the GridBagLayout, instead it's using AbsoluteLayout from Netbeans library. So I guess you generated these UI codes with the tools from Netbeans.

And then regarding your picture that does not fill all the screen when maximized: jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freetts/equations.png")));

Menu.getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1530, 990));

Same problem here, it's hardcoded to some numbers of pixels.

If you want everything to be centered when you maximized your screen, I think the only way to do is to use the gridbag layout for your JFrame and this requires you to update almost everything in your code. And you will need to fully understand how GridBagLayout works. Here is the place to start.

However if you only want the background image to fill the screen you can follow the steps here to let the picture scaled to fill the size of JLabel: Resize a picture to fit a JLabel

If it still doesn't work, you should also get the size of the screen (from one of the answers here) and then set the prefferedSize of the JLabel with those values in addition of scaling the image.

Community
  • 1
  • 1
0

To add on to @eitanfar's answer, the best way of enabling fullscreen in Java is using the FSEM (FullScreen Exclusive Mode) API.


As he stated, this is achieved by setting the windows as fullscreen on the GraphicsDevice you want the window to appear fullscreen on, usually the default one. Even if your device does not support FSEM (id est isFullscreenSupported() returns false), setting the window as fullscreen will still partially work as the API will emulate fullscreen. The only safety check is to verify whether the GraphicsEnvironment is headless (isHeadless()). If it is, then there are no devices to display to.


The advantage FSEM gives you is that all graphics processing is run on the GPU (the GraphicsDevice is the GPU, not the monitor), therefore making it faster on most systems. In your program's options, you can allow the user to choose to enable or not FSEM so that they can run at optimal performance.


However, the system's repaint events are undefined when in FSEM, you're better off using active rendering, therefore you're better off ignoring repaint (setIgnoreRepaint(true)) and then using a custom thread for drawing.

ThePyroEagle
  • 153
  • 12
-1

I am having a similar problem with my application. the nearest I have come is to set all components that reside on top to either component.setOpaque(false), or component.setBackground(new Color(255,255,255,0)). you could also try panel.setVisible(false) for the unused panels. its hard to offer up code with out the entire program but give this a whirl:

Menu.setBackground(new Color(255,255,255,0);
JRave
  • 81
  • 1
  • 8
  • How can setvisible false solve my problem? Can you please explain in a bit more detail? – Daksh Shah Mar 30 '14 at 07:00
  • much in the same way that you have to set your JFrame to setVisible(true) in order to see the frame it can be applied to panels and other components as well. if you set the surrounding unused panels to setVisible(false) it should increase the amount of area for your center panel. if that is not the case then your picture is not a high enough pixilation to accommodate your screen to begin with. – JRave Mar 30 '14 at 07:09
  • @JRrave I do not think that is what i am looking for here, i think what i want is to resize all the components to full screen or something like that. And i am not using jPanels everywhere and also i am not able to understand what you are saying. I am a 15 year old newbie Sorry. If you need any info to know what i want just ask... – Daksh Shah Mar 30 '14 at 07:12
  • you can think of components as anything that can be placed "ON TOP" of a JFrame. see my answer – JRave Mar 30 '14 at 07:20
  • I think @JRave is refering to your "grey background" problem, though not solving your sizing problem, it is a fair answer, imho. :-) – Udo Klimaschewski Apr 05 '14 at 23:57