I am wondering what your thoughts are on how I should go about this. I am thinking about using a bufferedReader to bring in the data into my program.
Here is a sample program:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableRowColumn extends JFrame
{
private final static String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
JTable table;
public DefaultTableModel model;
JPanel buttonPanel;
JButton button;
public TableRowColumn()
{
Object[][] data = {
{"9", "C"},
{"5", "A"},
{"3", "B"}
};
String[] columnNames = {
"Number",
"Letter"
};
model = new DefaultTableModel(data, columnNames);
//comment out this SortFilterModel line
final SortFilterModel sorter = new SortFilterModel(model);
//Change sorter to model
table = new JTable(sorter);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
// Add table and a Button panel to the frame
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
//
button = new JButton( "Add Row" );
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sorter.addRow( createRow() );
int row = table.getRowCount() - 1;
table.changeSelection(row, row, false, false);
table.requestFocusInWindow();
}
});
//Set up double click handler for column headers and sort
table.getTableHeader().addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event)
{
//check for click
if (event.getClickCount() < 2) return;
//find column of click and
int tableColumn = table.columnAtPoint(event.getPoint());
//translate to table model index and sort
int modelColumn = table.convertColumnIndexToModel(tableColumn);
sorter.sort(modelColumn);
}
});
}
private Object[] createRow()
{
Object[] newRow = new Object[2];
int row = table.getRowCount() + 1;
newRow[0] = Integer.toString( row );
newRow[1] = LETTERS.substring(row-1, row);
return newRow;
}
public static void main(String[] args)
{
TableRowColumn frame = new TableRowColumn();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
class SortFilterModel extends AbstractTableModel{
public SortFilterModel(DefaultTableModel m){
model = m;
rows = new Row[model.getRowCount()];
for (int i = 0; i < rows.length; i++) {
rows[i] = new Row();
rows.index = i;
}
}
public void sort(int c){
sortColumn = c;
Arrays.sort(rows);
fireTableDataChanged();
}
public Object getValueAt(int r, int c) {
return model.getValueAt(rows[r].index, c);
}
public boolean isCellEditable(int r, int c){
return model.isCellEditable(rows[r].index, c);
}
public void setValueAt(Object aValue, int r, int c){
model.setValueAt(aValue, rows[r].index, c);
}
public int getRowCount() {
return model.getRowCount();
}
public int getColumnCount(){
return model.getColumnCount();
}
public String getColumnName(int c) {
return model.getColumnName(c);
}
public Class getColumnClass(int c){
return model.getColumnClass(c);
}
private class Row implements Comparable {
public int index;
public int compareTo(Object other) {
Row otherRow = (Row)other;
Object a = model.getValueAt(index, sortColumn);
Object b = model.getValueAt(otherRow.index, sortColumn);
if (a instanceof Comparable) {
return ((Comparable)a).compareTo(b);
}
else{
return a.toString().compareTo(b.toString());
}
}
}
public void addRow(Object[] rowData) {
Row[] oldRows = new Row[rows.length];
for (int i=0; i<rows.length; i++) {
oldRows[i] = rows[i];
}
rows = new Row[rows.length +1];
for (int i=0; i >< oldRows.length; i++) {
rows[i] = oldRows[i];
}
rows[oldRows.length] = new Row();
rows[oldRows.length].index = oldRows.length;
addRow (rowData);
//model.addRow(rowData);
fireTableDataChanged();
}
private DefaultTableModel model;
private int sortColumn;
private Row[] rows;
}
------------------------------------------------------------------------------------
The data that I have in a CSV file needs to replace this part:
Object[][] data = {
{"9", "C"},
{"5", "A"},
{"3", "B"}
};
Any advice or pseudocode would be apprciated.
Thanks!