Full screen frame issue - java

I have this code, which basically initializes a new JFrame and sets it full screen
public class FullScreenFrameTest extends JFrame {
public FullScreenFrameTest() {
super();
initFrame();
setVisible(true);
//full screen
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
device.setFullScreenWindow(this);
//end full screen
}
public void initFrame() {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setLocation(0, 0); //tried removing this, still doesn't work
setSize(screen.width, screen.height);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
new FullScreenFrameTest();
}
}
The problem is that it sometimes it does work and sometimes it doesn't, especially with Ubuntu: sometimes i see it full screen, sometimes the two bars are shown. What am i missing?
UPDATE
There is a screenshot:

Make sure to build your GUI on the event dispatch thread with invokeLater().
Update: Here's an SSCCE that seems to work consistently.
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FullScreenFrameTest extends JFrame {
public FullScreenFrameTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
add(new JLabel("Test", JLabel.CENTER));
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
device.setFullScreenWindow(this);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new FullScreenFrameTest();
}
});
}
}

Change
setLocation(0, 0); //tried removing this, still doesn't work
setSize(screen.width, screen.height);
To
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);
And call FullScreenFrameTest() constructor within SwingUtilities.invokeLater.
UPDATE
This might be due to the bug in java run time environment. Here is the bug reported http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7057287
To know more about this issue look at HERE.
UPDATE
As last tryI would suggest you to use JFrame#setAlwaysOnTop(true)

Related

JWindow not repainting until JFrame returns from while loop

Assume I have a JWindow and a JFrame called TestWindow and TestFrame which should both be shown at the same time, why does the TestWindow only draw a blank grey window without its label while the TestFrame is inside the while(true)???
And why if I remove the while(true) the TestWindow correctly shows?
This is as example of a more complex program where I need to show a splashscreen while the main application is starting which takes 1 or 2 seconds.
During this time the splashscreen should be shown regardless of the state of the main application, instead the splashscreen shows only when the main application is already finished, invokeAndWait doesn't actually wait for the splashscreen to be initialized.
TestFrame.java:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class TestFrame extends JFrame
{
public static void main(String args[])
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
#Override
public void run()
{
new TestWindow();
}
});
}
catch (Exception ex)
{
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new TestFrame();
}
});
}
public TestFrame()
{
super("TestFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new JLabel("sss"), BorderLayout.CENTER);
setSize(500, 120);
setVisible(true);
while(true)
{
//this makes the other not show
}
}
}
TestWindow.java:
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class TestWindow
{
public TestWindow()
{
JWindow jWindow = new JWindow();
JPanel contentPanel = new JPanel();
final JLabel label = new JLabel("Test Window");
contentPanel.add(label, BorderLayout.CENTER);
jWindow.add(contentPanel);
jWindow.setSize(300, 200);
jWindow.setAlwaysOnTop(true);
jWindow.setVisible(true);
}
}
EDIT:
By the way, I realized putting a
catch (Exception ex)
{
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
Thread.sleep(1000);
}
between the two TestWindow and TestFrame creation actually solves the problem, isn't there anything less hacky than this???
Your while (true) loop is being called on the Swing event dispatch thread, or EDT, tying it up and preventing this thread from doing its necessary actions including drawing all your GUI's and interacting with the user. The solution is simple -- 1) get rid of the while loop (as you already figured out), or 2) if the loop is absolutely necessary, do it in a background thread.
For more details on Swing threading, please check out Lesson: Concurrency in Swing.

JFrame code compiles and runs, but does not open window

Here you will see my code:
I am just trying to make a little window that displays "Hello, Java!".
I am currently running on Ubuntu 14.04. To go more in depth with my problem, the icon with the coffee cup shows up when I run the program like there is a window, but there is not window attached to it and if clicked, no window pops up.
Any help would be much appreciated!
public class HelloJava1 extends javax.swing.JComponent
{
public static void main(String[] args)
{
javax.swing.JFrame f = new javax.swing.JFrame("HelloJava1");
f.setSize(300, 300);
f.getContentPane().add(new HelloJava1());
f.setVisible(true);
}
public void paintComponent(java.awt.Graphics g)
{
g.drawString("Hello, Java!", 125, 95);
}
}
Additionally, I am compiling via command line using javac HelloJava1.java and running using java HelloJava1.
I am writing the code via gedit.
This code should work reliably:
import java.awt.*;
import javax.swing.*;
public class HelloJava1 extends JComponent {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame("HelloJava1");
// f.setSize(300, 300); better to pack() the frame
f.getContentPane().add(new HelloJava1());
// pack should be AFTER components are added..
f.pack();
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
SwingUtilities.invokeLater(r);
}
#Override // good practice..
public void paintComponent(java.awt.Graphics g) {
// always call super method 1st!
super.paintComponent(g);
g.drawString("Hello, Java!", 125, 95);
}
// instead of setting the size of components, it is
// better to override the preferred size.
#Override
public Dimension getPreferredSize() {
return new Dimension(300,300);
}
}

JFileChooser in front of fullscreen Swing application

