I'm putting together a quick and dirty animation using swing. I would like the window to be maximized. How can I do that?
Provided that you are extending JFrame:
public void run() {
MyFrame myFrame = new MyFrame();
myFrame.setVisible(true);
myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
Something like this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame
{
public Test()
{
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
this.setMaximizedBounds(env.getMaximumWindowBounds());
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
Test t = new Test();
t.setVisible(true);
}
}
If you're using a JFrame, try this
JFrame frame = new JFrame();
//...
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
How about JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH)?
i like this version:
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Test
{
public static void main(String [] args)
{
final JFrame frame = new JFrame();
final GraphicsConfiguration config = frame.getGraphicsConfiguration();
final int left = Toolkit.getDefaultToolkit().getScreenInsets(config).left;
final int right = Toolkit.getDefaultToolkit().getScreenInsets(config).right;
final int top = Toolkit.getDefaultToolkit().getScreenInsets(config).top;
final int bottom = Toolkit.getDefaultToolkit().getScreenInsets(config).bottom;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int width = screenSize.width - left - right;
final int height = screenSize.height - top - bottom;
frame.setResizable(false);
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
The way to set JFrame to full-screen, is to set MAXIMIZED_BOTH option which stands for MAXIMIZED_VERT | MAXIMIZED_HORIZ, which respectively set the frame to maximize vertically and horizontally
package Example;
import java.awt.GraphicsConfiguration;
import javax.swing.JFrame;
import javax.swing.JButton;
public class JFrameExample
{
static JFrame frame;
static GraphicsConfiguration gc;
public static void main(String[] args)
{
frame = new JFrame(gc);
frame.setTitle("Full Screen Example");
frame.setExtendedState(MAXIMIZED_BOTH);
JButton button = new JButton("exit");
b.addActionListener(new ActionListener(){#Override
public void actionPerformed(ActionEvent arg0){
JFrameExample.frame.dispose();
System.exit(0);
}});
frame.add(button);
frame.setVisible(true);
}
}
I've tried the solutions in this thread and the ones here, but simply calling setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH); right after calling setVisible(true); apparently does not work for my environment (Windows 10, JDK 1.8, my taskbar is on the right side of my screen). Doing it this way still leaves a tiny space on the left, right and bottom .
What did work for me however, is calling setExtendedState(... when the window is activated, like so:
public class SomeFrame extends JFrame {
public SomeFrame() {
// ...
setVisible(true);
setResizable(true);
// if you are calling setSize() for fallback size, do that here
addWindowListener (
new WindowAdapter() {
private boolean shown = false;
#Override
public void windowActivated(WindowEvent we) {
if(shown) return;
shown = true;
setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
}
}
);
}
}
I ended up using this code:
public void setMaximized(boolean maximized){
if(maximized){
DisplayMode mode = this.getGraphicsConfiguration().getDevice().getDisplayMode();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
this.setMaximizedBounds(new Rectangle(
mode.getWidth() - insets.right - insets.left,
mode.getHeight() - insets.top - insets.bottom
));
this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}else{
this.setExtendedState(JFrame.NORMAL);
}
}
This options worked the best of all the options, including multiple monitor support. The only flaw this has is that the taskbar offset is used on all monitors is some configurations.
#kgiannakakis answer is fully correct, but if someone stuck into this problem and uses Java 6 on Linux (by example, Mint 19 Cinnamon), MAXIMIZED_BOTH state is sometimes not applied.
You could try to call pack() method after setting this state.
Code example:
public MainFrame() {
setContentPane(contentPanel); //some JPanel is here
setPreferredSize(new Dimension(1200, 800));
setMinimumSize(new Dimension(1200, 800));
setSize(new Dimension(1200, 800));
setExtendedState(JFrame.MAXIMIZED_BOTH);
pack();
}
This is not necessary if you are using Java 7+ or Java 6 on Windows.
Related
As far as I know, the setDivider() method of JSplitPane can take an int which is an absolute position from the left. For example, someSplitPane.setDivider(120); will set the divider 120px from the left. Is there any way I can do the same thing, but set the divider an absolute position from the right?
Simple implementation:
public class Window extends JFrame {
JSplitPane splitpane;
public Window() {
initComponents();
}
private void initComponents() {
setTitle("Debugging");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(640, 480));
splitpane = new JSplitPane();
System.out.println(splitpane.getSize().width); // prints 0
splitpane.setDividerLocation(splitpane.getSize().width - 120);
getContentPane().add(splitpane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Window().setVisible(true);
});
}
}
EDIT: I have written this code isolated from any existing code, and I reproduce the error. What looks to be happening is the JFrame is instantiating and appears on the desktop, and then about a second later, a 0 (from the commented print statement) is output to the console.
Assuming you create your GUI correctly by using the EDT you can add the following in the constructor of your class where you create the splitPane:
SwingUtilities.invokeLater(() ->
{
Dimension d = splitPane.getSize();
splitPane.setDividerLocation(d.width - 120);
});
This will add code to the EDT. So after the frame is visible and the split pane has been given a size, the divider location will be reset.
I made a few changes to your code and created this GUI.
I used a JFrame instead of extending a JFrame.
I set the preferred size of the JSplitPane, rather than the JFrame. Generally, you care more about the size of the JSplitPane than the JFrame. Usually, the JSplitPane will have two JPanels with Swing components, so you don't have to specify a preferred size at that point.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class JSplitPaneRight {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new JSplitPaneRight();
});
}
public JSplitPaneRight() {
initComponents();
}
public void initComponents() {
JFrame frame = new JFrame("JSplitPane Right");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createSplitPane(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JSplitPane createSplitPane( ) {
JSplitPane splitpane = new JSplitPane();
splitpane.setPreferredSize(new Dimension(640, 480));
int width = splitpane.getPreferredSize().width;
System.out.println(width); // prints 0
splitpane.setDividerLocation(width - 120);
return splitpane;
}
}
I have been working on making Pong in Java, but I can't seem to accurately set the size of the JFrame. Here is my main class:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Pong extends JFrame {
private final Game panel;
private final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public Pong() {
setTitle("Pong");
setSize(screenSize);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new Game(this);
add(panel);
}
public static void main(String[] args) {
new Pong();
}
}
When I run my program, the window takes up the whole screen, but when I try to get the dimensions of the screen, they are larger than those of the window, causing anything set at the lower extents of the window to get cut off. The only way I have found to get the true dimensions is to call this.getHeight(), this.getWidth(), or this.getSize() within the panel class. How can I get the true dimensions right off the bat?
You could use JFrame#setExtendedState(JFrame.MAXIMIZED_BOTH)
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
It has the same effect as when you enlarge a window in your OS
With this method, you can now have the "real" dimensions by calling the different methods mentionned above in the question.
Try to call method setExtendedState.
public class Pong extends JFrame {
private final Game panel;
public Pong() {
setTitle("Pong");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new Game(this);
add(panel);
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
//setVisible(true);
}
public static void main(String[] args) {
Pong pong = new Pong();
pong.setVisible(true);
}
}
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Pong extends JFrame {
private final Game panel;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
public Pong() {
setTitle("Pong");
setSize((int)width,(int)height);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new Game(this);
add(panel);
}
public static void main(String[] args) {
new Pong();
}
}
check this question How can I get screen resolution in java?
Put in your code:
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
Details you can find in documentation.
Another questions on same subject is here.
In my Java application I try to make a JFrame really fullscreen by using this code:
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
public MainFrame() {
super();
this.setTitle();
this.setUndecorated(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);
//this.pack();
}
}
But on my Mac I can still see the Dock and the top toolbar of the OSX. So how can I create a JFrame that really consumes my whole screen?
EDIT
I have to add that I want to call that JFrame from a eclipse plugin.
I haven't tried it yet, but Java has fullscreen API, which should meet your needs:
http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html
I know the answer. Firstly, I have to admit that the following trick won't work if you are making video or movie player or animation player. OK here is what i found after many tries:
Let's say that you want to make a JFrame (called frame) fullscreen when you press a button (called fullscreenButton).Then do the following :
import java.awt.*;
import javax.swing.*;
public class FullscreenJFrame extends JFrame{
private JPanel contentPane = new JPanel();
private JButton fullscreenButton = new JButton("Fullscreen Mode");
private boolean Am_I_In_FullScreen = false;
private int PrevX,PrevY,PrevWidth,PrevHeight;
public static void main(String[] args) {
FullscreenJFrame frame = new FullscreenJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,500);
frame.setVisible(true);
}
public FullscreenJFrame(){
super("My FullscreenJFrame");
setContentPane(contentPane);
//From Here starts the trick
FullScreenEffect effect = new FullScreenEffect();
fullscreenButton.addActionListener(effect);
contentPane.add(fullscreenButton);
fullscreenButton.setVisible(true);
}
private class FullScreenEffect implements ActionListener{
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(Am_I_In_FullScreen == false){
PrevX = getX();
PrevY = getY();
PrevWidth = getWidth();
PrevHeight = getHeight();
dispose(); //Destroys the whole JFrame but keeps organized every Component
//Needed if you want to use Undecorated JFrame
//dispose() is the reason that this trick doesn't work with videos
setUndecorated(true);
setBounds(0,0,getToolkit().getScreenSize().width,getToolkit().getScreenSize().height);
setVisible(true);
Am_I_In_FullScreen = true;
}
else{
setVisible(true);
setBounds(PrevX, PrevY, PrevWidth, PrevHeight);
dispose();
setUndecorated(false);
setVisible(true);
Am_I_In_FullScreen = false;
}
}
}
}
I hope you enjoyed it
An example:
import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FullScreenJFrame extends JFrame{
private GraphicsDevice vc;
public FullScreenJFrame(){
super();
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc= e.getDefaultScreenDevice();
JButton b = new JButton("exit");
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
dispose();
System.exit(0);
}
}
);
this.setLayout(new FlowLayout());
this.add(b);
setFullScreen(this);
}
public void setFullScreen(JFrame f){
f.setUndecorated(true);
f.setResizable(false);
vc.setFullScreenWindow(f);
}
public static void main(String[] args){
new FullScreenJFrame();
}
}
private Dimension screenSize; /* class level vars */
private swidth , sheight;
/*In GUI creating method:put this code: */
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
sheight = screenSize.height;
swidth = screenSize.width;
setBounds(0, 0, swidth, sheight-40);
NB: using swidth, sheight vars give you the liberty to further adjust.
Best way is to use int vars in place of - 40 e.g sheight/swidth - margin etc.
Here margin should come from parameter table. Getting full control of situation.
Direct usage also possible as: setBounds(0,0,screenSize.width, screenSize.height);
Use com.apple.eawt.FullScreenUtilities. And make sure to test that the system is running Mac OS.
public void enableFullscreen(Window window, boolean bool) {
if (System.getProperty("os.name").startsWith("Mac OS")) {
com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window, bool);
}
}
I know this question has been answered before, but it's just not working for me. I followed the instructions from here: How to change JProgressBar color?
import javax.swing.*;
import java.awt.*;
public class ProgressBarTest extends JFrame {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UIManager.put("ProgressBar.background", Color.orange);
UIManager.put("ProgressBar.foreground", Color.black);
UIManager.put("ProgressBar.selectionBackground", Color.red);
UIManager.put("ProgressBar.selectionForeground", Color.green);
JProgressBar progressBar = new JProgressBar(0,100);
progressBar.setValue(50);
f.add(progressBar, BorderLayout.PAGE_END);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
All I am getting is the same old colors.
I'm using Mac OS X 10.7.3 and Java 1.6. I tried the CrossPlatformLookAndFeel and it works with the new colors. However I want this in the default look and feel. How can I do this?
To override Look & Feel defaults, make the change before constructing the GUI on the event dispatch thread, as shown below.
On the com.apple.laf.AquaLookAndFeel, the progress bar's UI delegate is an instance of com.apple.laf.AquaProgressBarUI. As you have found, it ignores many defaults in favor of the native component. If a novel color scheme is required, consider supplying your own UI delegate, as shown here.
AquaProgressBarUI:
CustomProgressUI:
ProgressBar UI Defaults:
ProgressBar.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]
ProgressBar.border: javax.swing.plaf.BorderUIResource#47f08ed8
ProgressBar.cellLength: 1
ProgressBar.cellSpacing: 0
ProgressBar.cycleTime: 3000
ProgressBar.font: sun.swing.SwingLazyValue#6446d228
ProgressBar.foreground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.horizontalSize: javax.swing.plaf.DimensionUIResource[width=146,height=12]
ProgressBar.repaintInterval: 20
ProgressBar.selectionBackground: javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]
ProgressBar.selectionForeground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.verticalSize: javax.swing.plaf.DimensionUIResource[width=12,height=146]
ProgressBarUI: com.apple.laf.AquaProgressBarUI
SSCCE:
import java.awt.*;
import javax.swing.*;
public class ProgressBarTest extends JFrame {
public static void main(String args[]) {
UIManager.put("ProgressBar.repaintInterval", 100);
UIManager.put("ProgressBar.border",
BorderFactory.createLineBorder(Color.blue, 2));
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
f.setLayout(new GridLayout(0, 1, 5 , 5));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createBar());
f.add(createBar());
f.add(createBar());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private JProgressBar createBar() {
JProgressBar progressBar = new JProgressBar(0, 100);
progressBar.setValue(50);
return progressBar;
}
});
}
}
In my Java application I try to make a JFrame really fullscreen by using this code:
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
public MainFrame() {
super();
this.setTitle();
this.setUndecorated(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);
//this.pack();
}
}
But on my Mac I can still see the Dock and the top toolbar of the OSX. So how can I create a JFrame that really consumes my whole screen?
EDIT
I have to add that I want to call that JFrame from a eclipse plugin.
I haven't tried it yet, but Java has fullscreen API, which should meet your needs:
http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html
I know the answer. Firstly, I have to admit that the following trick won't work if you are making video or movie player or animation player. OK here is what i found after many tries:
Let's say that you want to make a JFrame (called frame) fullscreen when you press a button (called fullscreenButton).Then do the following :
import java.awt.*;
import javax.swing.*;
public class FullscreenJFrame extends JFrame{
private JPanel contentPane = new JPanel();
private JButton fullscreenButton = new JButton("Fullscreen Mode");
private boolean Am_I_In_FullScreen = false;
private int PrevX,PrevY,PrevWidth,PrevHeight;
public static void main(String[] args) {
FullscreenJFrame frame = new FullscreenJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,500);
frame.setVisible(true);
}
public FullscreenJFrame(){
super("My FullscreenJFrame");
setContentPane(contentPane);
//From Here starts the trick
FullScreenEffect effect = new FullScreenEffect();
fullscreenButton.addActionListener(effect);
contentPane.add(fullscreenButton);
fullscreenButton.setVisible(true);
}
private class FullScreenEffect implements ActionListener{
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(Am_I_In_FullScreen == false){
PrevX = getX();
PrevY = getY();
PrevWidth = getWidth();
PrevHeight = getHeight();
dispose(); //Destroys the whole JFrame but keeps organized every Component
//Needed if you want to use Undecorated JFrame
//dispose() is the reason that this trick doesn't work with videos
setUndecorated(true);
setBounds(0,0,getToolkit().getScreenSize().width,getToolkit().getScreenSize().height);
setVisible(true);
Am_I_In_FullScreen = true;
}
else{
setVisible(true);
setBounds(PrevX, PrevY, PrevWidth, PrevHeight);
dispose();
setUndecorated(false);
setVisible(true);
Am_I_In_FullScreen = false;
}
}
}
}
I hope you enjoyed it
An example:
import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FullScreenJFrame extends JFrame{
private GraphicsDevice vc;
public FullScreenJFrame(){
super();
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc= e.getDefaultScreenDevice();
JButton b = new JButton("exit");
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
dispose();
System.exit(0);
}
}
);
this.setLayout(new FlowLayout());
this.add(b);
setFullScreen(this);
}
public void setFullScreen(JFrame f){
f.setUndecorated(true);
f.setResizable(false);
vc.setFullScreenWindow(f);
}
public static void main(String[] args){
new FullScreenJFrame();
}
}
private Dimension screenSize; /* class level vars */
private swidth , sheight;
/*In GUI creating method:put this code: */
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
sheight = screenSize.height;
swidth = screenSize.width;
setBounds(0, 0, swidth, sheight-40);
NB: using swidth, sheight vars give you the liberty to further adjust.
Best way is to use int vars in place of - 40 e.g sheight/swidth - margin etc.
Here margin should come from parameter table. Getting full control of situation.
Direct usage also possible as: setBounds(0,0,screenSize.width, screenSize.height);
Use com.apple.eawt.FullScreenUtilities. And make sure to test that the system is running Mac OS.
public void enableFullscreen(Window window, boolean bool) {
if (System.getProperty("os.name").startsWith("Mac OS")) {
com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window, bool);
}
}