Gowtham Ravichandran
Gowtham Ravichandran

Reputation: 15

Replace a JPanel with the same with updated contents

Here I have a JPanel with JLabels and JTextfields and some JButtons. Where one JTextfield is to get data from user and on clicking search button, it should get the relevant data from the database and display the result in the same panel. When I click submit button. I find no change in my screen, but when I resize the frame I can see the updated panel behind the previous one. Even I try remove(rp) and then add it results the same as above.

How to replace a JPanel with the same with updated contents?

P.S: I want to remove and add the same panel with updated contents

my code looks like this while replacing

private void rp_validate(){
     f.add(rp);
     f.revalidate();
     f.repaint();
     f.pack();
}

Upvotes: 0

Views: 131

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

  1. Create a class that holds all the necessary data obtained from your Database query.
  2. Give the GUI class that holds your JPanel a public "setter" method that accepts an instance of the above class.
  3. Then inside of the JPanel class, call setText(...) passing information held in the setter method parameter to update the text displayed in your JPanel. Simple as pie.

Fore example:

public void setMyData(MyData myData) {
   this.myData = myData;
   lastNameTextField.setText(myData.getLastName());
   firstNameTextField.setText(myData.getFirstName());
   addressTextfield.setText(myData.getAddress());
   cityTextfield.setText(myData.getCity());
   stateTextfield.setText(myData.getState());
   // ... etc
}

Upvotes: 1

Related Questions