Closing a JFrame using the keyboard - java

I am creating a basic screensaver for a small project for college and I am having trouble closing the JFrame after I open it. I have removed the outer panel so as to make it look a proper screensaver but I now have to open the Task Manager to close down the program, I want the window to close when I press a button on the keyboard how can I do that?
Thanks in advance.
-------EDIT--------
I have tried the first two methods given but I can't seem to get them to work properly. Here is my code for the frame so far:
import java.awt.Color;
import javax.swing.JFrame;
public class ScreensaverTest
{
public static void main( String[] args )
{
JFrame frame = new JFrame( "Screen Saver" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setUndecorated(true);
ScreenSaverJPanel screensaverTestJPanel = new ScreenSaverJPanel();
frame.add( screensaverTestJPanel );
frame.setBackground( Color.BLACK );
frame.setVisible( true );
frame.setLocation( 0, 0 );
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
--------EDIT---------
Also I am using the repaint method to move objects around the screen and I want some of them to move at different speed. I am using a random number generator for the positioning so adjusting the numbers isn't really an option, please help.

There are a lot of examples on how to do this.
You need to add an eventListener to the frame that listens for keyboard-inputs and then closes the frame.
Try looking at this: http://www.java2s.com/Code/Java/Swing-JFC/Reacttoframecloseaction.htm
You will have to replace the event with something keyboard-related, but that is the best way to go I would think..
Edit:
To respond to the edit in the post you could do something like this:
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Test {
public Test() {
final JFrame frame = new JFrame("Screen Saver");
frame.validate();
frame.setVisible(true);
KeyAdapter listener = new KeyAdapter() {
#Override public void keyPressed(KeyEvent e) {
frame.dispose();
}
};
frame.addKeyListener(listener);
}
public static void main(String[] args) {
new Test();
}
}
And voila - that should work. It compiles for me on Java 7.

Here is the code if you have only a single JFrame. You should add it when you initialize your components.
addKeyListener(new KeyAdapter()
{
#Override
public void keyPressed(java.awt.event.KeyEvent evt)
{
processWindowEvent(new WindowEvent(getWindows()[0], WindowEvent.WINDOW_CLOSING));
}
});

Related

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 GUI Help (JFrame, JButton, etc)

Ok so im new to programming GUIs in Java and I need some help on how to add buttons and Labels. I went around looking at an example and I figured it this was how it worked to basically add a button:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUI_Tests extends JFrame{
public static void main(String[] args){
private JButton button;
button = new JButton("pls work");
add(button);
}
}
Well that didnt work at all... Can someone show me how its done and give me some pointers?
You should note that a JFrame has a default BorderLayout which, if not specified, will add a component to a BorderLayout.CENTER position.
When adding components to JFrame, if a different Layout is not specified, then you want to set a component's position, like add(button, BorderLayout.SOUTH);
Also you should use a constructor. Something like this
public class GUI_Tests extends JFrame {
public GUI_Tests(){
JButton button = new JButton("Pls work");
JLabel label = new JLabel("Pls work");
add(button, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
}
}
Also you need to remember to set the frame visible.
A simple running program would be something like this
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class GUI_Tests extends JFrame {
public GUI_Tests() {
JButton button = new JButton("Pls work");
JLabel label = new JLabel("Pls work");
add(button, BorderLayout.CENTER);
add(label, BorderLayout.WEST);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new GUI_Tests();
}
});
}
}
The main method may look somewhat crazy to you, but all it is doing is causing the program to run on the Event Dispatch Thread (EDT). I won't go into this too much as you are still a beginner.
What is going on in the run is that all I do is create an instance of me program, which extends JFrame. If I didn't extend JFrame, I would have to explicitly create a JFrame to run the program. All GUI programs need some top-level container to run.
Also you can see in the constructor, that when I added the button and label I set a layout position
add(button, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
as I stated earlier, when using a JFrame without specifying a different layout, you should use position with the default `BorderLayout. Other possible positions are
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER
BorderLayout.NORTH
BorderLayout.SOUTH
Take a look at [Laying Out Components Within a Container]Laying Out Components Within a Container)
Also, noticed I used pack(). What this does is make the frame the perfect with for the preferrsed sizes of the components. It is preferred to pack() rather then setSize() of a frame.
setDefaultCloseOperation(EXIT_ON_CLOSE); makes it so that when x out the frame, the program shuts down.
setLocationRelativeTo(null); set the frame's location in the center of the screen
setVisible(true); makes the frame visible. This should always be called to make the frame visible on start of the program.
Please have a look at the Swing tutorials
You are supposed to add your JButton to a JFrame, but it seems to me that you haven't even created a JFrame yet. You should first create an instance of GUI_Tests, then use the JFrame#add method with your JButton. For example:
public class GUI_Tests extends JFrame {
public GUI_Tests() {
super("My first Swing frame!");
this.setPreferredSize(new Dimension(640, 480));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create your GUI here
this.add(new JButton("Click on me :)"));
this.setVisible(true);
}
}
Actually, I wouldn't be mean to you, but I think you tried to go too fast: in my opinion, you should start by following some tutorial on how to deal with Swing :)
public class GUI_Tests extends JFrame {
private JButton button;
public GUI_Tests() {
setTitle("Title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so that the application properly closes when you click close
button = new JButton("pls work");
add(button);
pack(); // resize the frame to its contents
setLocationRelativeTo(null); // center the frame on the screen
}
public static void main(String args[]) {
// properly start a swing application
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
GUI_Tests gui = new GUI_Tests();
gui.setVisible(true); // set the frame visible
}
});
}
}
hope this helps you understand it a bit more. i didn't use any layouts in this code as its just a simple example, however, i highly recommend you to read on built-in layouts in swing and how to use them.
Here's one good start
You need to add your button to a JFrame The basic flow should look something like this:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class GUITests extends JFrame{
public GUITests() {
setTitle("Simple example");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button = new JButton("pls work");
add(button);
pack();
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
GUITests ex = new GUITests();
ex.setVisible(true);
}
});
}
}
The GUITests class extends the JFrame class so you can call all the methods that are visible on the JFrame class. In the main method It's just some boiler plate code that calls your constructor for you so that you can see your basic example.
Check out the tutorials here: http://zetcode.com/tutorials/javaswingtutorial/firstprograms/
type
setVisible( true );
setSize( 500 , 500 );
and I think you'll see something. Also get rid of private in the statement private JButton button But you should really put your code in the constructor of the GUI_Tests:
import javax.swing.*;
public class GUI_Tests extends JFrame {
public GUI_Tests() {
JButton button = new JButton( "Hello World" );
add( button );
setVisible( true );
setSize( 500 , 500 );
}
final public static void main( String[] args ) {
GUI_Tests tests = new GUI_Tests();
}
}
Check out the Java tutorial for more help, possibly here: http://docs.oracle.com/javase/tutorial/uiswing/start/index.html

Java Timers and TextFields

I have a panel where I have an uneditable Textfield. I want to be able to add a textField.setText(timer); sort of thing in the panel. I basically just want the JTextArea to display it like this: 0:0:0.
I've tried making Timers, TimerTasks, ActionListeners and the sort. I can't get the hang of this. Maybe it's because where I'm adding the timer? At the moment I tried adding it in a public void guiComponents() throws Exception{...}. This method holds all the properties for the panel, the frame it's in and other components.
Maybe I'm not understating the whole concept of timers. Origanlly I tried using a double-nested for loop to achieve this task, but because this has to go on while the rest of the program is running, I couldn't do that.
Don't use a JTextField for this. Use a JLabel to display text. Something like:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class TimerTime extends JFrame implements ActionListener
{
JLabel timeLabel;
public TimerTime()
{
timeLabel = new JLabel( new Date().toString() );
getContentPane().add(timeLabel, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
timeLabel.setText( new Date().toString() );
}
public static void main(String[] args)
{
TimerTime frame = new TimerTime();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
int time = 1000;
javax.swing.Timer timer = new javax.swing.Timer(time, frame);
timer.setInitialDelay(1);
timer.start();
}
}

Differences between Jbutton and Jpanel and JFrame ?

i make simple game , and it consist of 2 files , first file is "Alibaba.java" which is extended from JFrame , i used it to display general contents of the game !,
and the second file is "intro.java" which is extended from JPanel , i used it to show intro of the game which include (title & background & person) ,
my problem occured when i tried to add a simple button in the intro ! , i did a function to create the button , but the problem is when i run the game , the button which i added it don't appear !! , but when i tried add it from the first file which extended from JFrame , its appeared ! ,
so what is the problem in my code ? is JPanel don't accept JButtons ! or i must create the buttons from the JFrame file ?!
so i need to know how to add Jbutton inside Jpanel instead of add Jbutton in JFrame Direct !!,
this is my samples of my codes which contain the problem :
1st file (Alibaba.java)
package alibaba;
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Alibaba extends JFrame {
public Alibaba(){
super("Alibaba");
Intro intro = new Intro();
this.add(intro);
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = environment.getDefaultScreenDevice();
device.setFullScreenWindow(this);
}
public static void main(String[] args) {
Alibaba alibaba = new Alibaba();
}
}
2nd file(Intro.java) :
package alibaba;
import javax.swing.JButton;
public class Intro extends javax.swing.JPanel implements Runnable{
Thread _intro_run;
public Intro() {
_intro_run = new Thread(this);
_intro_run.start();
}
#Override
public void run() {
// Here i tried to add a button to the Intro !!!
this.add(this.createbutton("Exit"));
}
public JButton createbutton(String text){
JButton _button = new JButton(text);
return _button;
}
}
So please tell me what is the problem and how to solve it , sorry but iam new to java , new to programming games world ! ,, thank you :)
You have to add the JButton inside the main thread, cross thread Component manipulation is bad.
For example:
public Intro() {
JButton exitButton = new JButton("Exit");
this.add(exitButton);
}
Alternatively, use SwingUtilities.invokeLater(Runnable). For example, in your run method:
#Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
add(createbutton("Exit"));
}
}
}
Based on discussion it seems that you are overriding the paint or paintComponent methods. You need to call super in them, like:
void paint(Graphics g) {
super.paint(g);
// do other stuff to g
}
In addition, it is a bad sign that you have a JPanel that implements Runnable. In Java, all UI work (here you are using Swing components) is done from the Event Dispatching Thread - having an actual Swing component (your Intro class is a JPanel) Runnable flies in the face of that.
Suggestions:
Anytime you add a new component to a container, you have to tell the layout managers to layout the new and existing components. This is done by calling revalidate() on the JPanel receiving the button. You also should call repaint() on the JPanel after this.
You shouldn't do any of this in a background thread.
Most important read the Swing tutorials as they will tell you all of this and more.

Listen for when a Component is Shown for the First Time

I am trying to capture the very first moment when a component is shown on the screen without using 'dirty' solutions as with use of a timer.
Basically, I want to know the moment when I can safely start using getLocationOnScreen() method on the component.
I thought that component listener could help but no luck here. I am stuck for now and do not know which listener to use for this. Any suggestions?
Here is some sample code which shows that a component listener fails.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CompListenerTest
{
static ComponentListener cL = new ComponentAdapter()
{
#Override
public void componentShown(ComponentEvent e)
{
super.componentShown(e);
System.out.println("componentShown");
}
};
static MouseListener mL = new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e)
{
super.mousePressed(e);
JComponent c = (JComponent) e.getSource();
System.out.println("mousePressed c="+c.isShowing());
}
};
public static void main(String[] args)
{
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(300, 400));
p.setBackground(Color.GREEN);
p.addComponentListener(cL);
p.addMouseListener(mL);
System.out.println("initial test p="+p.isShowing());
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.RED);
contentPane.add(p);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Thanks in advance.
The reason a ComponentListener doesn't work is that it reports changes to the visible property - and that is true by default, even without being part of the component hierarchy.
To be reliably notified, use a HierarchyListener
Edit (musings about my knowledge evolution in regard to this question/answers, not sure what the netiquette has to say about doing it ... simply guide me if that's the wrong way to go :-)
First: the question as asked in the subject is not necessarily related to the actual problem (as commented by Boro below - any way to link to a comment?): there's no need to keep some kind of local flag to decide whether or not it is safe to send a getLocationOnScreen to a component, simply ask the component itself. Learn-item 1 for myself :-)
Second: The question as asked is quite interesting. Five experts (including myself, self-proclaimed), five different answers. Which triggered a bit of digging on my part.
My hypothesis: ComponentEvents are not useful for notification of (first-)showing. I knew that componentShown is useless because it's a kind-of propertyChange notification of the visible property of a component (which rarely changes). Puzzled about the suggested usefulness of moved/resized, though.
Constructing a use-case: fully prepare the frame in the example and keep it ready for later showing, a typical approach to improve perceived performance. My prediction - based on my hypothesis: resized/moved fired at prepare-time, nothing at show-time (note: the isShowing is what we are after, that is the latter). A snippet to add in the OP's example:
final JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(800, 600);
// f.pack();
JFrame controller = new JFrame("opener");
controller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Action open = new AbstractAction("open/hide second") {
#Override
public void actionPerformed(ActionEvent e) {
f.setVisible(!f.isVisible());
}
};
controller.add(new JButton(open));
controller.pack();
controller.setVisible(true);
Disappointment: no notification at prepare-time, notification at show-time, just as needed, my hypothesis seemed wrong ;-) Last chance: swap the setSize for a pack ... and voila, notification at prepare-time, no notification at show-time, happy me again. Playing a bit more: looks like ComponentEvents are fired if the a component is displayable, which may or may not be useful in some contexts but not if showing is the state we are after. The
New imperial rules (draft):
Do not use ComponentListener for notification of "showing". That's left-over from AWT-age.
Do use AncestorListener. That seems to be the Swing replacement, slightly misnomed notification of "added" which actually means "showing"
Do use HierarchyListener only if really interested in fine-grained state changes
I"ve use an AncestorListener and handled the ancestorAdded event.
Oddly, the ComponentListener works just fine when applied to the JFrame. Here is the altered source where I saw it work.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CompListenerTest
{
static ComponentListener cL = new ComponentAdapter()
{
#Override
public void componentShown(ComponentEvent e)
{
super.componentShown(e);
System.out.println("componentShown");
}
};
public static void main(String[] args)
{
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(300, 400));
p.setBackground(Color.GREEN);
System.out.println("initial test p="+p.isShowing());
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.RED);
contentPane.add(p);
JFrame f = new JFrame();
f.addComponentListener(cL);
f.setContentPane(contentPane);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Using AncestorListener and ancestorAdded worked for me. Here's sample code:
addAncestorListener(new AncestorListener() {
#Override
public void ancestorRemoved(AncestorEvent event) {}
#Override
public void ancestorMoved(AncestorEvent event) {}
#Override
public void ancestorAdded(AncestorEvent event) {
// component is shown here
}
});
static ComponentListener cL = new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
System.out.println("componentShown = "+e.getComponent().isDisplayable());
}
};
For most purposes you can go with the first call to ComponentListener.componentMoved (or if you are also interested in the size ComponentListener.componentResized). Those are called whenever the position/size of the component changes.

Categories

Resources