Reputation:
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;
}
Upvotes: 0
Views: 1263
Reputation: 2436
You can try this,
java.lang.reflect.Type listType = new TypeToken<ArrayList<BDetails>>() {
}.getType();
List<BDetails> bList = new Gson().fromJson(json, listType);
DefaultTableModel model = (DefaultTableModel) pTable.getModel();
model.setRowCount(0);
for( BDetails adr : bList)
{
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 );
}
model.setRowCount(0)
will clear all the rows before inserting data. It worked for me.
Upvotes: 2
Reputation: 347214
First, I took BDetails
and used Netbeans to add the equals
and hashcode
methods. This is important, as it provides away to assess if two instances of an object are the same
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;
}
@Override
public int hashCode() {
int hash = 7;
hash = 13 * hash + Objects.hashCode(this.username);
hash = 13 * hash + Objects.hashCode(this.firstName);
hash = 13 * hash + Objects.hashCode(this.lastName);
hash = 13 * hash + Objects.hashCode(this.address);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final BDetails other = (BDetails) obj;
if (!Objects.equals(this.username, other.username)) {
return false;
}
if (!Objects.equals(this.firstName, other.firstName)) {
return false;
}
if (!Objects.equals(this.lastName, other.lastName)) {
return false;
}
if (!Objects.equals(this.address, other.address)) {
return false;
}
return true;
}
}
I then create a custom TableModel
(I'm bias, but I don't have much love for DefaultTableModel
and it makes the next step easier)
public class BDetailsTableModel extends AbstractTableModel {
private List<BDetails> rows;
private String[] columnNames = {"User name", "First name", "Last Name", "Address"};
public BDetailsTableModel(List<BDetails> rows) {
this.rows = rows;
}
@Override
public int getRowCount() {
return rows.size();
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
BDetails details = rows.get(rowIndex);
switch (columnIndex) {
case 0:
return details.getUsername();
case 1:
return details.getFirstName();
case 2:
return details.getLastName();
case 3:
return details.getAddress();
}
return null;
}
}
And finally...
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);
//}
Set<BDetails> unquie = new HashSet<>(bList);
List<BDetails> rows = new ArrayList<>(unquie);
BDetailsTableModel model = new BDetailsTableModel(rows);
pTable.setModel(model);
Okay, all this does is adds the bList
to Set
, this will remove all duplicates from the list for us (using the hashcode
of the objects), add the results to a ArrayList
(because it has a get
method) and then supplies that to our BDetailsTableModel
Now, if you don't want to replace the TableModel
each time, but instead, add the results it becomes a little more difficult, as you need to manage the possibility that the new set of data might contain duplicates of the old set.
You could add the following to BDetailsTableModel
...
public void add(BDetails details) {
int rowCount = getRowCount();
if (rows.contains(details)) {
return;
}
rows.add(details);
fireTableRowsInserted(rowCount, rowCount);
}
public void addAll(List<BDetails> newRows) {
Set<BDetails> rows = new HashSet<>(newRows);
addAll(rows);
}
public void addAll(Set<BDetails> newRows) {
Set<BDetails> allRows = new HashSet<>(rows);
allRows.addAll(newRows);
rows = new ArrayList<>(allRows);
fireTableDataChanged();
}
and instead of creating a new instance of BDetailsTableModel
, simply use the existing instance (from pTable
) and use the above functionality to add new rows
Upvotes: 1