Full Screen Exclusive Mode - java

I want to implement full screen exclusive mode in my already made program, my main class is freeTTS.java which is:
package freetts;
public class FreeTTS {
public static void main(String[] args) {
new FormTTS().setVisible(true);
}
}
The other code of the whole program is in FormTTS.java which is a subclass of JFrame.
I tried to put the code to make it full screen in here but it gave all sorts of different errors, Do I have to put the code in FreeTTS or FormTTS?
Here is my file struct: (Note: FormTTS is another java file)
See i want to remove the pinkish whole border:

From your last question, The answer may have been incompatible with the netbeans GUI builder formatting and with your program design, So here is an example that may be more compatible. Try it out and see what happens.
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class FormTTS extends JFrame {
private boolean isFullScreen = false;
private JButton button;
public FormTTS() {
initComponents();
initFullScreen();
}
private void initComponents() {
setLayout(new GridBagLayout());
button = new JButton(
"I'm a smallbutton in a Huge Frame, what the heck?!");
add(button);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initFullScreen() {
GraphicsEnvironment env = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
isFullScreen = device.isFullScreenSupported();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(isFullScreen);
setResizable(!isFullScreen);
if (isFullScreen) {
// Full-screen mode
device.setFullScreenWindow(this);
validate();
} else {
// Windowed mode
this.pack();
this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FormTTS().setVisible(true);
}
});
}
}

You should just be able to do the following, although I recommend having a read of the official guide on fullscreen exclusive mode.
FormTTS ftts = new FormTTS();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gd.setFullScreenWindow(ftts);
ftts.setUndecorated(true);
ftts.setVisible(true);

Related

JFrame doesn't appear unless method invoked in main

