1

So I'm currently doing a project for a class at my college and I've hit a problem. I'm only a sophomore so I'm not far in enough to know advanced methods or anything, but right now we're building a program that displays a window with 2 buttons. 1 button launches a loan calculator, and the other launches a 401k calculator. Ive made the programs for both calculators as separate classes/.java files and need to make a third, main file that when you click a button it opens one of the two windows. How do you launch a new window from an existing one with a button?

Each class has a start and main method, is that the problem? Each of the three files launches on there own and displays the window properly, but I can't tie the two calculators to the initial 2 button choice window.

  • Are you looking for Swing answers or JavaFX? Are your views done with FXML? – purring pigeon Oct 20 '17 at 21:07
  • Sad to say I don't know what FXML is, thanks for the answers guys. I'll remove the idea of windows and have the whole thing be through the terminal like we regularly do, I just wanted to challenge myself with making pop up window menu versions. I was looking for javaFX as I didn't choose to try swing but I'll take swing answers since I am planning on giving that a shot soon. – INeedHealing Oct 21 '17 at 17:05

1 Answers1

0

You'll have one base class with a single Main method, and it will instantiate the other classes. You would add your buttons to this window, and the buttons will launch the other windows. See the comments:

public class Main {

    public static void main(String[] args) {

        //Main window and panel
        JFrame mainWindow = new JFrame();
        JPanel mainPanel = new JPanel();

        //The calculator classes
        FourZeroOneCalc fourCalc = new FourZeroOneCalc();
        LoanCalc lc = new LoanCalc();

        //
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add a button for each and a listener on each button that opens the calculator windows
        JButton launchFourCalc = new JButton("401k Calculator");
        launchFourCalc.addActionListener(e->{
            fourCalc.setVisible(true);
        });
        JButton launchLoanCalc = new JButton("Loan Calculator");
        launchLoanCalc.addActionListener(e->{
            lc.setVisible(true);
        });

        //Add everything to the main window and show it
        mainPanel.add(launchLoanCalc);
        mainPanel.add(launchFourCalc);
        mainWindow.add(mainPanel);
        mainWindow.pack();
        mainWindow.setVisible(true);

    }

}

Loan calculator and 401k calculator would be like this:

//This class would be the same as your existing class but it extends JFrame so it can be opend as a window
public class FourZeroOneCalc extends JFrame{
    private static final long serialVersionUID = 1L;
    //constructor has to handle the UI stuff, and anything else your existing class does
    public FourZeroOneCalc(){
        //UI stuff that you probably already had in your existing program
        JPanel panel = new JPanel();
        JLabel l = new JLabel("401k Calcluator");
        panel.add(l);
        this.add(panel);
        this.pack();

    }
    //Additional methods etc. that your existing class has
}
Alex
  • 827
  • 8
  • 18
  • The question is marked JavaFX, but this is a Swing based solution, so it will probably just confuse the asker. Perfectly nice solution for Swing though :-) – jewelsea Oct 20 '17 at 21:48
  • You're right, My mistake! I didn't notice the tag for JavaFX, just made an assumption that the OP was in an intro course which tend to use swing. I need to read more attentively! – Alex Oct 23 '17 at 04:13