0

I'm trying to open DebtForm.java when the btnAddNewDebt button is pressed from IOUApplication.java. Once the button is pressed the IOUApplication.java window should close and the DebtForm.java window should open.

I've managed to open the DebtForm.java when the btnAddNewDebt button is pressed, yet I'm not able to close the IOUApplication.java window.

I've tried using the following:

public void close(){
    WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOS­ING);
    Toolkit.getDefaultToolkit().getSystemEve­ntQueue().postEvent(winClosingEvent);
}

Yet I'm not sure where to place the code or if there are any other alternate methods to close the window.

Here's a snippet of IOUApplication.java for context:

public void initialize() {

    frame = new JFrame();
    frame.getContentPane().setBackground(Color.DARK_GRAY);
    frame.setBounds(100, 100, 450, 132);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnAddNewDebt = new JButton("Add new debt");
    btnAddNewDebt.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            DebtForm frame = new DebtForm();
            frame.setVisible(true);
        }
    });

    btnAddNewDebt.setBounds(81, 18, 117, 29);
    frame.getContentPane().add(btnAddNewDebt);      

    JButton btnPersonalDebt = new JButton("Personal Debt");
    btnPersonalDebt.setBounds(266, 18, 117, 29);
    frame.getContentPane().add(btnPersonalDebt);

    JLabel lblWrittenAndCoded = new JLabel("Written and coded by Samuel Kahessay");
    lblWrittenAndCoded.setForeground(Color.WHITE);
    lblWrittenAndCoded.setBounds(108, 88, 252, 16);
    frame.getContentPane().add(lblWrittenAndCoded);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 23 '16 at 10:50
  • What's wrong with [`Window#dispose`](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#dispose()) or [`Window#setVisible`](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setVisible(boolean))? Maybe have a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) and for an alternative have a look at [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – MadProgrammer Mar 23 '16 at 10:51
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Mar 23 '16 at 10:52

1 Answers1

0

In order to close the JFrame, all you need to do is call frame.dispose(). This method will completely get rid of the JFrame in memory. If you want to later re-open the window, all you need to do is frame.setVisible(false) and then frame.setVisible(true) when you want to reopen it. You might have to make your JFrame a global variable in order to do this. Assuming that the code you wrote for DebtForm() works to create it's own JFrame, here is what it looks like:

public void initialize() {

/* Your same code */
frame = new JFrame();
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setBounds(100, 100, 450, 132);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JButton btnAddNewDebt = new JButton("Add new debt");
btnAddNewDebt.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        DebtForm debtForm = new DebtForm();
        debtForm.setVisible(true);
        /* THE ONLY NEW LINE OF CODE */
        frame.setVisible(false); //This will make the first window disapear.
        /* ------------------------- */
    }
});

btnAddNewDebt.setBounds(81, 18, 117, 29);
frame.getContentPane().add(btnAddNewDebt);      

JButton btnPersonalDebt = new JButton("Personal Debt");
btnPersonalDebt.setBounds(266, 18, 117, 29);
frame.getContentPane().add(btnPersonalDebt);

JLabel lblWrittenAndCoded = new JLabel("Written and coded by Samuel Kahessay");
lblWrittenAndCoded.setForeground(Color.WHITE);
lblWrittenAndCoded.setBounds(108, 88, 252, 16);
frame.getContentPane().add(lblWrittenAndCoded);
}

Later, in order to make the original window reappear, outside of your initialize() method you will need to declare "frame" like this:

public static JFrame frame = new JFrame();

Now in your DebtForm.java, you can make the frame visible again like this:

IOUApplication.frame.setVisible(true);

I hope this helps. Thanks for the read!