I'm having problems getting the JTable to display new data(either newly
inserted rows or an update of existing rows) from a TableModel.
I have created a very simple application which contains a TextBox, Button
and a JTable. The user just has to enter a value in the TextBox, click the
button, and the results should be displayed in the Table.
I have included the 3 simple (and small) classes involved in this
application.
Firstly, the class which describes the data I'm looking to display is:
class StockValues
{
.//member variables to be displayed on the table...
String stockName;
double price;
...//constructors...etc...
}
Secondly, the TableModel Class which retrieves and updates the data in the
TableModel. The core part of this
class is the updating of the TableModel using the fireTableRowsInserted &
fireTableRowsUpdated methods.
class MyTableModel extends AbstractTableModel
{
Vector data = new Vector();
String FirstColumn = "stock Name";
String SecondColumn = "Price";
String getColumnName (int col)
{
switch (col)
{
case 0:
return FirstColumn;
case 1:
return SecondColumn;
}
}
.//other methods such as
int getColumnCount() {...}
int getRowCount() {...}
Object getValueAt(int row, int col)
{
StockValues myStock = (StockValues)data.elementAt(row);
switch (col)
{
case 0:
return myStock.stockName;
case 1:
return new Double(myStock.price);
}
}
void updateStock(StockValues myStock)
{
String myStockName = myStock.stockName;
double myStockPrice = myStock.price;
.// I then have code which travseres through the data Vector
.// to see if myStockName already exists in the data Collection.
...
.// If myStockName already exists in the data Collection then I
call
fireTableRowsUpdated(row, col);
.//else if the myStockName is not found in the collection then it
needs to
.// be added to the collection which is done by the following line
of code
fireTableRowsInserted(row, col);
}
}//end of class
And Finally, the class which creates and displays all the UI components:
class UserInterfaceClient extends JFrame
{
...//creates button, menu, textbox...
...
MyTableModel tm = new MyTableModel();
...
JTable jt = new JTable(tm);
...
JScrollPane jsp = new JScrollPane(jt);
...
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.add(jsp);
...//other compontents...
Insets insets = contentPane.getInsets();
jsp.setBounds(25 + insets.left, 5 + insets.top, 750, 200);
this.setContentPane(contentPane);
this.pack();
this.setVisible(true);
...
}//end of class
I'm aware that I must include some kind of Listener and Events to complete
this functionality.
But as someone new to Java Swing where is this code to be added and what
exactly should
I be adding???
Your help is greatly appreciated!!!
Many Thanks,
E. Holmes
JTable is an automatic listener on its model. Looking at your code, your
model is notifying the view correctly. The problem could lie in your
button's event handler. Hope this helps.
>
> .//else if the myStockName is not found in the collection then it
> needs to
> .// be added to the collection which is done by the following line
> of code
> fireTableRowsInserted(row, col);
^^^^^^^^
The arguments to fireTableRowsUpdated/fireTableRowsInserted have to be
the first and the last *row index* that changed/were inserted.
Christian