How to make full screen Java app.? - java

I want to make a Java app. with full screen which would run correctly on all screen resolution.
I tried Toolkit in my JFrame it made my frame full screen but some its components (e.g. JButton, JTextField) were not shown when I run it on 800 x 600 screen size, but properly shown in 1600 x 900 screen size.
How to make full screen Java app. with robust component layout?

may be you used 'null' for your JFrame
setLayout(null).
Please use your required layout for the JFrame

Try it :
JFrame jf = new JFrame();
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setUndecorated(true);
jf.setVisible(true);

Try this:
setExtendedState(JFrame.MAXIMIZED_BOTH);

this.setExtendedState(JFrame.MAXIMIZED_BOTH);
you can try the above one it will resize the whole java app window to the available desktop resolution.

Related

How to add a background image to a JFrame with no panels, without using a JLabel?

I want to add a background image to a JFrame which doesn't have any panels. It is a project I'm working on and I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code to do that and also I'm using netbeans. Is there any solution for this?
I want to add a background image to a JFrame which doesn't have any panels.
The content pane of the frame is a JPanel, so yes it does have panels.
I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code
If you want a background image then you will need to change your code to make sure the content pane can display the image. So yes you will need to change your code whether you use a JLabel of a JPanel that paints an image.
Check out Background Panel for code that will allow you to use either approach.
The key is that you need to set the content pane of your frame BEFORE you start adding components to the frame. So the code might look something like:
BackgroundPanel panel = new BackgroundPanel( yourImage );
frame.setContentPane( panel );
frame.add(northPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);
I don't know what the Netbeans generated code looks like so I'll leave it up to you to figure out where to put the code.

Why will image not add when JFrame has .setSize

I'm a somewhat novice programmer and I'm have some trouble adding an image to my frame. While I know how to add images generally, this specific case it does not work.
public class Tutorial extends JFrame{
Tutorial(){
JFrame frame = new JFrame("ImageTutorial");
frame.setVisible(true);
frame.setSize(750,850);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
ImageIcon image = new ImageIcon(getClass().getResource("Green Block.png"));
JLabel imagelabel = new JLabel(image);
imagelabel.setBounds(10, 10, 75, 75);
imagelabel.setOpaque(true);
frame.add(imagelabel);
Now, I've located the problem but I don't understand 'why' its a problem. When I remove
frame.setSize(750,850);
the image shows, but when its there it doesn't. How can the frame's size impact the image showing and how can I get around it?
Just curious, logically, what makes you think a frame should be visible before you add any components? Logically speaking, wouldn't it seem right to add your components first, then make the frame visible. It's like displaying a painting in an art gallery even before the painter has painted anything on it. It just makes no sense. I highly doubt setting the size has anything to do with it. IF you don't set the size of the frame, then the frame appears as small as possible. When you resize the frame, it causes a repaint, then showing the label you add. But generally, you want to always set frame visible after all you components are added, to avoid this problem.
Side note: You should stay away from null layouts. You need to learn to use Layout Managers and let them do the dynamic sizing and locating for you.

Can you put JFrames inside Full Screen Exclusive Mode?

To be honest, I didn't quite know how to express my question in the title. So if someone has a clearer idea of what i'm trying to ask, then please be so kind as to edit it, for the greater good of mankind.
I have the following code:
public class Main {
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
JFrame frame = new JFrame();
Window window = new Window(frame);
JButton btn = new JButton("Quit");
window.add(btn);
if(gd.isFullScreenSupported())
gd.setFullScreenWindow(window);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(null);
}
});
}
}
What I want to do is make a library system, illustrated in full screen mode. Inside the full screen mode, there should be simple frame windows with text fields in them and so on... No heavy 2D/3D graphics required here...
My problem is, the quit button is resized to fit the entire screen. I've tried to use setBounds, setSize, etc, on the frame, window and even button... But it doesn't seem to let me be able to center the button in the middle.
So my question: Is it possible to make JFrame application windows inside a JFrame application window which is set to full screen exclusive mode? Or is it only possible to use full screen exclusive mode together with 2D/3D methods like drawing?
Note: The only thing I intend to use full screen mode for is to set it to a black background, and have a Jframe window ontop of this black background in the center of the application. It is to give the illusion that the application is actually a system.
Thank you.
P.S. please note that the quit button was merely for my own testing purposes. It won't be part of the actual system, so my question does not revolve around how to resize this particular quit button.
Fullscreen Exclusive Mode is not meant to have Swing components inside. It is meant as a high performance way to draw graphics in Java (and can benefit as much as possible of underlaying hardware if done right).
What you want to do could be done by using an undecorated JDesktopPane and maximize it over the dimensions of the screen. Then proceed with using a JInternalFrame to have a JFrame like window inside that JDesktopPane.
I've included links to the respective API sections, they should get you started.
I think that what you are after is an MDI Application. If that is what you are after you could take a look here.
Your problem is that you do not use layout manager correctly.
Learn about layout managers - the modules that put and size/resize the visual components on screen. Then decide how do you want your button to look like and then decide which layout manager to use. It will do its job.
You know what i also had the exact same problem and the only thing i know from my experience is that you can use jinternal frame and set its property undecorated to true then add your custom title bar according to your requirement.

How to set JDesktopPane size

i'm developing a java application with a main Panel in which i add a JDesktopPane.
users clicking on JButton, will show a JInternalFrame. My problem is that if i use Mac OSx look and feel, the JDesktopPane shows with correct size the JInternalFrame, if i use other look and feel, it will display JInternalFrame in the dimension of screen. This is what i do:
In main JPanel:
jDesktopPane1 = new JDesktopPane();
setContentPane(jDesktopPane1);
when users click JButton(s):
public void addInDesktop(JInternalFrame frame){
frame.setVisible(true);
try {
jDesktopPane1.setSize(frame.getSize());
jDesktopPane1.setPreferredSize((frame.getSize()));
System.out.println(frame.getSize().toString());
System.out.println(jDesktopPane1.getSize().toString());
jDesktopPane1.add(frame);
frame.moveToFront();
frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
The System.out.println says me that the dimension of JDesktopPane and JInternalFrame is the same, but why it show me the JInternalFrame in full screen size?
If you were to print out the sizes after jDesktopPane1.add(frame) you would then probably see the change in size. I would suggest that setting the desktop pane to the size of the internal frame is a bad practice - I would have thought it should remain the size of the client area of the parent JFrame the desktop pane sits in (the frame where you call setContentPane(jDesktopPane1) ), and just let the internal frame sit somewhere within it naturally.
What might be happening is that setting the pane and internal to the same size might make the look and feel assume you mean 'maximized', and so when it resolves the size of the desktop pane correctly it does something weird with the internal frame.
Try not calling setSize() / setPreferredSize() on the desktop pane and see if it then behaves consistently across L&Fs. Perhaps then you can figure out what best to do with the internal frame if its default size isn't what you want.
If you want to set the size as wide and high as your jframe where you are going to put in. You can use
variable_desktopPane.setBounds(this.getBounds());
By this I mean the Jframe you are calling its bounds.
I hope this helps, greetings.

Only small portion of window visible when shown. How do I show the whole thing?

I have a JFrame full of various components. When I run it, only a small portion appears. I have to resize the window (drag the edge with cursor) to make everything appear. Any idea how to fix this?
Call the pack() method on your frame before making it visible:
JFrame f = new JFrame();
...
f.pack();
f.setVisible(true);

Categories

Resources