Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Button which suppose to hide JDialog..

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Button which suppose to hide JDialog..

    Hello everyone!

    I've got a problem. I'm writing a simple Jabber communicator in Java for my college project. Currently I'm dealing with GUI in Swing and i have this problem. To describe i show you the conditions in which my program runs:

    I've got the main frame which is loading and shows up the JDialog (setVisible(true)) which is logging in dialog. In login dialog I've got two buttons: "Log in" and "Exit", when i click "Log in" my JDialog should disappear (setVisible(false), also tried dispose()) but it won't happen this way. When i'm clicking "Log In" in my JDialog, the Dialog is hiding but then the whole JDialog is showing once again and then finally i can close dialog but clicking the button.. I dont know why this is happening. heres my code in fragments:

    The one where I'm calling the JDialog constructor(in MainWindow constructor)
    class MainWindow extends JFrame {      
    ...
    private LoginDialog login;
     
            public MainWindow() {
     
                    ...
     
                    if(login == null)
                            login = new LoginDialog(this);
                    login.setVisible(true);
            }
    }
    The JDialog code when I'm setting JButton action
    class LoginDialog extends JDialog {
    ...
         public LoginDialog() {
         ...
                  button.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                    LoginDialog.this.setVisible(false);
                            }
                    });
     
          }
    }

    The whole code is available here: https://bitbucket.org/dardam/miracle...cf5e45f6b/src/
    Last edited by dardam; April 7th, 2012 at 08:23 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,638
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Button which suppose to hide JDialog..

    Can you post a small complete program that compiles, executes and shows the problem (a SSCE). Not the full project which probably has a lot of code not related to the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Button which suppose to hide JDialog..

    Quote Originally Posted by Norm View Post
    Can you post a small complete program that compiles, executes and shows the problem (a SSCE). Not the full project which probably has a lot of code not related to the problem.
    Sure, so here comes a code

    Main Frame
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class Main extends JFrame {
     
    	SampleDialog dialog;
     
    	public Main() {
    		setTitle("Sample");
     
    		setSize(300, 300);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
     
    		if(dialog == null)
    		 	dialog = new SampleDialog(this);
    		dialog.setVisible(true);
     
    	}
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				Main main = new Main();
    			}
    		});
    	}

    Dialog Frame
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class SampleDialog extends JDialog {
     
    	public SampleDialog(Main owner) {
    		super(owner,true);
     
    		setTitle("Sample Dialog");
     
    		JButton button = new JButton("Hide");
    		button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				setVisible(false);
    			}
    		});
     
    		add(button);
     
    		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
     
    		pack();
    		setResizable(false);
    		setVisible(true);
     
    	}
    }

    So my problem is with the "Hide" button, it doesnt work properly. Please help me fix that issue.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,638
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Button which suppose to hide JDialog..

    Add this line of code after where you create the SampleDialog and before the call to setVisible.
         System.out.println("back from dialog");
    Observe when it is executed and prints out.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Radio button hide based on input..please help guys
    By ravi9999 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 7th, 2012, 06:24 AM
  2. JDialog from JFrame button (GUI)
    By Jameseyboy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2012, 07:55 AM
  3. [SOLVED] Click button to open JDialog
    By susieferrari in forum Java Theory & Questions
    Replies: 6
    Last Post: March 30th, 2011, 09:24 AM
  4. Why does it get smaller when it's suppose to get bigger
    By velvetymold in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2011, 11:34 PM
  5. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM