1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| package swing.tests;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class MyModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 7520116325451560601L;
public static final int UNIT_PRICE_INDEX = 0;
public static final int AMOUNT_INDEX = 1;
public static final int TOTAL_PRICE_INDEX = 2;
private List<PresentationItem> data;
private List<String> columnNames;
public MyModel() {
String[] colNamesTab = {"Prix unitaire","Nombre","Prix total"};
columnNames = Arrays.asList(colNamesTab);
data = new ArrayList<PresentationItem>(5);
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public String getColumnName(int column) {
return columnNames.get(column);
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if(columnIndex == UNIT_PRICE_INDEX) {
return BigDecimal.class;
} else if (columnIndex == AMOUNT_INDEX) {
return BigInteger.class;
} else if(columnIndex == TOTAL_PRICE_INDEX) {
return BigDecimal.class;
}
return Object.class;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
PresentationItem item = data.get(rowIndex);
if(value != null) {
if(columnIndex == UNIT_PRICE_INDEX) {
BigDecimal price = (BigDecimal) value;
} else if (columnIndex == AMOUNT_INDEX) {
if(value != null) {
BigInteger amount = null;
amount = (BigInteger) value;
item.setItemNumber(amount);
}
}
}
fireTableRowsUpdated(rowIndex, rowIndex);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if ( columnIndex == AMOUNT_INDEX) {
return true;
}
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
PresentationItem item = data.get(rowIndex);
if(columnIndex == UNIT_PRICE_INDEX) {
//
return item.getItemPrice();
} else if (columnIndex == AMOUNT_INDEX) {
return item.getItemNumber();
} else if(columnIndex == TOTAL_PRICE_INDEX) {
return item.getTotalPrice();
}
return null;
}
public void addRow(PresentationItem item) {
data.add(item);
fireTableRowsInserted(data.size()-1, data.size()-1);
}
public void insertRow(PresentationItem item,int rowIndex) {
data.add(rowIndex, item);
fireTableRowsInserted(rowIndex, rowIndex);
}
public void removeRow(int rowIndex) {
data.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);
}
public static void main(String[] args) {
PresentationItem item =new PresentationItem();
item.setItemPrice(new BigDecimal("123.44"));
item.setItemNumber(new BigInteger("5"));
PresentationItem item2 =new PresentationItem();
item2.setItemPrice(new BigDecimal("11.13"));
item2.setItemNumber(new BigInteger("3"));
MyModel model = new MyModel();
model.addRow(item);
model.addRow(item2);
JTable t = new JTable(model);
JFrame f = new JFrame("Hekk");
f.setSize(400, 300);
f.add(new JScrollPane(t));
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
} |
Partager