Search code examples
javaclassuser-interfacejbuttonioexception

Jumping to another class using a JButton


I have the below code where I made a simple GUI. I would like Button2 to navigate to class 'Project2', which should start another piece of code. Just to note, in its current state, 'Project2' has no GUI, though I intend to add one soon. Anyway, this 'code jump' which I used by adding: String[] args = {}; Project2.main(args); is not working, as the IDE says 'IOException must be caught or thrown'. I know how this works, though I am not sure how to implement it in the program.

Thanks in advance!


Solution

  • In most of the IDE's, when you right-click on the Button2 in the Design(GUI) pane, you can travel through:

    Events -> Actions -> actionPerformed().

    And write this code in the selected method to switch classes:

    this.setVisible(false); //turns off the visibility of the current class
    outputClass out = new outputClass(); //creating the object of the class you want to redirect to
    out.setVisible(true);//turns on the visibility of the class you want to redirect to