Paramone
Paramone

Reputation: 2724

Change the JFrames' panel from another JPanel class

so I got this in my Main class:

public class Main extends JFrame {

public static void main(String[] args) {       
    JFrame Launch = new JFrame();
    Launch.setSize(800, 400);
    Launch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Launch.setContentPane(new StartView());
    Launch.setTitle("De Hartige Hap");
    Launch.setVisible(true);        
    }
}

Now let's say I'm in that Panel ("StartView()")

and onClick on a button, I want to change the frames' contentpane..

How would I do this?

public class StartView extends javax.swing.JPanel {
public StartView() {
    initComponents();
}



private void OrderButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
   /*instead of Launch.setContentPane(new StartView());
    *it has to be (new otherView())
    */
}

Upvotes: 0

Views: 103

Answers (1)

RJ Pijpker
RJ Pijpker

Reputation: 76

Pass your Launch object to your panel object (i.e. new StartView(launch)). This way, you can create a method changeView() in Launch and you can call this method from your panel (launch.changeView()), you can change your view inside that method.

Also, if I may adivce you, take a look at the ModelViewController pattern. This makes sure you keep the View (your panels) and the Controller (your Frame) seperated so you don't get issues like this.

Upvotes: 5

Related Questions