I know there are some topics relative to this question (mainly this unanswered one and this one which is not handling full screen app).
I basically tried every combination of first topic sample and available methods (requestFocus, requestFocusInWindow, ...) but JFileChooser is always displaying behind the fullscreen app. I tried to change filechooser's parent too (setting it to null, itself or the parent frame) with no more success.
Have anyone a working example of this not-that-much-particular use case? Or is there a workaround to let user select files in a fullscreen app?
Unfortunately I can't say how you realised the implementation of the fullscreen app. But I tried a few things and came up with this:
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Gui extends JFrame {
public Gui() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
// Set some charateristics of the frame
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setBackground(Color.black);
this.setUndecorated(true);
JButton a = new JButton("PRESS ME!");
a.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(getParent());
}
});
this.add(a);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Gui();
}
});
}
}
Pay attention to the fact, that I created a new JFileChooser with the parent of the current JFrame as parameter.
EDIT:
I now even tried to set
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(new Gui());
and without the
this.setUndecorated(true);
it worked for me (got a nice fullscreen view and the JFileChooser was in the front). I believe the problem with the window decoration is linked to my window manager (I'm using linux with gnome).
Hopefully this solution works for you, if not:
Could you explain a little bit more, how you create the fullscreen app?
I would suggest instead of using using a Popup, just embed the JFileChooser into your application. It doesn't really make sense to have popups in a windowless application (Personally, I don't like popups much anyways).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FullScreenApp {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); // Ugh.
frame.setVisible(true);
final Box panel = Box.createVerticalBox();
JButton btn = new JButton();
btn.setText("Button");
panel.add(btn);
frame.add(panel);
final CustomFileChooser chooser = new CustomFileChooser(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
chooser.show();
}
});
}
public static class CustomFileChooser extends JFileChooser{
/** Node this chooser should be added to.
* There's likely a better way of doing this,
* but it was convenient for a quick example */
Container parent;
public CustomFileChooser(Container parent){
super();
this.parent = parent;
//Make configurations for your file chooser
setApproveButtonText("Open");
}
#Override
public void approveSelection(){
super.approveSelection();
//Perform accept action here
System.out.println(getSelectedFile().getAbsolutePath());
parent.remove(CustomFileChooser.this);
parent.repaint();
}
#Override
public void cancelSelection(){
super.cancelSelection();
//Perform cancel action here
System.out.println("Canceled");
parent.remove(CustomFileChooser.this);
parent.repaint();
}
#Override
public void show(){
rescanCurrentDirectory();
parent.add(this);
revalidate();
repaint();
}
#Override
public Dimension getMaximumSize(){
//Not necessary - But I felt the chooser should have a maximum size
return new Dimension(500,300);
}
}
}
FullscreenLib
//import
import argha.util.Fullscreen;
//this for JFrame
//true for setting Undecorated on/off
Fullscreen screen = new Fullscreen(this, true);
screen.DoTheWorkFor();
You can use my library for creating fullscreen windows and the problem you are facing hope it solved after that i tested and its working.
Hope it may helped you

Java, Swing and Xubuntu, JFrames not correct

I'm completely new to Linux and have been trying to get my (Windows built) Java Swing projects to work correctly on XUbuntu on a separate machine from executable jar files I built.
I've reduced the problem to a minimum amount of code
import java.awt.Dimension;
import javax.swing.*;
public class JFrameTest extends JFrame {
public JFrameTest(String title) {
super(title);
JLabel lab = new JLabel("Label");
this.getContentPane().add(lab);
this.setMinimumSize(new Dimension(200, 200));
this.pack();
this.setVisible(true);
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JFrameTest frame = new JFrameTest("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
On Windows I see:
In Xubuntu I just see a grey box and the label, not the Title or close icons etc. I also have to kill (-9) the jvm after I've ctrl zedded from the command line. I launched it with java -jar filename.jar
My Linux machine is running Xubuntu 11. I've installed the sun Java 6_24 JRE. I Googled for this and found something similar relating to Compziz(?) but this was allegedly fixed a while back. I'm a bit stuck now. I have got one Swing app that works OK in that it responds to buttons OK but still doesn't show the Title etc. Any help would be much appreciated.
I don't use Xubuntu, but the general rule is that any code that updates a GUI should be executed on the Event Dispatch Thread. See the section from the Swing tutorial on Concurrency.
The examples from the Swing tutorial all use a format like this:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
add( new JLabel("Label") );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new SSCCE() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
Suggestion: Start the GUI on the EDT. Vis.
import java.awt.Dimension;
import javax.swing.*;
public class JFrameTest extends JFrame {
public JFrameTest(String title) {
super(title);
JLabel lab = new JLabel("Label");
this.getContentPane().add(lab);
this.setMinimumSize(new Dimension(200, 200));
this.pack();
this.setVisible(true);
}
public static void main(String args[]) {
// Costruct & show the GUI on the EDT
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JFrameTest frame = new JFrameTest("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
For more information see Concurrency in Swing.

JFrame Maximize window

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.

Categories

Resources