Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update content of JPanel on a frame on button click in another frame

I have created a frame x1 which has a panel p1. When x1 is loaded, checkboxes are added dynamically to p1. The number of checkboxes added to p1 depend on the number of data values in the database table(t1) that satisfy a particular criteria.

There is a button b1 on frame x1. When b1 is clicked,it displays another frame x2 in which data values of the database table t1 can be modified. A button 'update' in x2,updates t1 to reflect the changes made to its data values.

After modification,when x2 is closed I want the panel p1 in frame x1 to be updated automatically to reflect the changes made to the database ie the number of data values satisfying the criteria might have changed after modifictaion of t1 in x2 and hence,the number of check boxes to be displayed on p1 might have changed as well.

How do I refresh and reload the components of panel p1 in x1 from x2.

Was wondering if anyone could help me fix the problem. Thanks in advance and sorry about the load of text in the question.

    In x2:

       private void UPDATEActionPerformed(java.awt.event.ActionEvent evt)
         {
        //x1 is an object of ParentFrame

        ParentFrame f1=new ParentFrame();
              f1.fillPanel();
              //fillpanel()fills p1 with checkboxes after running validate() and repaint() on it


             }
like image 620
user1748910 Avatar asked Nov 15 '12 07:11

user1748910


People also ask

How do I change JPanel in JFrame?

Just call the method pack() after setting the ContentPane , ( java 1.7 , maybe older) like this: JFrame frame = new JFrame(); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); .... frame. setContentPane(panel1); frame.

How many Jpanels can be placed in another JPanel?

D. Yes, but only one JPanel may be placed inside another.

Can you add ActionListener to JPanel?

First off as @Sage mention in his comment a JPanel is rather a container than a component which do action. So you can't attach an ActionListener to a JPanel .


3 Answers

When b1 is clicked,it displays another frame x2 in which data values of the database table t1 can be modified

  • use CardLayout instead

The number of checkboxes added to p1 depend on the number of data values in the database table(t1) that satisfy a particular criteria.

be sure that current contents from JPanel is removed, then

p1.revalidate();
p1.repaint();
  • will be works, don't to re_create a new instance of JPanel, then you have to add this Objcet to the JFrame, then have to call validate() & repaint() to JFrame (now I hope that you doean't extends this container :-)

After modification,when x2 is closed I want the panel p1 in frame x1 to be updated automatically to reflect the changes made to the database ie the number of data values satisfying the criteria might have changed after modifictaion of t1 in x2 and hence,the number of check boxes to be displayed on p1 might have changed as well.

  • use CardLayout instead

  • don't to create two or more JFrames, use JDialog instead,

  • create JDialog (setDefaultCloseOperation(JDialog.CLOSE...)) only one instance, reuse this container for another action from parent

like image 182
mKorbel Avatar answered Oct 06 '22 01:10

mKorbel


Usually you call a validate() and then a repaint() when you want changes to the UI to actually be visible correctly.

like image 42
Dan D. Avatar answered Oct 06 '22 02:10

Dan D.


You should pass frame f1 to frame f2

JFrame Frame;
public f1(JFrame f){
    frame=f;
}

then, write a close method

int closeFrame(){
    f.fillPanel();
    f.show();
    return JFrame.HIDE_ON_CLOSE;
}

set default close operation of frame f2

public f1(JFrame f){
    frame=f;
    this.setDefaultCloseOperation(closeFrame());
}

Hope it's ok.

like image 38
Guest Coder Avatar answered Oct 06 '22 00:10

Guest Coder