How to close jframe panel or window - java

I have 2 frames/windows, I have Exit button on window 2, an from window 1 I launch window 2 and then exit it i.e setVisible(false);
When I execute window 2 I can easily click button exit and hide the current window, however when I launch window 2 from window 1, and then click exit button I get NullPointerException Error. then I instantiated it in the beginning with static and this error was gone, however the window 2 is not being closed/hidden its still there with no effect of button.
Window 1 code:
package com.my.jlms;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LibrarianMenu extends JFrame {
private JPanel contentPane;
private static LibrarianMenu frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LibrarianMenu() {
setTitle("Librarian");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 385, 230);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnPasswd = new JButton("Change Pass");
btnPasswd.setBounds(202, 76, 146, 39);
contentPane.add(btnPasswd);
btnPasswd.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChangePwd framee = new ChangePwd();
framee.setVisible(true);
}
});
}
}
Window 2 Code:
package com.my.jlms;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
public class ChangePwd extends JFrame {
private JPanel contentPane;
private static ChangePwd frame = new ChangePwd();;
private JButton btnExit;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new ChangePwd();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChangePwd() {
setResizable(false);
setTitle("Password!");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 266, 154);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btnExit = new JButton("Exit");
btnExit.setBounds(20, 80, 89, 30);
contentPane.add(btnExit);
btnExit.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
frame.setVisible(false);
}
});
}
}
Is there a solution I can set window 2 to hide ?

The problem here is that you are creating your frame, as your class, and not on the object frame, but you hide the frame which represents the object frame.
Change this line (in your actionListener's actionPerformed() method):
frame.setVisible(false);
to:
setVisible(false);

You can use dispose function for the purpose.see how dispose works.

If you want to close a JFrame, you could use the dispose() method.
Example:
public void actionPerformer(ActionEvent e)
{
if(e.getSource().equals(closeFrameButton)
{
dispose(); //This will close the current JFrame
}
}
NOTE: this is different to System.exit(0);. Using this will close the Java virtual machine. if you just want to close the frame, use dispose()

Related

How to get JDialog textFiels Value from JFrame

Hi I am new in Java coding and trying to design a user friendly desktop App with the help of JFrame and JDialog -
My JFrame is -
package com.myapp.ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;
public class MyFrame extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyFrame frame = new MyFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyFrame() {
setTitle("MyFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnClickMe = new JButton("Click Me");
btnClickMe.setBounds(271, 171, 115, 29);
contentPane.add(btnClickMe);
JCheckBox chckbxOpenDialog = new JCheckBox("Open Dialog");
chckbxOpenDialog.setBounds(25, 171, 139, 29);
contentPane.add(chckbxOpenDialog);
chckbxOpenDialog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chckbxOpenDialog.isSelected()== true){
MyDialog MD = new MyDialog();
MD.setModal(true);
MD.setVisible(true);
}
}
});
btnClickMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Here I want to get the value of textFieldName and textFieldEmail from JDialog
//after Click on Confirm Button in JDialog and closing/disposing it
}
});
}
}
My JDialog is -
package com.myapp.ui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textFieldName;
private JTextField textFieldEmail;
private JButton btnConfirm;
public static void main(String[] args) {
try {
MyDialog dialog = new MyDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public MyDialog() {
setTitle("MyDialog");
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
textFieldName = new JTextField();
textFieldName.setBounds(108, 26, 146, 26);
contentPanel.add(textFieldName);
textFieldName.setColumns(10);
textFieldEmail = new JTextField();
textFieldEmail.setBounds(108, 68, 146, 26);
contentPanel.add(textFieldEmail);
textFieldEmail.setColumns(10);
btnConfirm = new JButton("Confirm");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//passing the Name and Email field value in JFrame
}
});
btnConfirm.setBounds(132, 141, 115, 29);
contentPanel.add(btnConfirm);
MyFrame MF = new MyFrame();
}
}
Both the JFrame and JDialog are in the same package. I am trying to get the value of
textFieldName and textFieldEmail from JDialog to JFrame
if any one can guide me the best possible way, would be relly great.
You could:
Add setters to MyFrame
Pass MyFrame reference to MyDialog (using this) when it is created in the ActionListener
In the ActionListener in MyDialog set fields in MyFrame
There are probably better ways to do this. The built in JOptionPane produces modal dialog windows that wait for user input. These would be better rather than passing references of JFrames to JDialog.
Also note that JavaFX has largely replaced Java Swing for desktop UIs

Java Swing JTextField text one tick behind

I use this Java code for demonstration (Just created using eclipse)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class executor extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
executor frame = new executor();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public executor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
final JLabel lblNewLabel = new JLabel("New label");
contentPane.add(lblNewLabel, BorderLayout.NORTH);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
#Override
public void keyTyped(KeyEvent e) {
lblNewLabel.setText(textField.getText());
}
});
contentPane.add(textField, BorderLayout.CENTER);
textField.setColumns(10);
}
}
and if you type anything into the textfield the label shows everything exactly except prior to the last
Buttonpress. Is there any way to fix that? thank you!
The text field contents change only after the key listener is notified. Instead of trying to track key presses, it is better to listen to the changes in the contents of the text field. This ensures that you also catch changes made by other means, such as cut and paste. The interface for doing that is DocumentListener:
textField.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e) {
lblNewLabel.setText(textField.getText());
}
#Override
public void insertUpdate(DocumentEvent e) {
lblNewLabel.setText(textField.getText());
}
#Override
public void changedUpdate(DocumentEvent e) {
}
});

