View Single Post
Old 10-26-2012, 03:51 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Okay, using the swing JTable with the gui instead of the command line?
That's no problem to do, but I'd recommend not using Object[][] as your data handling. Instead, use the TableModel of the JTable as it will accept a collection of object.
The TableModel is easiest done with the DefaultTableModel (or you can write your own, but that's a more advanced feature). Its use is simply as such:
PHP Code:
JFrame frame = new JFrame("Commission Sample Table");
TableModel tmData = new DefaultTableModel();
JTable table = new JTable(tmData);
((
DefaultTableModel)tmData).addColumn("Total Sales");
((
DefaultTableModel)tmData).addColumn("Total Compensation");
                
frame.getContentPane().add(new JScrollPane(table));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(00600600);
frame.setVisible(true); 
Now, TableModel can be given new rows on the fly. This is still beneficial even if not responding to events; adding new records is simply accepting Object[] or vectors. For an example:
PHP Code:
    public static void insertCalculatedNumbers(DefaultTableModel tmdouble dStart)
    {
        for (
double d dStart<= dStart 1.5+= 5000)
        {
            
tm.addRow(new Object[]{d"Calculated Result Here"});
        }
    } 
Given a DefaultTableModel, this will increment dStart by 5000 until it hits 1.5x the size of dStart. It will insert a new row on each iteration. Calculated result is probably best done in another method instead of inline with this code.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Basic Chaos (10-28-2012)