JTable duplicate values in row
Here's a complete example that may prove helpful. As the sample Map
is unmodifiable, I refer you to @mKorbel's example on how to override isCellEditable()
and setValueAt()
.
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
/** @see https://stackoverflow.com/questions/9132987 */
public class EnvTableTest extends JPanel {
public EnvTableTest() {
this.setLayout(new GridLayout());
this.add(new JScrollPane(new JTable(new EnvDataModel())));
}
private static class EnvDataModel extends AbstractTableModel {
private Map<String, String> data = System.getenv();
private String[] keys;
public EnvDataModel() {
keys = data.keySet().toArray(new String[data.size()]);
}
@Override
public String getColumnName(int col) {
if (col == 0) {
return "Key";
} else {
return "Value";
}
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public Object getValueAt(int row, int col) {
if (col == 0) {
return keys[row];
} else {
return data.get(keys[row]);
}
}
}
private void display() {
JFrame f = new JFrame("EnvTableTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new EnvTableTest().display();
}
});
}
}
update values if duplicate in JTable
There are a lot of ways to do this. The best ones involve a clean separation between the data model (a list of stocks with amounts) and the table model (an adapter to express that in a way JTable understands).
Start with a List<StockHolding>
where StockHolding
is a super-simple class you make to hold the data for one row.
public class StockHolding {
private String product;
private int quantity;
}
(add in the constructor and getProduct/getQuantity)
Now when you enter a new product+amount...
addRequest(String product, int amount) {
StockHolding holding = findInList(product, myList);
if (holding == null) {
myList.add(new StockHolding(product, amount));
} else {
holding.setQuantity(amount+holding.getQuantity());
}
The key to making this work is to express your table model in terms of this list. getRowCount()
returns myList.size()
and the cell values are getValue(myList.get(rowNum), columnNum)
where getValue()
maps the column number to product or amount respectively.
This important lesson here is that isolating the express-as-a-table problem from the store-data problem make each problem easier to solve.
Create a way to stop to many duplicate values coming up in a JTable
Go through the TableModel counting the yes value from the last column. After that compare the counter with 8 and show the message if necessary.
Like this (is not tested)
public void checkCount(TableModel model) {
int count=0;
for (int row=0; row<model.getRowCount(); row++) {
if ("Yes".equals(model.getValueAt(row, 3)) {
count++;
}
}
if (count>8) {
JOptionPane.showMessageDialog(null, "Please remove a athlete from the team");
}
}
Prevent adding duplicate data into JTable in Java
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.
Related Topics
Android: How to Use Alarmmanager
Sqlite Getting Nearest Locations (With Latitude and Longitude)
Fast Bitmap Blur For Android Sdk
Showing Firebase Data in Listview
Working Post Multipart Request With Volley and Without Httpentity
Should I Actually Remove the Valueeventlistener
Json Array Iteration in Android/Java
Running Code in Main Thread from Another Thread
Concurrent Modification Exception: Adding to an Arraylist
Why Retrieving Google Directions For Android Using Kml Data Is Not Working Anymore
Base 64 Encode and Decode Example Code
Android/Java - Date Difference in Days
String Concatenation: Concat() VS "+" Operator