Opening a different frame with a button in Java

I am trying to open the menu frame using a button on the main frame. I added an event to the button and I tried calling the other class but it keeps giving me an error of ":: expected after this token"
This is my main frame
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
public static JPanel mainPane;
public final JButton menuButton = new JButton("New button");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
mainPane = new JPanel();
mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(mainPane);
mainPane.setLayout(null);
menuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Menu.main(String[] args);
}
});
menuButton.setBounds(76, 89, 104, 32);
mainPane.add(menuButton);
}
}
And this is my menu frame
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class Menu extends JFrame {
public static JPanel menuPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu frame = new Menu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Menu() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
menuPane = new JPanel();
menuPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(menuPane);
menuPane.setLayout(null);
JLabel menuTitle = new JLabel("Menu");
menuTitle.setBounds(194, 11, 46, 14);
menuPane.add(menuTitle);
}
}
change your action event to this.no need to call main method .create a new instance of Menu class instead.
menuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Menu menu = new Menu();
menu.setVisible(true);
}
});
if you relly want to call main method then use
menuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Menu.main(new String[0]);
}
});
the error is here
Menu.main(String[] args);//error
this is not a correct way of passing arguments to a methods.this is declaration of parameter list.
you can correct error by changing it to ,
String args[] = null;
Menu.main(args); //correct

How to place the JFileChooser on top of the specific Parent Window/Frame

For my development i use a 3 Monitors. When I open a my Java Application and dragged it to another screen and press a button to display the FileChooser. File Chooser appears on the Main monitor, which is set by the Windows. What is the wrong with this?
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(<parent Comp>);
What should i do to make it appear it(File Chooser) on top my Java Application?
It seems you are getting the parent in incorrect way
import java.awt.EventQueue;
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.JPanel;
import javax.swing.border.EmptyBorder;
public class MainWindow extends JFrame
{
private static final long serialVersionUID = 1708404088747322174L;
private JPanel contentPane;
/**
* Create the frame.
*/
public MainWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
final JFileChooser fileChooser = new JFileChooser();
final JButton button = new JButton("Test");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(final ActionEvent arg0)
{
fileChooser.showOpenDialog(button.getParent());
}
});
contentPane.add(button);
}
/**
* Launch the application.
*/
public static void main(final String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
final MainWindow frame = new MainWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
in this example JFileChooser place on top of parent component

Changing the Bounds of a Swing GUI and Naming of the JButtons

I know how to set the the bounds, so in the end a new setbounds() call would give the new bounds, but I dont know how long/wide should the new bound be, it depends on the input number of buttons like here for example :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EmptyBorder;
public class Book_GUI extends JFrame {
private EconomyClass eco;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_GUI frame = new Book_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//this.add(contentPane);
JButton btnBookFlight;
//eco = new EconomyClass();
//eco.setSeats(5);
for(int i=0;i<45;i++){
btnBookFlight = new JButton("Book" +i);
btnBookFlight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JButton button = (JButton)arg0.getSource();;
button.setBackground(Color.RED);
button.setOpaque(true);
}
});
btnBookFlight.setBounds(77, 351, 100, 23);
contentPane.add(btnBookFlight);
}
}
}
As you see the last 5 buttons are not visible, one has to enlarge the GUI a little bit with mouse... and first 10 buttons are smaller than others because after 9 the number digits increase which is logical but can I align all of them at the same order and size? Another issue, the button name "Book" is just for test it should be 1A Window, 1B Middle, 1C Aisle some space 1D Aisle,1E Middle,1F Middle, 1G Aisle some space 1H Aisle, 1I Middle, 1J Window and below these 2A Window... Just like in a plane, any hints how I can arrange the namings and the necessary space between them is highly appreciated!
You should avoid using null layout or absolute positioning for arranging swing components. Always use the best appropriate layout manager in the situation since it has a lot of advantages. The best layout to handle your current situation is GridLayout
Here is the modified version of your code using GridLayout
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EmptyBorder;
public class Book_GUI extends JFrame {
// private EconomyClass eco;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Book_GUI frame = new Book_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Book_GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new GridLayout(9, 5));
// contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// this.add(contentPane);
JButton btnBookFlight;
// eco = new EconomyClass();
// eco.setSeats(5);
for (int i = 0; i < 45; i++) {
btnBookFlight = new JButton("Book" + i);
btnBookFlight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JButton button = (JButton) arg0.getSource();
;
button.setBackground(Color.RED);
button.setOpaque(true);
}
});
// btnBookFlight.setBounds(77, 351, 100, 23);
contentPane.add(btnBookFlight);
}
pack();
}
}
Further read : A Visual Guide to Layout Managers
for assign dynamically names to a collection of JButton, you can using this:
List<JButton> listOfButtons = new ArrayList<JButton>(collection.size());
for (int i=0; i < collection.size(); i++) {
JButton button = new JButton();
listOfButtons.add(button);
}

Categories

Resources