I have a class which create a JFrame with an image but everytime I create the class and run the method to instantiate it, it doesn't appear. However, I have noticed that if I was to create the exact same class and run the same method in the main then the frame appears.
This is most of the code from the class with the JFrame that I am trying to create:
JFrame myFrame= new JFrame();
public void CreateFrame()
{
JLabel background=new JLabel(new ImageIcon("image.jpg"));
myFrame.add(background);
background.setLayout(new FlowLayout());
myFrame.setLayout(new GridLayout(1,1));
myFrame.setSize(360,250);
myFrame.setUndecorated(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
myFrame.setLocation((dim.width/2-170), dim.height/2-125);
myFrame.pack();
myFrame.setVisible(true);
}
If I run the code
MyClass mc = new MyClass();
mc.CreateFrame();
in a method in another class it doesn't come up. However, if I run the exact same code in a main method, it works.
For example, this doesn't work:
Example 1
public class otherClass extends JFrame
{
public void MethodA()
{
MyClass mc = new MyClass();
mc.CreateFrame();
}
public static void main(String[] args)
{
otherClass oc = new otherClass();
oc.MethodA();
}
}
but this does work
Example 2
public class otherClass extends JFrame
{
public void MethodA()
{
//CODE
}
public static void main(String[] args)
{
otherClass oc = new otherClass();
oc.MethodA();
MyClass mc = new MyClass();
mc.CreateFrame();
}
}
Can anyone see where I'm going wrong? Sorry if a stupid mistake, I'm still getting to grips with Java.
Thanks
EDIT
import javax.swing.ImageIcon;
import javax.swing.JWindow;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.Timer;
import java.util.Date;
import javax.swing.JFrame;
public class MyClass
{
JFrame homeFrame = new JFrame();
public void createFrame()
{
JLabel background=new JLabel(new ImageIcon("images.jpg"));
myFrame.add(background);
background.setLayout(new FlowLayout());
myFrame.setLayout(new GridLayout(1,1));
myFrame.setSize(360,250);
myFrame.setUndecorated(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
myFrame.setLocation((dim.width/2-170), dim.height/2-125);
myFrame.setAlwaysOnTop(true);
myFrame.pack();
myFrame.setVisible(true);
durationOfTime();
}
public void durationOfTime()
{
MainProgram mp = new MainProgram();
long startTime = System.currentTimeMillis();
long elapsedTime = 0L;
int count =0;
while (elapsedTime < 2*1000)
{
if(count==0)
{
mp.launchInitiation();
}
count+=1;
elapsedTime = (new Date()).getTime() - startTime;
}
myFrame.setVisible(false);
mp.homeFrame.setVisible(true);
}
public static void main(String[] args)
{
MyClass mc = new MyClass();
mc.createFrame();
}
}
Full code from class with JFrame trying to make. I am trying to use this JFrame as a splash screen but whatever class I call
MyClass mc = new MyClass();
mc.createFrame();
from, it just doesn't appear. Two seconds do pass by before my main GUI appears up but this method is supposed to be called in a login type frame. However, I have tested it with a blank JFrame / GUI to appear upon button click also and it still doesn't appear.
EDIT2
I also previously tried this SplashScreen example by # http://examples.oreilly.com/jswing2/code/ch08/SplashScreen.java but I couldn't get it to work (same problem, appears when called from main but not when called from action listener)
import java.awt.*;
import javax.swing.*;
public class SplashScreen extends JWindow {
private int duration;
public SplashScreen(int d) {
duration = d;
}
// A simple little method to show a title screen in the center
// of the screen for the amount of time given in the constructor
public void showSplash() {
JPanel content = (JPanel)getContentPane();
content.setBackground(Color.white);
// Set the window's bounds, centering the window
int width = 450;
int height =115;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height-height)/2;
setBounds(x,y,width,height);
// Build the splash screen
JLabel label = new JLabel(new ImageIcon("oreilly.gif"));
JLabel copyrt = new JLabel
("Copyright 2002, O'Reilly & Associates", JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
Color oraRed = new Color(156, 20, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
// Display it
setVisible(true);
// Wait a little while, maybe while loading resources
ClassToLoad ctl = new ClassToLoad();
try {
Thread.sleep(duration);
ctl.initiate();
} catch (Exception e) {}
setVisible(false);
}
public void showSplashAndExit() {
showSplash();
System.exit(0);
}
public static void main(String[] args) {
// Throw a nice little title page up on the screen first
SplashScreen splash = new SplashScreen(10000);
// Normally, we'd call splash.showSplash() and get on with the program.
// But, since this is only a test...
splash.showSplashAndExit();
}
}
I added the code in the lines with ClassToLoad and this SplashScreen is called on an action listener, what happens is the program waits the 2 seconds that I tell it to, no frame appears, and then the main class that I wanted to load while the splash screen is visible loads. I tried this method first but this didn't work which lead to me using the code listed above this edit
EDIT 3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFrame extends JFrame implements ActionListener
{
JPanel thePanel = new JPanel(null); //layout
JButton button = new JButton();
public void startGUI()
{
this.setLayout(new GridLayout(1,1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CREATEMYPANEL();
this.add(thePanel);
this.setTitle("NO_TITLE_SET");
this.setSize(400,400);
this.setVisible(true);
this.setResizable(true);
}
public void CREATEMYPANEL()
{
button.setLocation(242,151);
button.setSize(100,50);
button.addActionListener(this);
button.setText("button");
thePanel.add(button);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
System.out.println("button has been pressed ");
SplashScreen splash = new SplashScreen(1000);
splash.showSplash();
}
}
public static void main(String[] args )
{
TestFrame tf = new TestFrame();
tf.startGUI();
}
}
An example of where I call splash screen from. Still doesn't work. Also, just a note that the image I am loading is a local image
Apologies for bad question formatting
Your code works for me except for some details I noticed:
You're calling setSize(...) and then calling pack(). Probably your image isn't being loaded and thus your JFrame has a size of 0, 0. (And thus it looks like it never appears). .pack() and .setSize(...) are mutually exclusive.
You're setting the JLabel's layout manager to FlowLayout but never adding anything to it. (You can safely remove it)
I see you're importing java.util.Timer if you want to dispose the JFrame after 2 seconds, then you should be using a javax.swing.Timer instead. Otherwise you could get problems related to threading.
Also don't forget to place your program on the Event Dispatch Thread (EDT) as Swing is not thread safe
Following above recommendations you can have this code:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SplashscreenSample {
private JFrame myFrame;
private JLabel background;
private Timer timer;
public void createFrame() {
timer = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
myFrame.dispose();
#SuppressWarnings("serial")
JFrame frame = new JFrame(getClass().getSimpleName()) {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timer.stop();
}
});
myFrame = new JFrame(getClass().getSimpleName());
try {
background = new JLabel(new ImageIcon(new URL("http://cdn.bulbagarden.net/upload/thumb/6/6b/175Togepi.png/250px-175Togepi.png")));
} catch (MalformedURLException e) {
e.printStackTrace();
}
myFrame.add(background);
timer.setInitialDelay(2000);
timer.start();
myFrame.setLayout(new GridLayout(1, 1));
myFrame.setUndecorated(true);
myFrame.setAlwaysOnTop(true);
myFrame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
myFrame.setLocation((dim.width/2-170), dim.height/2-125);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SplashscreenSample().createFrame());
}
}
Or you can use the Splashscreen class...
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class SplashScreen extends JWindow {
private int duration;
public SplashScreen(int d) {
duration = d;
}
// A simple little method to show a title screen in the center
// of the screen for the amount of time given in the constructor
public void showSplash() {
ImageIcon icon = null;
try {
icon = new ImageIcon(new URL("http://www.cqsisu.com/data/wallpapers/5/718448.gif"));
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JPanel content = (JPanel) getContentPane();
content.setBackground(Color.white);
// Set the window's bounds, centering the window
int width = icon.getIconWidth();
int height = icon.getIconHeight();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width) / 2;
int y = (screen.height - height) / 2;
setBounds(x, y, width, height);
// Build the splash screen
JLabel label = new JLabel(icon);
JLabel copyrt = new JLabel("Copyright 2002, O'Reilly & Associates", JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
Color oraRed = new Color(156, 20, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
// Display it
setVisible(true);
// Wait a little while, maybe while loading resources
loadResources();
setVisible(false);
}
public void loadResources() {
TestFrame tf = new TestFrame();
try {
Thread.sleep(duration);
tf.startGUI();
} catch (Exception e) {
}
}
public static void main(String[] args) {
SplashScreen splash = new SplashScreen(10000);
splash.showSplash();
}
}
Try creating and setting the frame visible in a constructor of the class.
Could it be that while your program is counting
while (elapsedTime < 2*1000)
{
if(count==0)
{
mp.launchInitiation();
}
count+=1;
elapsedTime = (new Date()).getTime() - startTime;
}
it is blocking for said 2 seconds and waiting for this method to finish, return back to the create method and then just finish that one?
it seems this could be a better comment, but i need 50 rep for some reason to comment.

Java memory consumption for Swing GUI

I have the following piece of code,It just opens a JTextPane in a Jframe..
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultCaret;
public class TextPane extends JFrame{
public static TextPane instance;
private static JTextPane pane = new JTextPane();
private static JScrollPane scroll = new JScrollPane();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TextPane.getInstance().init();
}
});
}
private static TextPane getInstance() {
if(null == instance){
instance = new TextPane();
}
return instance;
}
private void init() {
pane.setFont(new Font("Courier new", Font.PLAIN, 12));
pane.setLayout(new BorderLayout());
pane.setBackground(Color.black);
pane.setForeground(Color.white);
pane.setCaretColor(Color.green);
pane.setDragEnabled(false);
DefaultCaret caret = (DefaultCaret) pane.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
scroll.setViewportView(pane);
add(scroll);
setTitle("Dummy");
setSize(500 , 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setVisible(true);
}
}
To analyze memory used by application i ran this code and once the GUI comes i verified Memory(Private working set) in Task manager ,(By right click on this application in Task manager Application tab and Go to Process).
For simply opening the GUI it showed around 16000K . is it normal?? If not how can i check the actual memory usage, which we can say this is the memory used by this application(For opening GUI).
Please help.

