Hello Brian, (Or anyone who may wish to reply)
Thank you for taking the time to read my post and I have found your
Java Swing book to be very helpful! I feel priviledged!
My goal is to start with three rows in a table and delete the middle one, so only two show.
ie. showing only the first column in my numismatic table, I wish to delete the middle row.
Before
184O-0 ...
1849-O ...
1852-O ...
After (my goal)
184O-0 ...
1852-O ...
I actually see now
1840-O ...
blank
My TableModel is:
class MyTableModel extends AbstractTableModel {
private
String[] columnNames = {"Date",
"Denomination",
"Variety",
"Surviving Count",
"Summary"};
private HashMap theData;
MyTableModel()
{
theData = new HashMap();
}
public HashMap getTheData()
{
return theData;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return theData.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
Integer tmpKey= new Integer(row);
//tmpKey.append(row);
Entry value = (Entry) theData.get(tmpKey);
if(value == null)
return null;
if(col == 0)
{
return value.getDate();
}
else if(col == 1)
{
return value.getDenomination();
}
else if(col == 2)
{
return value.getVariety();
}
else if(col == 3)
{
return value.getSurvivingCount();
}
else if(col == 4)
{
return value.getSummary();
}
else
return null;
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
/*public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}*/
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
return true;
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
//StringBuffer tmpKey=new StringBuffer();
Integer tmpKey = new Integer(row);
//tmpKey.append(row);
Object tmpObject = theData.get(tmpKey);
if (tmpObject == null)
return;
Entry tmpEntry = (Entry) tmpObject;
if(col == 0)
{
tmpEntry.setDate((String) value);
}
else if(col == 1)
{
tmpEntry.setDenomination((String) value);
}
else if(col == 2)
{
tmpEntry.setVariety((String)value);
}
else if(col == 3)
{
tmpEntry.setSurvivingCount((Integer)value);
}
theData.put(tmpKey, tmpEntry);
fireTableCellUpdated(row, col);
return;
}
}
The table is in the following class (TheTable) with excerpts from it:
public class TheTable extends JPanel implements TableModelListener,ListSelectionListener{
private boolean DEBUG = false;
private MyTableModel model;
private AdminPanel adminPanel;
ListSelectionModel listSelectionModel;
int[] indices;
public TheTable() {
super(new GridLayout(2,1));
model = new MyTableModel();
adminPanel = new AdminPanel();
adminPanel.setModel(model);
adminPanel.setTable(this);
JTable table = new JTable(model);
model.addTableModelListener(this);
listSelectionModel = table.getSelectionModel();
listSelectionModel.addListSelectionListener(this);
table.setPreferredScrollableViewportSize(new Dimension(500, 140));
//table.setFillsViewportHeight(true);
//table.setAutoCreateRowSorter(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
add(adminPanel);
}
... (TheTable Class continued)
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
int firstIndex = e.getFirstIndex();
int lastIndex = e.getLastIndex();
boolean isAdjusting = e.getValueIsAdjusting();
JButton delete = adminPanel.getDeleteButton();
if (lsm.isSelectionEmpty()) {
if(delete.isEnabled())
{
delete.setEnabled(false);
}
;
} else {
// Enable Delete button if not enabled.
if(!delete.isEnabled())
{
delete.setEnabled(true);
}
// Find out which indexes are selected.
int minIndex = lsm.getMinSelectionIndex();
int maxIndex = lsm.getMaxSelectionIndex();
indices = new int[maxIndex - minIndex + 1];
int j = 0;
for (int i = minIndex; i <= maxIndex; i++) {
if (lsm.isSelectedIndex(i)) {
indices[j++] = i;
}
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TheTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TheTable newContentPane = new TheTable();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
Entry row1 = new Entry("1843-o","25 Cents",
"Large O", new Integer(80));
MyTableModel theModel = newContentPane.getModel();
HashMap map = theModel.getTheData();
map.put(new Integer(0), row1);
Entry row2 = new Entry("1849-o","25 Cents",
"Large O", new Integer(80));
map.put(new Integer(1), row2);
Entry row3 = new Entry("1852-o","25 Cents",
"Large O", new Integer(80));
map.put(new Integer(2), row3);
theModel.fireTableRowsInserted(0, 2);
//Display the window.
frame.pack();
frame.setVisible(true);
}
Another class has a listener which actually deletes the data:
public void actionPerformed(ActionEvent e)
{
HashMap map;
map = model.getTheData();
if (DELETE.equals(e.getActionCommand())) {
int[] indices = table.getIndices();
for(int i=indices.length-1;i >= 0;i--)
{
model.getTheData().remove(new Integer(indices[i]));
model.fireTableRowsDeleted(indices[i], indices[i]);
}
}
else if (ADD.equals(e.getActionCommand())) {
...
}
}
Sorry for all the code. Any suggestions would be greatly appreciated.
Thank you,
Ken