3

I'm creating a simple Task Manager, the issue I have is I want to open a new JFrame when I click the JButton. The window open's but the problem is multiple windows open. I have two classes App(Main) and NewTask. If someone could have a look at the code and see what I'm doing wrong that would be great, Sorry if I posted too much Code, Thanks in Advance.

App Class

    JButton btnNewTask = new JButton("New Task");
    btnNewTask.addActionListener(new ActionListener() 
{
     public void actionPerformed(ActionEvent e)
{
     JFrame frame = new JFrame ("New Task");
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     //frame.getContentPane().add (new NewTask());
     frame.pack();
     frame.setVisible (true);
}
});

btnNewTask.setBounds(10, 216, 116, 23);
contentPane.add(btnNewTask);

   NewTask Class

     /**
 * Launch the application.
 */
//public static void main(String[] args) 
{
    EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {
            try 
            {
                NewTask frame = new NewTask();
                frame.setVisible(true);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public NewTask() 
{

    setTitle("New Task");
    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);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1857403
  • 289
  • 5
  • 12
  • Sorry I've just noticed I left this line commented out //frame.getContentPane().add (new NewTask()); With this commented out it open a blank JFrame just once. – user1857403 Nov 27 '12 at 18:35
  • 3
    See this: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) rather use `JDialog` or [`CardLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Also do not use null/absolute Layout rather use an appropriate [`LayoutManger`](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html). And please post an [SSCCE](http://sscce.org) (specifically compilable code) – David Kroukamp Nov 27 '12 at 18:44

2 Answers2

1

Create new frame only once as a field of you ActionListener, and change it visibility with button.

  JFrame frame = null;

  public void actionPerformed(ActionEvent e) {
    if ( frame == null ) {
      frame = new NewTask();
      frame.pack();
    }
    frame.setVisible( !frame.isVisible() );
  }

But still rly bad idea, becouse of multiple frames =)

How to Make Dialogs

gmatagmis
  • 318
  • 2
  • 5
  • Thanks for the help, still having problems, tried the JDialog instead of JFrame. My Java skills are quiet poor but thanks anyway. – user1857403 Nov 28 '12 at 22:24
0

Creating multiple JFrames really ends up being bad practice in the end. You program ends up looking incoherent and the code just looks ugly in the end.

I would suggest using CardLayout. It's very easy to use and ends up being super convenient in the end. Also, if this is for school, I would definitely suggest CardLayout because Professors really don't like when you have specific functions call an opening of a new JFrame. ;)