I seem to have a problem refreshing the data within my JTable. I know the code is being run through.
I have a JTable which is populated from a selection in a combo box, on first click within the combo box the table displays the correct data.
So basically the data within the JTable doesnt refresh. :(
So please help me to use the current JTable to display different query results on it.
The whole code that Im using is :
package mypackage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class Bes extends JFrame {
private ResultSetTableModel tableModel; // class definition presented at the end of the code
private JTabbedPane tabbedpane = new JTabbedPane();
private JComboBox comboBox = new JComboBox();
private JComboBox comboBox1 = new JComboBox();
private JLabel label = new JLabel("Select the requested item from comboboxes.");
private JPanel panel = new JPanel();
public Bes() {
initUI();
}
public void conn(String driver,String dbName,String url,String query)
{
// *********************************************************************************************************
// create ResultSetTableModel and display database table
try {
// create TableModel for results of query
tableModel = new ResultSetTableModel( driver, url, query );
JTable resultTable = new JTable( tableModel );
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Jtable <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
panel.setLayout(null);
panel.add( new JScrollPane( resultTable ),BorderLayout.NORTH);
resultTable.setFillsViewportHeight(true);
resultTable.setBounds(60,200,400,50);
//Box box = Box.createHorizontalBox();
panel.add(resultTable);
}
catch ( ClassNotFoundException classNotFound ) {
JOptionPane.showMessageDialog( null,
"Clientdriver bulunamadı", "Driver bulunamadı",
JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null,
sqlException.toString(),
"Veritabanı hatası", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
driver = ""; // i tried to reset conn fnc' parameters
dbName=" "; // before calling again
url =" ";
query = " ";
//**********************************************************************************************************************
}
public void initUI() {
panel.setLayout(null);
panel.setBackground(Color.white);
comboBox.addItem(" Vitamin ");
comboBox.addItem("Vitamin_A_retinol");
comboBox.addItem("Vitamin_B1_thiamin");
comboBox.addItem("Vitamin_B2_riboflavin");
comboBox.addItem("Niasin");
comboBox.addItem("Vitamin B5 (pantotenik asit)");
comboBox.addItem("Vitamin B6");
comboBox.addItem("Vitamin B12");
comboBox.addItem("Vitamin C");
comboBox1.addItem("Mineral ");
comboBox1.addItem("Bakır");
comboBox1.addItem("Cinko");
comboBox1.addItem("Demir");
comboBox1.addItem("Florit");
comboBox1.addItem("Folat");
comboBox1.addItem("Fosfor");
comboBox1.addItem("İyot");
comboBox1.addItem("Kalsiyum");
panel.add(comboBox);
panel.add(comboBox1);
panel.add(label);
comboBox.setBounds(60, 60, 190, 30);
comboBox1.setBounds(300, 60, 150, 30);
label.setBounds(60, 15, 300, 30);
tabbedpane.setBounds(0, 200, 300, 100);
getContentPane().add(tabbedpane, BorderLayout.CENTER);
tabbedpane.addTab(" Vitamin / Mineral ", panel);
JButton quitButton = new JButton("ex-it");
quitButton.setBounds(60, 560, 80, 30);
ActionListener actionListener = new ActionListener() {
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == comboBox)
{
String v = (String)comboBox.getSelectedItem();
System.out.println(v);
String driver = "org.apache.derby.jdbc.ClientDriver";
// URL to connect to books database
String dbName="seconddb";
String url ="jdbc:derby://localhost:1527/" + dbName +";create=true";
// query to select entire authors table
String query = "SELECT FOOD_NAME,"+v+" FROM FOODTABLET ";
conn(driver,dbName,url,query);
} // END İF1
if (e.getSource () == comboBox1)
{ String v = (String)comboBox1.getSelectedItem();
System.out.println(v);
}
}
};
comboBox.addActionListener(actionListener);
comboBox1.addActionListener(actionListener);
//
quitButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
//
panel.add(quitButton);
setTitle("Demo1");
setSize(600, 720);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} // end initUI
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Bes ex = new Bes(); //
ex.setVisible(true); //
}
});
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ResultSetTableModel tableModel; // related class definition :
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import java.sql.*;
import java.util.*;
// Java extension packages
import javax.swing.table.*;
// ResultSet rows and columns are counted from 1 and JTable
// rows and columns are counted from 0. When processing
// ResultSet rows or columns for use in a JTable, it is
// necessary to add 1 to the row or column number to manipulate
// the appropriate ResultSet column (i.e., JTable column 0 is
// ResultSet column 1 and JTable row 0 is ResultSet row 1).
public class ResultSetTableModel extends AbstractTableModel {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
// initialize resultSet and obtain its meta data object;
// determine number of rows
public ResultSetTableModel( String driver, String url,
String query ) throws SQLException, ClassNotFoundException
{
// load database driver class
Class.forName( driver );
// connect to database
connection = DriverManager.getConnection( url );
// create Statement to query database
statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
// set query and execute it
setQuery( query );
}
// get class that represents column type
public Class getColumnClass( int column )
{
// determine Java class of column
try {
String className =
metaData.getColumnClassName( column + 1 );
// return Class object that represents className
return Class.forName( className );
}
// catch SQLExceptions and ClassNotFoundExceptions
catch ( Exception exception ) {
exception.printStackTrace();
}
// if problems occur above, assume type Object
return Object.class;
}
// get number of columns in ResultSet
public int getColumnCount()
{
// determine number of columns
try {
return metaData.getColumnCount();
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems occur above, return 0 for number of columns
return 0;
}
// get name of a particular column in ResultSet
public String getColumnName( int column )
{
// determine column name
try {
return metaData.getColumnName( column + 1 );
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems, return empty string for column name
return "";
}
// return number of rows in ResultSet
public int getRowCount()
{
return numberOfRows;
}
// obtain value in particular row and column
public Object getValueAt( int row, int column )
{
// obtain a value at specified ResultSet row and column
try {
resultSet.absolute( row + 1 );
return resultSet.getObject( column + 1 );
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems, return empty string object
return "";
}
// close Statement and Connection
protected void finalize()
{
// close Statement and Connection
try {
statement.close();
connection.close();
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
}
// set new database query string
public void setQuery( String query ) throws SQLException
{
// resultSet.deleteRow(); not works!
// specify query and execute it
resultSet = statement.executeQuery( query );
// obtain meta data for ResultSet
metaData = resultSet.getMetaData();
// determine number of rows in ResultSet
resultSet.last(); // move to last row
numberOfRows = resultSet.getRow(); // get row number
// notify JTable that model has changed
fireTableStructureChanged();
}
} // end class ResultSetTableModel