Refresh/Recreate All Panel in a Card Layout each time any button is clicked in JFrame Holding these Panels

StackOverflow https://stackoverflow.com/questions/17637612

Question

I have a jFrame witch contains two panel Right Panel and LEFT PANEL.( Both are part of Split Panel)

Right Panel has a card layout.

p1=new AddPanel();
p2=new DeletePanel();
c2=new CardLayout(); 
right.setLayout(c2);
right.add(p1,"Add");
right.add(p2,"Delete");

In the left panel, there are 2 buttons (ADD, DELETE).

left.add(new JButton("Add");
left.add(new JButton("DELETE");

When i press ADD button- a new panel (AddPanel) shows in the LeftPanel .

 private void addActionPerformed(java.awt.event.ActionEvent evt) {                                    
   c2.show(right,"Add");

} 

AddPanel has various textfield and that writes the value to a database.

When i press DELETE button - a new panel (DeletePanel) shows in the LeftPanel.

private void deleteActionPerformed(java.awt.event.ActionEvent evt) {                                     
   c2.show(right,"Delete");
}                                    

DeletePanel also has various text fields that retireves all those values from database and there are three buttton :- next , previous ,remove.

User can navigate through mysql records ,up and down,using next,previous button as well as delete records by pressing REMOVE button and at run time only it gets updated ie the record deleted is not shown again as i again call SELECT statement at REMOVE Button call in DELETEPanel. This all is working.

Problem arises when i again click on ADD button , submit new record and now try to view records by pressing DELETE Button in LEFT PANEL of JFRAME.

How do i make it so each time i press 'DELETE' button (from left panel in jFrame), DeletePanel is created again so that it gets updated result from database.

problem i am facing is since DELETE Button was called previously , therefore DELETE PANEL WAS created before and thus it maintain its OLD STATE. How to RE INITIALIZE DELETE PANEL. I dont get new updated record from database. Only works if i again restart application.
Please help.

Was it helpful?

Solution

FInally found answer and posting Response here ANSWER IS TO USE COMPONENT LISTENER to re-initialize a panel each time it is viewed.....

public class DeletePanel implements ComponentListener

and then inorder to add panel ie DELETEPANEL to a listnener, which i require in my question, use this line.

this.addComponentListner(this); // in deletepanel construtor.

then define overridden Component Shown method to initialize things. EX- like in my case select statement.

@Override
public void componentShown(ComponentEvent e) {
     try {
        Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online", "root","");
        st = con.prepareStatement("select question,op1,op2,op3,op4 from java");
        rs = st.executeQuery();
    } catch (ClassNotFoundException | SQLException ex) {
  }
}

This Works 100%

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top