Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update sql populated JTable

Status
Not open for further replies.

bobrivers2003

Technical User
Oct 28, 2005
96
GB
I have a frame that opens up and displays a jtable with sql data. I have a button that i want to click that sends a order by command, and refreshes the table so I can order the table by one of the two column headings. When I press the button (actionlistener near bottom of code) it opens a new frame but the table isn't refreshed. I just want it to refresh the current table.

Suggestions, ideas or pointers will be greatly appreciated. Many Thanks

import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;




public class buttonTest extends JFrame {

private JPanel jContentPane = null;
private JButton jButton = null;
String sortVar = "second";

public buttonTest() {
Vector columnNames = new Vector();
Vector data = new Vector();

initialize();

try {
// Connect to the Database

String driver = "org.gjt.mm.mysql.Driver";
//
// String url = "jdbc:eek:dbc:Teenergy"; // if using ODBC Data Source
// name
String url = "jdbc:mysql://127.0.0.1/test";
String userid = "root";
String password = "monkey";

Class.forName(driver);
Connection connection = DriverManager.getConnection(url, userid,
password);

// Read data from a table

String sql = "Select * from joke order by "+ sortVar;
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();

// Get column names

for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}

// Get row data

while (rs.next()) {
Vector row = new Vector(columns);

for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}

data.addElement(row);
}

rs.close();
stmt.close();
} catch (Exception e) {
System.out.println(e);
}

JTable table = new JTable(data, columnNames) {

public Class getColumnClass(int column) {
for (int row = 0; row < getRowCount(); row++) {
Object o = getValueAt(row, column);

if (o != null) {
return o.getClass();
}
}

return Object.class;
}
};

// JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add(new JScrollPane(table), BorderLayout.NORTH);
}

private void initialize() {
this.setSize(300, 300);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}


private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJButton(), java.awt.BorderLayout.SOUTH);
}
return jContentPane;
}


private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//JOptionPane.showMessageDialog(null, "hello", "Message", JOptionPane.ERROR_MESSAGE);

String sortVar = "first";
buttonTest frame1 = new buttonTest();
frame1.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
}
});
}
return jButton;
}

public static void main(String[] args) {


buttonTest frame = new buttonTest();
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame.pack();
frame.setVisible(true);


}

}
 
Well, your actionPerformed method just creates another class, you need to modify it so it orders the table in the same class.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top