private void close () {
WindowEvent winClosing;
winClosing = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosing);
}
i am trying to close the previous Jframe with help of close function mentioned above by putting this code in the back button highlighted in the picture attached
this.close();
MainMenu obj = new MainMenu();
obj.setVisible(true);
problem is that above code closes my whole application instead of closing the frame which is now activated
Here is a quick demo of (what I think) you are trying to do.
Please note:
a. I do not think it is the right approach to achieve this functionality. Consider using JDialog or internal frames for the secondary windows.
b. Look at it as an example of an MCVE. In fact it would have been much better to demonstrate the issue using one secondary frame, instead of two.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainWindow extends JFrame {
private JFrame frame1, frame2;
public MainWindow() {
super("Main");
//make 2 other frames
frame1 = new Frame1(this);
frame2 = new Frame2(this);
setSize(400, 300);
//add listener to quit if when window closes
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
//add buttn top open one frame
JButton btnFrame1 = new JButton("Frame 1");
btnFrame1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//make frame 1 visible
frame1.setVisible(true);
//make this invisible
setVisible(false);
}
});
getContentPane().add(btnFrame1, BorderLayout.NORTH);
//add button to open a second frame
JButton btnFrame2 = new JButton("Frame 2");
btnFrame2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//make frame 1 visible
frame2.setVisible(true);
//make this invisible
setVisible(false);
}
});
getContentPane().add(btnFrame2, BorderLayout.SOUTH);
//make main frame visible
setVisible(true);
}
public static void main(String[] args) {
new MainWindow();
}
public void showMain() {
//make this one invisible
frame1.setVisible(false);
frame2.setVisible(false);
//make main window visible
setVisible(true);
}
}
class Frame1 extends JFrame {
public Frame1(MainWindow mainWindow) {
super("Frame 1");
setSize(300, 200);
//add listener to show main this window closes
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent arg0) {
mainWindow.showMain();
}
});
//add btn to hide this window and show main
JButton btnFrame1 = new JButton("Back");
btnFrame1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mainWindow.showMain();
}
});
getContentPane().add(btnFrame1, BorderLayout.SOUTH);
getContentPane().setBackground(Color.CYAN);
//make it invisible
setVisible(false);
}
}
class Frame2 extends JFrame {
public Frame2(MainWindow mainWindow) {
super("Frame 2");
setSize(300, 200);
//add listener to show main this window closes
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent arg0) {
mainWindow.showMain();
}
});
JButton btnFrame1 = new JButton("Back");
btnFrame1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mainWindow.showMain();
}
});
//add btn to hide this window and show main
JButton btnMain = new JButton("Back");
btnMain.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
mainWindow.showMain();
}
});
getContentPane().add(btnMain, BorderLayout.SOUTH);
getContentPane().setBackground(Color.YELLOW);
//make it invisible
setVisible(false);
}
}
Don't hesitate to ask for clarifications as needed.
Related
I have a parent JDialog opened from button on a JFrame. The parent JDialog itself has a child JDialog that is opened from a button on the parent dialog. When I close the parent dialog and open it again using the button on the frame, I do not want to child dialog to also open.
Is there a way to prevent the child dialog from opening, unless the user explicitly presses on the button on the parent dialog?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MultiDialog {
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
final JDialog d1 = new JDialog();
final JDialog d2 = new JDialog(d1);
d1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d2.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d1.setSize(new Dimension(150, 100));
d1.setTitle("Parent");
d1.setLocation(50, 50);
d2.setTitle("Child");
d2.setSize(new Dimension(100, 100));
d2.setLocation(150, 150);
JFrame f = new JFrame("App");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b1 = new JButton("Show Parent Dialog");
f.add(b1);
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
}
});
JButton b2 = new JButton("Show Child Dialog");
d1.add(b2);
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
d2.setVisible(true);
}
});
f.pack();
f.setVisible(true);
}
}
);
}
}
Add a WindowListener to your Parent JDialog and use the windowClosing method by overiding it.
From now on, when you're closing the Parent, the Child's visible attribute becomes false until we're clicking on the Parent's button again.
d1.addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent arg0) {
d2.setVisible(false);
};
});
Full Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MultiDialog {
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
final JDialog d1 = new JDialog();
final JDialog d2 = new JDialog(d1);
d1.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d2.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d1.setSize(new Dimension(150, 100));
d1.setTitle("Parent");
d1.setLocation(50, 50);
d2.setTitle("Child");
d2.setSize(new Dimension(100, 100));
d2.setLocation(150, 150);
JFrame f = new JFrame("App");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b1 = new JButton("Show Parent Dialog");
f.add(b1);
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
}
});
d1.addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent arg0) {
d2.setVisible(false);
};
});
JButton b2 = new JButton("Show Child Dialog");
d1.add(b2);
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
d2.setVisible(true);
}
});
f.pack();
f.setVisible(true);
}
}
);
}
}
I have a JFrame gui im making and I need help.
Im trying to find out how to add a action to my button.
as in using it in a "if" statement or make i print something out when you push it.
thanks!
code:
package agui;
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
JButton button = new JButton("click me");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
You want the "addActionListener" method, something like:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the button");
}
});
I have the following problem:
I have a main application window (JFrame) that fills the whole screen.
When a button is clicked, a smaller window appears, to let the user input some Data. While the User does this, the main window should neither jump in front of it, nor allow interaction.
Solution to that: open a modal JDialog. Lets call that Dialog 1.
But when a button within this Dialog is clicked, a NEW window (yes/no-dialog) is supposed to pop up.. and, again, needs to act modal on the already modal JDialog Dialog 1. Trying to do that, the second Dialog keeps disappearing behind the first one.
I tried making Dialog 1 a JFrame but then, of course, I loose the "modal" bit. Forcing Dialog 1 to stay in from still keeps the main window's Button clickable.
What am I missing? How can I put a modal swing window OVER another modal swing window?
Edit:
minimal example of one not-really working version:
Main class for opening:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
Main window:
public class MainWin extends JFrame {
public MainWin(){
this.setSize(800,800);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Dia1(MainWin.this);
}
});
this.setVisible(true);
}
}
First Dialog:
public class Dia1 extends JDialog {
public Dia1(final JFrame parent){
super(parent, true);
this.setSize(400, 400);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Dia2(parent);
}
});
}
}
Second Dialog:
public class Dia2 extends JDialog {
public Dia2(JFrame parent){
super(parent, true);
this.setSize(200, 200);
JButton b = new JButton("click hehe");
this.getContentPane().add(b);
this.setVisible(true);
}
}
PS: I just realised: Dialog 2 is not hidden, as I suspected.. it is simply not there. Most likely because the parent window is blocked from the Modal Dialog?
Here is an MCVE of modality working as advertised.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ModalOverModal {
private JComponent ui = null;
ModalOverModal() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridLayout());
ui.setBorder(new EmptyBorder(40,40,40,40));
final JButton b1 = new JButton("Open Modal Dialog");
b1.setMargin(new Insets(40, 200, 40, 200));
ui.add(b1);
final JButton b2 = new JButton("Open 2nd Modal Dialog");
ActionListener al1 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b1, b2);
}
};
b1.addActionListener(al1);
ActionListener al2 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b2, "Close Me!");
}
};
b2.addActionListener(al2);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
ModalOverModal o = new ModalOverModal();
JFrame f = new JFrame("Modal over Modal");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
I'm a java beginner and I tried making a basic program that will delete a certain file in the temp files in Windows. It did delete the file without a problem when I hadn't implemented the JPanel & JFrame but I haven't had any luck since. It is supposed to delete the file when the "Delete for sure" jbutton is pressed and exit the program when the "exit" jbutton is pressed. All it does right now is bring up the GUI and nothing else. Not even system out prints. Here is the code:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* User: Andrew
* Date: 12/4/12
* Time: 7:09 PM
* To change this template use File | Settings | File Templates.
*/
public class DeleteFile {
public static void main (String args[]) throws IOException {
frame.setVisible(true);
frame.setName(boxname);
frame.setSize(100, 150);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
button1.setText(buttontext);
button1.setVisible(true);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
}
});
class Action1 implements ActionListener {
public void actionPerformed (ActionEvent e) {
deleteFile();
JLabel label = new JLabel("Deletion was successful");
JPanel panel = new JPanel();
panel.add(label);
}
}
class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
}
public void windowEvent (WindowEvent e) {
System.exit(0);
}
}
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Delete for sure?");
panel.add(button);
button.addActionListener (new Action1());
panel.setName(boxname);
JButton button2 = new JButton("Exit");
panel.add(button2);
button2.addActionListener (new Action2());
// JLabel label = new JLabel(filePath);
// panel.add(label);
}
static String buttontext = "Delete file for sure?";
static String boxname = "Trepix Temp File Deleter";
static String filePath = "C:\\Users\\Andrew\\AppData\\Local\\Temp\\CamRec0\\cursor-1.ico";
static JFrame frame = new JFrame();
static JButton button1 = new JButton();
static JPanel panel = new JPanel();
public static boolean fileIsValid() {
File file = new File(filePath);
if (file.exists()) {
return true;
} else {
return false;
}
}
public static void deleteFile() {
if (fileIsValid() == true) {
File file = new File(filePath);
file.delete();
}
}
}
class Action1 implements ActionListener {
public void actionPerformed (ActionEvent e) {
deleteFile();
JLabel label = new JLabel("Deletion was successful");
JPanel panel = new JPanel();
panel.add(label);
}
}
The panel object is never placed in any container that is part of a hierarchy leading to a top-level window. In other words, it's not placed in anything that is sitting in either a JFrame or JDialog, and so it will never be displayed.
class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
}
public void windowEvent (WindowEvent e) {
System.exit(0);
}
}
It makes no sense to place this windowEvent method in an ActionListener, since that is part of a WindowListener, something completely different. Why not simply call System.exit(0); in the actionPerformed(...) method?
Also your code shouldn't have any static fields or methods as that is antithetical to object-oriented programming.
I have a JFrame that I want closed when the user clicks off of it. I have two JTextFields and a JButton (username, password, submit). When I give them all the FocusListener, anytime the user goes from one field to another the window closes. How can I allow the user to go from field to field and only close it if the user clicks anywhere OUT of the pop up window?
public class LoginForm {
static JTextField userName;
static JTextField password;
static JButton submit;
JFrame main;
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
UserSession session;
public LoginForm(){
Handler handle = new Handler(); //inner class
LoginFormFocusListener fl = new LoginFormFocusListener(); //inner class
main = new JFrame("Please Login");
main.setUndecorated(true);
main.setBounds((dim.width/2) - (500/2),(dim.height/2) - (150/2),500,150);
main.setVisible(true);
main.setAlwaysOnTop(true);
main.setResizable(false);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
userName = new JTextField(10);
password = new JTextField(10);
main.setLayout(new GridLayout(0,1));
JPanel panel = new JPanel();
main.add(panel);
panel.add(new JLabel("Username: "));
panel.add(userName);
panel.add(new JLabel("Password: "));
panel.add(password);
submit = new JButton("Submit");
panel.add(submit);
userName.addFocusListener(fl);
password.addFocusListener(fl);
submit.addFocusListener(fl);
submit.addActionListener(handle);
}
}
... (unimportant methods and "Handler" class omitted)
public class LoginFormFocusListener implements FocusListener{
public void focusGained(FocusEvent fe) {
System.out.println("focus gained...");
System.out.println("click off of this window to close...");
}
public void focusLost(FocusEvent fe){
System.out.println("focus lost...");
WindowEvent winEvt = new WindowEvent(main, 0);
winEvt.getWindow().dispose();
}
}
//test
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new LoginForm();
}
});
}
Don't use a FocusListener for this since these are for components that gain and lose the focus, not for top level windows. Perhaps use a WindowListener listening for the window is deactivated or iconified.
For example:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class WindowListenerFun {
public static void main(String[] args) {
JPanel panel = new JPanel();
panel.add(new JTextField(10));
panel.add(new JButton("button"));
JFrame frame = new JFrame("Bad Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent wEvt) {
((JFrame)wEvt.getSource()).dispose();
}
#Override
public void windowDeactivated(WindowEvent wEvt) {
((JFrame)wEvt.getSource()).dispose();
}
});
}
}
EDIT: I misread your code; the other answer is correct--you need to use a WindowFocusListener instead of a FocusListener.
public class Listener extends WindowAdapter
{
public void windowLostFocus(WindowEvent e)
{
Window w = e.getWindow();
e.setVisible(false);
e.dispose();
}
}
and
main.addWindowFocusListener(new Listener());
Edit2: replaced placeholder with window closing code.
Then you add a focus listener to individual menu components, it gets fired whenever a component loses focus. You only want it to get fired when the window loses focus, so add it to the window instead.
main.addWindowFocusListener(f1);
should fix your problem.