Display a JPanel on full screen - java

Hello I have a JPanel who contained a JTable and a TextField. I want that the display screen is displayed in full screen because in my case it appears half of the screen.here's not a parameter of screen size that I have to specify or change?
This is the result:
public MainWindow()
{
super("Fullscreen");
getContentPane().setPreferredSize(
Toolkit.getDefaultToolkit().getScreenSize());
pack();
setResizable(false);
show();

getContentPane().setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
pack();
setResizable(false);
show();
A few problems with the code:
You can't just set the size of the content pane to be the size of the screen. A frame has decorations (title bar, borders) that need to be considered.
The order of pack() and setResizable(...) is wrong. The pack() statement needs to be invoked AFTER you add components to the screen and after you set the resizable property of the frame.
The show() method is not used anymore.
If you just want to display the frame maximized the code is:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
I suggest you look at the FrameDemo found in the Swing tutorial on How to Make Frames for working code to get you started. I don't know where you found your current code example but I would lose the code and learn how to do Swing coding properly. Bookmark the Swing tutorial for examples of all Swing basics.

the frame has a setSize method, where you can pass the dimension you want.
to get the screen size you can use
Toolkit.getDefaultToolkit().getScreenSize()

setExtendedState(JFrame.MAXIMIZED_BOTH);

Add a ComponentListener with componentResized() method. When the size changed make equal your JPanel's and JTable's size to JFrame's size. And also, while defining JPanel and JTable make their sizes' equal to JFrame's size.

Related

Removing JFrame Border?

I have a Java Swing GUI and everything in my JFrame is offset by a few pixels. On MacOS, I had to offset everything by 12 pixels downward to account for it. On Windows, everything is shifted to the left and downward as well. I discovered that
setUndecorated(true);
removes the JFrame border (which I suspect is the cause of my problems) but it also removes the title bar.
Is there a way I can remove the JFrame border (or some other alternative to make sure everything is centered) and still keep the title bar? I need the title bar so that I can move the JFrame around and have the maximize/minimize/close functions.
Also, the layout is set to null in case that matters. (Everything I'm doing is pixel - based so I cannot set it to anything else).
Thanks.
I found a solution after Googling a bit more. Calling
getContentPane().setPreferredSize(...)
pack();
inside the JFrame constructor will adjust everything so that the titlebar and borders will not impact the view of the content pane.
For anyone else that may need this, you have to set the preferred size of the content pane specifically as that is what you want to appear correctly.
This way you can keep the titlebar and all the normal functionality of a JFrame window that you would otherwise lose with setUndecorated(true);.
The reason why you are adding 12px is because this is consume by the Title and border.
If you use setUndecorated(true) You will loose the title bar and you have to implement the addWindowListener to add a location changing of of an application.
The best way to do is:
Class Main{
public static void main(String[] args){
//JFrame
JFrame frame = new JFrame();
//Create a Main Panel and setPreferred Size and not set Size or set Bound
JPanel mainPanel = new JPanel(); //You value down
mainPanel.setPrefferedSize(new Dimension(x, y));
frame.add(mainPanel);
//and then in last add
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//the pack method help you to setSize of the JFrame According to the
//Component size
frame.pack();
frame.setVisible(true);
}
}

Java - jframe does not fit set dimensions

I've used methods in JFrame:
setSize(600, 600);
setResizable(true);
Next I've created a JButton, and set it's bounds to 0,0,600,600.
I've found that the button is a bit (~40) bigger than the window. It made me some problems when I tried to put a few components exactly where I wanted to. I am using null layout. How to fix it and make my frame exactly 600x600?
The obvious answer is to get rid of the null layout and use a LayoutManager instead. If you want a component to take all available space, use a BorderLayout and put the component in the BorderLayout.CENTER.
To answer your specific question: the size of the JFrame is 600 by 600, but that is not the size of its content pane. The JFrame also contains window decorations which take up some of the size.
You could try to remove those decorations, or simply make your JFrame bigger. Or start wondering about your requirement to have a JFrame with a content pane of exactly 600 by 600 while it is still resizable by the user.

How to auto resize JFrame when elements move on next line using FlowLayout

While you resize JFrame manually via mouse, elements on JPanel can go on next line if there is no enough space, same time new element-lines can be removed if JFrame is big enough
Example:
at this moment there are two lines to fit all JButtons
If I change JFrame width via mouse, JButtons will drop on next lines:
the problem is you can't see all JButtons, JFrame needs to be resized in height.
How to make JFrame auto resize in height to fit all elements according to set width?
This is not a very good design. Show me another professional application that works like this? People don't like to see the size of frames jump. The user is usually resizing the frame to fit the app on their desktop and will get frustrated if they fix the width and the height changes. Let the user control the size.
If you want all the buttons to display, then maybe a better solution is to just make the frame non resizable by using:
frame.setResizable( false );
frame.pack();
frame.setVisible();
Anyway, if you really want to do this then you would probably add a ComponentListener to the content pane of the frame. Then in the componentResized() method you would invoke pack().
However, this will cause multiple events to be generated every couple of pixels so you may also want to use:
Toolkit.getDefaultToolkit().setDynamicLayout( false );
So that the pack() is only done once when the mouse is released.
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
frame.setSize(400,200);

How can I determine the midpoint of the users display?

I've written a Swing application. I want to set the JFrame to be centred in the user's screen. I can set it to the approximate center of my screen using:
JFrame frame = new JFrame("Test Frame");
Rectangle r = new Rectangle(600, 300, 400, 400);
frame.setBounds(r);
Is there a way to define the Rectangle r such that the center of it is at the center of any screen?
Is setBounds() the wrong method for varying the frame position dynamically?
Do you want to center a JFrame in the screen? If so then simply call setLocationRelativeTo(null) on the JFrame after calling pack(). You really don't want to set the size of the JFrame if you can avoid it, but instead have your layout managers do the size setting for you. The pack() method will ask the containers' layout managers to lay out their components and set the best sizes for their components.
Edit
Regarding your question:
Thanks, that's great. Any chance you could tell me where I could find the method details within docs.oracle
Since this method can be called on a JFrame, simply go to the JFrame API. If this is a method of a parent class, the API will tell you and provide the link to the parent class and its method.

How to change the background color on a Java panel?

Right now, the background I get is a grey. I want to change it to black. I tried doing something like setBackground(color.BLACK); but it didnt work. Any suggestions?
public test()
{
setTitle("Adjustment Form");
setSize(670,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,6,2,2));
setVisible(true);
}
You could call:
getContentPane().setBackground(Color.black);
Or add a JPanel to the JFrame your using. Then add your components to the JPanel. This will allow you to call
setBackground(Color.black);
on the JPanel to set the background color.
I think what he is trying to say is to use the
getContentPane().setBackground(Color.the_Color_you_want_here)
but if u want to set the color to any other then the JFrame, you use the object.setBackground(Color.the_Color_you_want_here)
Eg:
jPanel.setbackground(Color.BLUE)
setBackground() is the right method to use. Did you repaint after you changed it? If you change it before you make the panel (or its containing frame) visible it should work
I am assuming that we are dealing with a JFrame? The visible portion in the content pane - you have to use jframe.getContentPane().setBackground(...);

Categories