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 5 of 5

Thread: Action Center problem

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Action Center problem

    Hello i have this little program here, what i would like to happen is when i press the login button it makes that dialog dissapear and open a new frame. I can make that happen but i can't make the Dialog dissapear.
    could someone please say what i have to change i my code here so it will work?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;  
     
     
    public class Bank {
    public final JDialog dialog = new JDialog();
    	public static void main(String [] args)	{
    //	JDialog dialog = new JDialog();
    	dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
    	dialog.setLayout(new FlowLayout());
    	dialog.setSize(new Dimension(300, 159));
    	dialog.setTitle("Login");
    	//Button
    	JButton login = new JButton();
    	login.setText("Login");
     
    	//North
    	dialog.setLayout(new BorderLayout());
    	JPanel north = new JPanel(new GridLayout(3, 3));
    	north.add(new JLabel ("Username: "));
    	north.add(new JTextField());
    	north.add(new JLabel ("Password: "));
    	north.add(new JTextField());
    	north.add(new JButton("Opret"));
    	north.add(login);
    	login.addActionListener(new login());
     
     
    	dialog.add(north, BorderLayout.NORTH);
     
    	dialog.setVisible(true);
     
    	// action center
    		login.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    			dialog.setVisible(false);
    			}
    		});
     
    	}


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Action Center problem

    One way is to create your JFrame first before making the JDialog, pack() it so that it's rendered, but don't make it visible initially. Then create the JDialog so that it is subordinate to the JFrame (pass the JFrame into the JDialog's constructor). When you make the dialog invisible, make the JFrame visible. For example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Bank {
       public static void main(String[] args) {
          final JFrame frame = new JFrame("Frame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(new JLabel("My  Frame", SwingConstants.CENTER));
          frame.setPreferredSize(new Dimension(500, 400));
          frame.pack();
          frame.setLocationRelativeTo(null);
     
          final JDialog dialog = new JDialog(frame, "My Dialog", true);
     
          dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
          dialog.setLayout(new FlowLayout());
          dialog.setSize(new Dimension(300, 159));
          dialog.setTitle("Login");
          JButton login = new JButton();
          login.setText("Login");
     
          dialog.setLayout(new BorderLayout());
          JPanel north = new JPanel(new GridLayout(3, 3));
          north.add(new JLabel("Username: "));
          north.add(new JTextField());
          north.add(new JLabel("Password: "));
          north.add(new JTextField());
          north.add(new JButton("Opret"));
          north.add(login);
          dialog.add(north, BorderLayout.NORTH);
     
          // dialog.setVisible(true); // must add actionlistener first
     
          login.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
                frame.setVisible(true);
             }
          });
     
          dialog.setVisible(true);
       }
    }

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    lf2killer (November 22nd, 2012)

  4. #3
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Action Center problem

    Thanks for that
    but i have other objects which i change into when i press different buttons, would it be better if placed all them in my main and then called my method on how to do diffrent stuff from other classes?

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Action Center problem

    Quote Originally Posted by lf2killer View Post
    Thanks for that
    but i have other objects which i change into when i press different buttons, would it be better if placed all them in my main and then called my method on how to do diffrent stuff from other classes?
    No, the above example was just that, a very brief example of how this can be done. You should extract and use the general ideas obtained, but your regardless of what you do, your main method should be *very* small, and in fact do little more than set your program up and start it running and nothing more.

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Action Center problem

    okay
    but how do i remove the frame from the code you gave me bacause I tried to cut it own, but the code wont work after that?

Similar Threads

  1. Problem with Action Event
    By jayzee98 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: August 29th, 2012, 10:04 AM
  2. add action listener problem
    By mdhmdh100 in forum AWT / Java Swing
    Replies: 5
    Last Post: February 5th, 2012, 02:53 AM
  3. Problem with Animation and Button Action
    By ygeva in forum AWT / Java Swing
    Replies: 8
    Last Post: December 3rd, 2011, 01:57 PM
  4. button action problem
    By macko in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 10:51 AM
  5. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM