How to prevent adding duplicate data into Jtable.
This is my code.
try {
URL url = new URL("http://localhost:8080/webservice/rest/bdetails/get");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
String json = "";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
json += output;
}
conn.disconnect();
java.lang.reflect.Type listType = new TypeToken<ArrayList<BDetails>>() {
}.getType();
List<BDetails> bList = new Gson().fromJson(json, listType);
for( BDetails adr : bList)
{
DefaultTableModel model = (DefaultTableModel) pTable.getModel();
Vector<String> row = new Vector<String>();
row.add(detail.getUserName());
row.add(detail.getFirstName());
row.add(detail.getLastName());
row.add(detail.getAddress();
model.addRow( row );
}
} catch (IOException | RuntimeException ex) {
System.out.println(ex);
}
When I run this method It add data to the table. When I run again It add same data to the table. And again it do the same. How can I fix that? Can anybody help me? Thanks in advance. Below I have added the BDetails Class.
BDetails Class
public class BDetails
{
private String username;
private String firstName;
private String lastName;
private String address;
public BDetails() {
}
public BDetails(String username, String firstName, String lastName, String address) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
Setfirst, then use this as you bases for yourTableModel. You will probably need a POJO withequalsandhashCodesupport and make use of customTableModel(based onAbstractTableModel, but you'll get more control over how things get doneVectorto store the information, make a "Plain Old Java Object" to store the data. You can override theequalsandhashcodemethods (any decent IDE will guide you through how to make these work correctly) and then use aSetas the bases for your row information. TheSetwill prevent duplicates (based on thehashcodeof the objects)Object [ ]. But it didn't work. So can you give me a code snippet. Since I am usingWeb Services.BDetails?