How do I refresh JTable data based off a JCombo selection?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Robert_Richards
Posted On:   Friday, April 4, 2003 03:48 PM

I would like to be able to refresh the data in a JTable based off a JCombo selection. The JCombo selection would be used as a parameter in the SQL string that builds the result set for the JTable. I am using a vector of vectors to populate my JTable using the AbstractTableModel. I mostly need to know what code needs to go in my action listener for my JCombo to get my JTable repopulated. I have not yet written the SQL statements to condition off the JCombo selection. I am at the early stages of this form creation and write now it is loading the JTable with all the records by default. I will build the conditional SQL later when I know how to call it based off the JCombo selection. So how would I call my dynamic SQL and then have the JTable refreshed based upon this n   More>>

I would like to be able to refresh the data in a JTable based off a JCombo selection. The JCombo selection would be used as a parameter in the SQL string that builds the result set for the JTable. I am using a vector of vectors to populate my JTable using the AbstractTableModel. I mostly need to know what code needs to go in my action listener for my JCombo to get my JTable repopulated. I have not yet written the SQL statements to condition off the JCombo selection. I am at the early stages of this form creation and write now it is loading the JTable with all the records by default. I will build the conditional SQL later when I know how to call it based off the JCombo selection. So how would I call my dynamic SQL and then have the JTable refreshed based upon this new SQL result set?



Here is some code I am working with (not in its entirety, but hopefully enough of the pieces to make sense):



//java core packages

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.util.*;



//java extension packages

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.table.*;



public class Running extends JFrame {



private JComboBox cboRunner, cboProgram;

private JTable tblRun;



// create GUI via constructor

public Running()

{

// call the superclass's constructor

super( "Running" );



//get content pane and set its layout

container = getContentPane();

layout = new GridBagLayout();

container.setLayout( layout );



//instantiate gridbag constraints

constraints = new GridBagConstraints();



constraints.anchor =GridBagConstraints.NORTHWEST;



cboRunner = new JComboBox();

cboProgram = new JComboBox();



CreateColumns();

getRowData();



tblRun = new JTable( new MyTableModel() );



addComponent( cboRunner, 1, 1, 1, 1 ); //component, row, column, gridwidth, gridheight

addComponent( cboProgram, 2, 1, 2, 1 );
//component, row, column, gridwidth, gridheight


constraints.ipady = 100;

addComponent( tblRun, 23, 0, 7, 3 ); //component, row, column, gridwidth, gridheight


JScrollPane scrollpane = new JScrollPane( tblRun );

addComponent( scrollpane, 21, 0, 7, 4 );



getRunner();

getProgram();



ActionEventHandler handler = new ActionEventHandler();



cboRunner.addActionListener( handler );



setSize( 800, 700 ); //width, height

setVisible( true );



} //end Running constructor



public static void main( String args[] )

{

Running application = new Running();



application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

} //end main method


private void addComponent( Component component, int row, int column, int width, int height )

{

constraints.gridx = column;

constraints.gridy = row;



constraints.gridwidth = width;

constraints.gridheight = height;



// set constraints and add component

layout.setConstraints( component, constraints );

container.add( component );

} //end method addComponent



private void getRunner()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String filename = "d:\java\running\running.mdb";

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";

database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end

// now we can get the connection from the DriverManager

Connection con = DriverManager.getConnection( database ,"","");



Statement s = con.createStatement();

s.execute("select distinct runner from tblrunner order by runner");

ResultSet rs = s.getResultSet();



cboRunner.addItem("[All]");


if (rs != null)

while(rs.next())

{

cboRunner.addItem(rs.getString(1));

}

s.close();

con.close();



} // end try


catch (Exception e)

{

System.out.println("Error: " + e);

}



} //end method getRunner



private void getProgram()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String filename = "d:\java\running\running.mdb";

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";

database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end

// now we can get the connection from the DriverManager

Connection con = DriverManager.getConnection( database ,"","");



Statement s = con.createStatement();

s.execute("select distinct program from tblprogram order by program");

ResultSet rs = s.getResultSet();



cboProgram.addItem("[All]");



if (rs != null)

while(rs.next())

{

cboProgram.addItem(rs.getString(1));

}

s.close();

con.close();



} // end try


catch (Exception e)

{

System.out.println("Error: " + e);

}



} //end method getRunner




public void CreateColumns()

{


columnNames = new Vector();

columnNames.removeAllElements();


columnNames.addElement( new String("Splits ID") );

columnNames.addElement( new String("Date") );

columnNames.addElement( new String("Runner") );

columnNames.addElement( new String("Program") );

columnNames.addElement( new String("Total Time") );

columnNames.addElement( new String("Total Distance") );

columnNames.addElement( new String("Total Laps") );



} //end CreateColumns


public void getRowData()

{



try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String filename = "d:\java\running\running.mdb";

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";

database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end

// now we can get the connection from the DriverManager

Connection con = DriverManager.getConnection( database ,"","");



Statement s = con.createStatement();



s.execute("select splits_id, run_date, runner, program, total_hours + ':' + total_min + ':' + total_sec, total_dist, total_laps from tblsplits order by run_date desc");

ResultSet rs = s.getResultSet();



final Vector colNames = new Vector();

rowData = new Vector();

rowData.removeAllElements();



final ResultSetMetaData rsMeta = rs.getMetaData();


for (int i = 1; i <= rsMeta.getColumnCount(); i++) {

colNames.addElement( rsMeta.getColumnName(i) );

}




if (rs != null)

while(rs.next())

{

final Vector row = new Vector();

for (int i = 1; i <= rsMeta.getColumnCount(); i++) {

row.addElement( rs.getObject(i) );

}

rowData.addElement( row );

}



s.close();

con.close();



} // end try


catch (Exception e)

{

System.out.println("Error: " + e);

}



} //end getRowData




class MyTableModel extends AbstractTableModel

{

public int getColumnCount() {

return columnNames.size();

}



public int getRowCount() {

return rowData.size();

}



public String getColumnName(int col) {

//return columnNames[col];

String colName = "";



if (col <= getColumnCount())

colName = (String)columnNames.elementAt(col);

//colName = (String)columnNames.elementAt(col);



return colName;

}



public Object getValueAt(int row, int col) {

return ((Vector) rowData.elementAt( row )).elementAt(col);

}

//System.out.println((row * getColumnCount()) + col);



} //end MyTableModel




private class ActionEventHandler implements ActionListener

{

public void actionPerformed( ActionEvent event )

{

if ( event.getSource() == cboRunner )

{

getRowData();

}

} //end actionPerformed

} //end ActionEventHandler



} //end class Running

   <<Less

Re: How do I refresh JTable data based off a JCombo selection?

Posted By:   Bahman_Barzideh  
Posted On:   Saturday, April 5, 2003 06:48 AM

There are two things you need to do in your actionPerformed ():

  • Repopulate your MyTableModel's data store with information form your database. I assume this is the rowData variable (although I don't see it being defined anywhere in you
    program). How you do this is up to you. You can either load the info
    into it by defining the setValueAt () method of your MyTableModel class and calling this method for each cell with new data or you can directly load the data into rowData in your actionPerformed () method. You can decide how to do this based on
    your own coding style and application needs.

  • After you have loaded the table's model with the new data, you need
    to force the table to repaint itself. Usually a call to table.invalidate () (or table.revalidate ()) followed by a call to table.repaint () should do the trick.
  • About | Sitemap | Contact