Sunday, 24 July 2016

java - How to add external JPanel in JFrame?

enter image description here




I'm working on large scale program. As you can see I have one main JFrame and about 20 menu items on that. Each menu item must pop up a new window. At the beginning I have created a JLayeredPanel and then I assigned each menu item to one JPanel which is inside JFrame.Then I put 25 panel in JLayeredPanel... Default all the panels are set to invisible like:



panel1.setVisible(false);
panel2.setVisible(false);


so on



When user click on one menu item, its JPanel will be visible and rest are invisible. It looks messy and I have 5000 lines code. I used InternalFrame and TabbedPane but I'm not happy with them. I want to split my code in different JPanel classes and assign them to the main JFrame. I mean when user clicked on each menu item it will call the external JPanel and render it on the JPanel on the main JFrame. I am using design mode in netbeans and it does everything for me but the simpled structure is like this and it is not working:




 public class NewJPanel extends JPanel{
//I have added buttons and etc on this panel
......

}

public class frame extends JFrame(){

JPanel panel = new JPanel();
.....

Public frame(){
frame.add(panel);
}
......
//When use click on the any button on the panel
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//this is not working

NewJPanel fi = new NewJPanel ();
panel1.add(fi);


//or I tested this way separately but it did not work

panel1.remove();
panel1 = new NewJPanel();
add(panel);
invalidate();
}



}


please give me any suggestion how I can control this program in splited classes in professional way.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...