aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes tablesorter problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "tablesorter problem" Watch "tablesorter problem" New topic
Author

tablesorter problem

sajjad murtaza
Greenhorn

Joined: Aug 27, 2005
Posts: 4
Hi All,
i m working on a jtable which shows live stock quotes. the data is coming from a text file and i have the Obeserver running seperately, connected to my main applet class which notifies the applet whenever any updation occurs on the text file.

i've connected the TableSorter with my Tablemodel and set its tableheader and all in the init() method as usual but the problem arrises once i received a notification from the observer.

in my update method in the applet class, i first gets the data from the tablesorter (incase user have changed the view of the table based on any column) and then call my tablemodel to get the data from the file that has been updated........ then i sort my table model according to the data gathered from the tablesorter.........meaning if the file contains records of 3 companies (IBM,CMD,HSB)... and a user have clicked on the table column to sort it alphabetically, i want data to be sorted alphabetically in the model as well (CMD, HSB,IBM ).

Also, i have created a new CustomRenderer to show relevent colors according to the status of quotes. for that i call my CustomRenderer constructor with the TableSorter data gathered previously to update its private vector and check the incoming values with the values in the vector when getTableCellRendererComponent method is called.

it all works fine except whenever any update occurs on the text file, the jtable change its table header as it was previously which is annoying for a user.


Here is the code of my main applet class


public class DataFileTable extends JApplet implements Observer, ListSelectionListener {

protected JScrollPane scrollpane;

protected TableCellRenderer renderer;

protected Object[][] obje;

protected String dataFilePath;

protected TableSorter sorter;

protected JTable table;

protected DataFileTableModel model;

protected DataFileWatchdog wd;

protected Font f;

protected String[] columnNames;

protected JPanel panel;



public void init(){

columnNames = new String[16];

columnNames[0] = getParameter ("symbol_enabled");
columnNames[1] = getParameter ("company_enabled");
columnNames[2] = getParameter ("prev_closing_enabled");
columnNames[3] = getParameter ("offer_vol_enabled");
columnNames[4] = getParameter ("offer_price_enabled");
columnNames[5] = getParameter ("bid_price_enabled");
columnNames[6] = getParameter ("bid_vol_enabled");
columnNames[7] = getParameter ("cur_price_enabled");
columnNames[8] = getParameter ("traded_vol_enabled");
columnNames[9] = getParameter ("today_high_enabled");
columnNames[10] = getParameter ("today_low_enabled");
columnNames[11] = getParameter ("avg_price_enabled");
columnNames[12] = getParameter ("fifty2_weeks_high_enabled");
columnNames[13] = getParameter ("fifty2_weeks_low_enabled");
columnNames[14] = getParameter ("change_enabled");
columnNames[15] = getParameter ("sign_enabled");



dataFilePath = "http://localhost:8080/examples/newone/griddata.txt";

Container con = getContentPane();

con.setFont(f);

con.setLayout(new BorderLayout());

panel = new JPanel();

f = new Font("SanSerif",Font.PLAIN,12);

model = new DataFileTableModel(dataFilePath,columnNames);

sorter = new TableSorter(model); //ADDED THIS

table = new JTable(sorter);

sorter.setTableHeader(table.getTableHeader()); //ADDED THIS

table.setPreferredScrollableViewportSize(new Dimension(1000, 550));

//Set up tool tips for column headers.
table.getTableHeader().setToolTipText("Click to specify sorting; Control-Click to specify secondary sorting");

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

table.setFont(f);

renderer = new CustomTableCellRenderer2();



//the folowing code is meant to set a renderer which shows data in the same font color as i dont want color to be changed when user clicks on any column for sorting

sorter.addTableModelListener(new javax.swing.event.TableModelListener()
{
public void tableChanged(javax.swing.event.TableModelEvent e)
{

renderer = new CustomTableCellRenderer2();

try
{
table.setDefaultRenderer( Class.forName( "java.lang.Double" ), renderer );
}
catch( ClassNotFoundException ex )
{
System.exit( 0 );
}

}
});




scrollpane = new JScrollPane(table);

panel.add(scrollpane);

con.add(panel);

wd = new DataFileWatchdog(dataFilePath);

wd.addObserver(this);

}

public void update(Observable o, Object arg) {


obje=new Object[sorter.getRowCount()][sorter.getColumnCount()];

int i = 0;
int j=0;

for(i=0;i<sorter.getRowCount();i++){
for(j=0;j<sorter.getColumnCount();j++){
obje[i][j] = sorter.getValueAt(i,j);
}

}


int row=i;
int col=j;

CustomTableCellRenderer renderer1 = new CustomTableCellRenderer(obje,row,col);

try
{
table.setDefaultRenderer( Class.forName( "java.lang.Double" ), renderer1 );
}
catch( ClassNotFoundException ex )
{
System.exit( 0 );

}

sorter = new TableSorter(new DataFileTableModel(obje,row,col,dataFilePath)); //ADDED THIS

sorter.setTableHeader(table.getTableHeader());

table.setModel(sorter);



}



}




I badly need help as my boss i on me like hell.


Thanks in Advance
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

ps: edited to eliminate auto-boxing so this will compile in j2se 1.4
doesn't use generics but works okay in j2se 1.5
[ August 29, 2005: Message edited by: Craig Wood ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tablesorter problem