How to use JFrame to open another JFrame window on the other monitor?

I am writing a program designed to work on a two monitor system. I have to separate JFrame objects, and have it so be default, the first frame instance opens. The user then has to drag that frame over to a specific monitor, or leave it in place. When they click a button on that frame, I want the program to open up the second frame on the opposite monitor.
So, How would I figure out which monitor a frame object is on, and then tell another frame object to open on the opposite one?
Looking up GraphicsEnvironment, you can easily find out the bounds and location of each screen. After that, it is just a matter of playing with the location of the frames.
See small demo example code here:
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestMultipleScreens {
private int count = 1;
protected void initUI() {
Point p = null;
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
p = gd.getDefaultConfiguration().getBounds().getLocation();
break;
}
createFrameAtLocation(p);
}
private void createFrameAtLocation(Point p) {
final JFrame frame = new JFrame();
frame.setTitle("Frame-" + count++);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JButton button = new JButton("Click me to open new frame on another screen (if you have two screens!)");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GraphicsDevice device = button.getGraphicsConfiguration().getDevice();
Point p = null;
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
if (!device.equals(gd)) {
p = gd.getDefaultConfiguration().getBounds().getLocation();
break;
}
}
createFrameAtLocation(p);
}
});
frame.add(button);
frame.setLocation(p);
frame.pack(); // Sets the size of the unmaximized window
frame.setExtendedState(Frame.MAXIMIZED_BOTH); // switch to maximized window
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestMultipleScreens().initUI();
}
});
}
}
Yet, consider reading carefully The Use of Multiple JFrames, Good/Bad Practice? because they bring very interesting considerations.

