#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    24
    Rep Power
    0

    GUI Close the new window and keep main program


    Hello

    I have this code in GUI, it will ask the user to enter name. And it will display welcome message.

    I want to close the welcome window after clicking the Exit button but keeping the main program open.

    How I can do that?

    PHP Code:
    package newpackage;

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;

    public class 
    NewWindow extends JFrame{
        
        private 
    int FRAME_WIDTH 300;
        private 
    int FRAME_HEIGH 130;
        
    JPanel pNorthpCenterpSouth;
        
    JLabel question;
        
    JTextField input;
        
    JButton jSubmitjExit;
        
    String lab1;
        
        public 
    NewWindow(){
            
    super("Welcome: ");
            
    setSize(FRAME_WIDTHFRAME_HEIGH);
            
    setLayout(new BorderLayout(510));
            
            
    pNorth = new JPanel();
            
    pNorth.setLayout(new GridLayout(1155));
            
            
    question = new JLabel("Hello, please enter your name: "JLabel.CENTER);
            
    question.setFont(new Font("Serif"Font.BOLD16));
            
    question.setForeground(Color.red);
            
            
    pNorth.add(question);
            
    getContentPane().add(pNorthBorderLayout.NORTH);
            
            
    pCenter = new JPanel();
            
    pCenter.setLayout(new FlowLayout());
            
            
    input = new JTextField(25);
            
    pCenter.add(input);
            
            
    getContentPane().add(pCenterBorderLayout.CENTER);
            
            
    pSouth = new JPanel();
            
    pSouth.setLayout(new FlowLayout());
            
            
    jSubmit = new JButton("Submit");
            
    jSubmit.setForeground(Color.BLUE);
            
    jExit  = new JButton("Exit");
            
    jExit.setForeground(Color.red);        
            
            
    pSouth.add(jSubmit);
            
    pSouth.add(jExit);
            
            
    getContentPane().add(pSouthBorderLayout.SOUTH);
            
            
            
    jSubmit.addActionListener(new ActionListener() {
                public 
    void actionPerformed(ActionEvent e){
                    if(
    input.getText().equalsIgnoreCase("")){
                        
    JOptionPane.showMessageDialog(null"Please enter your name first."
                                
    "No input"JOptionPane.ERROR_MESSAGE);
                    
                        }else{
                            
    lab1 input.getText();
                            new 
    WelcomeMessage(lab1);
                        
                           
    // JOptionPane.showMessageDialog(null, "Shipment successfully entered.", 
                               // "Shipment Message", JOptionPane.INFORMATION_MESSAGE);
                        
    }
                    }       
             });
            
            
    jExit.addActionListener(new ActionListener() {
                    public 
    void actionPerformed(ActionEvent e){
                       
    System.exit(0);
                    }
                });
        }
        
        private static class 
    WelcomeMessage extends JFrame{
            
                
    int FRAME_WIDTH 350;
                
    int FRAME_HEIGH 120;
                
    JPanel pCenterpSouth;
                
    JLabel welcomewelcome2;
                
    JButton jBye;
                
    String lab1;
            
            public 
    WelcomeMessage(String input) {
                
    this.lab1 input;
                
                
    JFrame con = new JFrame();
                
                
    con.setResizable(false);
                
    con.setSize(FRAME_WIDTHFRAME_HEIGH);
                
    //con.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
    con.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                
    con.setVisible(true);
                
    con.setLocationRelativeTo(null);
                
    con.setTitle("Welcome Message");
                
    con.setLayout(new BorderLayout(510));
                
                
    pCenter = new JPanel();
                
    pCenter.setLayout(new GridLayout(2155));

                
    welcome = new JLabel("Hello " inputJLabel.CENTER);
                
    welcome.setFont(new Font("Serif"Font.BOLD16));
                
    welcome.setForeground(Color.blue);
                
                
    welcome2 = new JLabel("welcome to you in my program"JLabel.CENTER);
                
    welcome2.setFont(new Font("Serif"Font.BOLD16));
                
    welcome2.setForeground(Color.blue);
                
                
    pCenter.add(welcome);
                
    pCenter.add(welcome2);
                
                
    con.add(pCenterBorderLayout.CENTER);
                
                
    pSouth = new JPanel();
                
    pSouth.setLayout(new FlowLayout());
                
                
    jBye  = new JButton("Exit");
                
    jBye.setForeground(Color.red);        
                
                
    pSouth.add(jBye);
            
                
    con.add(pSouthBorderLayout.SOUTH);
                
                
    jBye.addActionListener(new ActionListener() {
                    public 
    void actionPerformed(ActionEvent e){
                       
    System.exit(0);
                    }
                });
                
            }
        }
        
        public static 
    void main (String [] args){
            
            
    JFrame frame = new NewWindow();
         
            
    frame.setResizable(false);
            
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);        
            
    frame.setVisible(true);
            
    frame.setLocationRelativeTo(null);
        }
        
    }
    // end NewWindow class 
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What is the program supposed to do if it does not have a way (the GUI) to communicate with a user?
    The program will keep executing if it has a thread running. See the API doc for the Thread class for a description.
    Last edited by NormR; February 4th, 2014 at 07:05 AM.

IMN logo majestic logo threadwatch logo seochat tools logo