Re: customizing JTable
thufir wrote:
public class CarTableModel extends DefaultTableModel {
private static final TableModel INSTANCE = new CarTableModel();
public class CarTableData {
private static final CarTableData INSTANCE = new CarTableData();
Personally, these static variables weird me out. What if you need to
display two tables? Say the user is comparing two lists of cars, one
list from cars in rows 100-110 of the database (the cheap cars) and the
other table shows rows 340-350? How do you do that when there can only
be one table ever?
I think you should just get rid of the "INSTANCE" field in both cases,
and just call "new" on the class like a normal class. You should be
able to just make a table model and drop it into the JFrame's JTable,
kinda like this:
package jtabletest;
import java.math.BigDecimal;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Main {
public static void main(String[] args) {
Object[] headers = {"Car ID", "Name", "Vendor", "Type", "Year",
"Price", "Used"
};
Object[][] tableData = {{"1001", "Super-Zoom", "Nissan", "Sedan",
new Integer(2007), new BigDecimal("13000.00"),
new Boolean(true)
},
{"201", "Tough TX", "Ford", "Pick-up", new Integer(2008),
new BigDecimal("22000.99"), new Boolean(false)
},
{"555", "Prius", "Toyota", "Hybrid", new Integer(2008),
new BigDecimal("25000.00"), new Boolean(false) }
};
JTable table = new JTable( tableData, headers );
JFrame app = new JFrame( "Sampe JTable App" );
app.add( new JScrollPane( table ) );
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
app.pack();
app.setLocationRelativeTo( null );
app.setVisible( true );
}
}