Create banner/tool bar in java acting like Windows Taskbar

I want to create desktop application, a banner / tool bar, in java (I'm using swing in netbeans) and I want it to act same as windows Task bar, means desktop icons will rearrange according to banner position.
How to do so?
Thanks for any responses.
one of way is use JWindow or Modal and un_decorated JDialog, for example
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SlideText_1 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
final JWindow window = new JWindow();
final JPanel windowContents = new JPanel();
JLabel label = new JLabel("A window that is pushed into view..........");
windowContents.add(label);
window.add(windowContents);
window.pack();
window.setLocationRelativeTo(null);
final int desiredWidth = window.getWidth();
window.getContentPane().setLayout(null);
window.setSize(0, window.getHeight());
window.setVisible(true);
Timer timer = new Timer(15, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int newWidth = Math.min(window.getWidth() + 1, desiredWidth);
window.setSize(newWidth, window.getHeight());
windowContents.setLocation(newWidth - desiredWidth, 0);
if (newWidth >= desiredWidth) {
((Timer) e.getSource()).stop();
window.getContentPane().setLayout(new BorderLayout()); //restore original layout
window.validate();
window.setVisible(false);
}
}
});
timer.start();
}
private SlideText_1() {
}
}
Shahar asked this question on my behalf. I thought that it could be done using pure Java, but sadly as far as I know, in this case, Java is a dead end.
You need to use windows API and for that you will need to use Java Native Interface (JNI).
Best way is to create a DLL using C or C++ (use the header windows) and import it into the Java code.

What affects the enabled status of the zoom button on a JFrame for Mac?

I have a Java program that I wrote that changes whether or not a JFrame is resizable depending on application state. In Windows this works great, when it is resizable the maximize button is enabled when it's not the button is disabled. However, on my Mac when I change resizable back to true the zoom button does not become enabled but the window DOES become resizable - the resize grabber shows.
Unfortunately, I haven't been able to replicate this behavior in a simple application. Only in my complex app does this appear. So I'm wondering other than the resizable state of a JFrame what affects if the zoom button is enabled?
Here's a simple example that shows the expected behavior on Mac OS X; you might compare it to what you're doing for reference.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class FrameTest extends JPanel implements Runnable {
private JFrame f;
private boolean test;
public static void main(String[] args) {
EventQueue.invokeLater(new FrameTest());
}
#Override
public void run() {
f = new JFrame("Test");
test = f.isResizable();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public FrameTest() {
this.add(new JToggleButton(new AbstractAction("Test") {
#Override
public void actionPerformed(ActionEvent e) {
test = !test;
f.setResizable(test);
}
}));
}
}

Categories

Resources