Forum Stats

  • 3,873,419 Users
  • 2,266,565 Discussions
  • 7,911,531 Comments

Discussions

Changing the title of parent frame window when the Dialog box closes

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Feb 28, 2017 9:09PM in New To Java

Hi,

I have a JFrame window which pops up a dialog box at the time of pressing a menu item. Now in dialog box i have a login button which performs some processing and then closes the dialog box. But before closing it must change the title of parent window. I searched & found following commands:

            parentWind.invalidate();

            parentWind.validate();

            parentWind.repaint();

            parentWind.pack();

            parentWind.setVisible(true);

but they are not doing anything. I also found on this forum to use window listener:

addWindowListener(new WindowAdapter() { 

                    @Override 

                    public void windowClosing(WindowEvent e) { 

                        parentWind.setTitle("Login = bob");

            parentWind.invalidate();

            parentWind.validate();

            parentWind.repaint();

            parentWind.pack();

            parentWind.setVisible(true);

                    } 

                }); 

but addWindowListener(..) requires an object to invoke. I tried this & parentWind obj but it says cant find symbol constructor().

My dialog box code is given below:

class LoginDialog extends JDialog implements ActionListener{

        JFrame parentWind;

        JTextField userName = new JTextField();

        JTextField password = new JTextField();

        JLabel label = new JLabel("LOGIN");

        //JLabel label1 = new JLabel("TraineeID");

        JLabel label2 = new JLabel("Password");

        JButton btn = new JButton("Login");

        LoginDialog(JFrame parent,String title){

            super(parent,title,false);

            setLayout(new FlowLayout());

           

        //setSize(300, 200);

            //JOptionPane.showMessageDialog(null,"Inside LoginDialog");

            setSize(800,700);

            userName.setPreferredSize(new Dimension(130,30));

            password.setPreferredSize(new Dimension(130,30));

            label.setPreferredSize(new Dimension(30,30));

            label2.setPreferredSize(new Dimension(70,80));

            parentWind = parent;

            add(userName);

            add(label);

            add(label2);

           

            add(password);

        

            add(btn);

            //setVisible(true);

            btn.addActionListener(this);

           

        }

        public void actionPerformed(ActionEvent e){

           

            parentWind.addWindowListener(new WindowAdapter() { 

                    @Override 

                    public void windowClosing(WindowEvent e) { 

                        parentWind.setTitle("Login = bob");

            parentWind.invalidate();

            parentWind.validate();

            parentWind.repaint();

            parentWind.pack();

            parentWind.setVisible(true);

                    } 

                }); 

                

           

          

        }

}

.

Somebody please guide me.

Zulfi.

Answers

  • morgalr
    morgalr Member Posts: 457
    edited Feb 28, 2017 9:09PM
    import java.awt.Dimension;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;public class Junk {  private void makeIt(){      JF1 f = new JF1();      JDialog d = new JOptionPane("Continue", JOptionPane.OK_OPTION).createDialog(f, "JD");      d.setVisible(true);      d.setModal(true);      try{        Thread.sleep(2000);      }catch(Exception e){        e.printStackTrace();      }      f.setTitle("JF2-Closed");      f.repaint();  }  public static void main(String[] args) {    new Junk().makeIt();  }  class JF1 extends JFrame{    public JF1(){      this.setName("JF1");      this.setTitle("JF1-A");      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      JPanel p = new JPanel();      p.setPreferredSize(new Dimension(400, 100));      this.add(p);      this.pack();      this.setVisible(true);      try{        Thread.sleep(2000);      }catch(Exception e){        e.printStackTrace();      }    }  }}
This discussion has been closed.