Updating JPanel content in a frame when a button is clicked in another frame

I created a frame x1 that has a p1 panel. When x1 loads, flags are added dynamically to p1. The number of flags added to p1 depends on the number of data values ​​in the database table (t1) that satisfy certain criteria.

In frame x1 there is a button b1. When b1 is clicked, it displays another x2 frame in which the data values ​​of the database table t1 can be changed. The "update" button in x2 updates t1 to reflect the changes made to its data values.

After the change, when x2 closes, I want the p1 panel in frame x1 to be updated automatically to reflect the changes made to the database, that is, the number of data values ​​that meet the criteria could change after modifying t1 to x2 and therefore the number The flags that will be displayed on p1 can also change.

How to update and reload p1 panel components in x1 from x2.

I wonder if anyone can help me fix the problem. Thanks in advance and sorry for loading the text into 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 } 
+4
source share
5 answers

When b1 is pressed, another x2 frame is displayed, in which the data values ​​of the t1 database table can be changed

The number of flags added to p1 depends on the amount of data values ​​in the database table (t1) that satisfy certain criteria.

make sure the current content from JPanel is deleted, then

 p1.revalidate(); p1.repaint(); 
  • will work, do not reinstall a new instance of JPanel , then you must add this Objcet to the JFrame , then you need to call validate() and repaint() in the JFrame (now I hope you do not extend this container JFrame

After the change, when x2 closes, I want the p1 panel in frame x1 to be updated automatically to reflect the changes made to i.e. the number of data values ​​that meet the criteria can change after t1 is modified to x2 and, therefore, the number of flags that will be displayed on p1 can also be changed.

  • use cardlayout instead

  • do not create two or more JDialog , use JDialog ,

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

+7
source

You usually call validate() and then repaint() when you want the user interface changes to actually display correctly.

+3
source

Try this link ...... This problem has already been resolved there ....

Transferring data from one frame to another (JAVA)

+1
source

You must pass frame f1 to frame f2

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

then write the close method

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

set the default close operation of the f2 frame

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

I hope that everything is in order.

0
source

One thing you can do is in ActionListener for button you can perform the following operations:

 //jp is a JPanel //remove the current JPanel frame.remove(jp); //do the changes you want to do //set sizes and border as before //again add the panel frame.add(jp); frame.revalidate(); frame.repaint(); 
0
source

Source: https://habr.com/ru/post/1446155/


All Articles