我有一个应用程序从数据库中获取一些数据并在JTable
中显示。一切都很好,但我希望点击JButton
后我的表格会被刷新。我的意思是如果我在JTable中编写smth,在点击JButton之后,应用程序将再次连接数据库,获取数据并正确显示它。我试图将一个ActionListener添加到JButton,但我不知道我应该在方法actionPerformed()中写什么。
这是我的代码:
public class App extends JFrame implements ActionListener{
protected JButton button;
public App() {
Vector<Object> columnNames = new Vector<Object>();
Vector<Object> data = new Vector<Object>();
button = new JButton("Refresh");
button.addActionListener(this);
try
{
...
String query = "Select count(distinct country) sum from customers";
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery(query);
query = "SELECT * from country";
rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++)
{
columnNames.addElement(md.getColumnName(i));
}
while (rs.next())
{
Vector<Object> row = new Vector<Object>(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement(rs.getObject(i) );
}
data.addElement(row);
}
rs.close();
stmt.close();
c.close();
}
catch(Exception e)
{
System.out.println(e);
}
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
@Override
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;
}
};
JTable table = new JTable(model);
...
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
getContentPane().add(button, BorderLayout.SOUTH);
button.addActionListener(this);
}
public static void main(String[] args)
{
App frame = new App();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setSize(250,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
}
}
答案 0 :(得分:0)
table
设为实例字段,这样就可以通过actionPerformed
方法访问actionPerformed
方法中调用actionPerformed
时,请调用&#34;数据加载&#34;方法,创建一个新的TableModel
并将其应用于JTable
例如
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class App extends JFrame implements ActionListener {
private static String dbName;
private static String userName;
private static String password;
protected JButton button;
private JTable table;
public App() {
button = new JButton("Refresh");
button.addActionListener(this);
table = new JTable();
try {
table.setModel(loadData());
} catch (ClassNotFoundException | SQLException | IOException ex) {
ex.printStackTrace();
}
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table.getColumnModel().getColumn(0).setHeaderValue("Nazwa panstwa");
table.getColumnModel().getColumn(1).setHeaderValue("Przychod netto USD");
table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
table.getColumnModel().getColumn(1).setCellRenderer(centerRenderer);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
getContentPane().add(button, BorderLayout.SOUTH);
button.addActionListener(this);
}
public static void main(String[] args) {
App frame = new App();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setSize(250, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected TableModel loadData() throws ClassNotFoundException, SQLException, IOException {
Vector<Object> columnNames = new Vector<Object>();
Vector<Object> data = new Vector<Object>();
Class.forName("org.postgresql.Driver");
try (Connection c = DriverManager.getConnection(dbName, userName, password)) {
try (BufferedReader br = new BufferedReader(new FileReader("login.txt"))) {
dbName = br.readLine();
userName = br.readLine();
password = br.readLine();
}
c.setAutoCommit(false);
System.out.println("Opened database successfully");
String query = "Select count(distinct country) sum from customers";
try (Statement stmt = c.createStatement()) {
try (ResultSet rs = stmt.executeQuery(query)) {
query = "SELECT C.country, SUM(O.netamount) "
+ "from customers as C join orders as O "
+ "on C.customerid = O.customerid "
+ "where O.orderdate < '20040701' and O.orderdate > '20031231' "
+ "group by C.country "
+ "order by SUM(O.netamount) DESC";
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}
while (rs.next()) {
Vector<Object> row = new Vector<Object>(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
}
}
}
return new DefaultTableModel(data, columnNames) {
@Override
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;
}
};
}
public void actionPerformed(ActionEvent arg0) {
try {
table.setModel(loadData());
} catch (ClassNotFoundException | SQLException | IOException ex) {
ex.printStackTrace();
}
}
}
您需要更好地关注您正在创建的资源,因为从Java 7开始,您可以使用try-with-resources
来管理这些类型的资源
它的作品,谢谢。但我有一个问题,当我刷新我的表时所有数据都显示没有格式。我的意思是数据不在中心,列的名称是默认的。
重新申请格式化程序......
public void actionPerformed(ActionEvent arg0) {
try {
table.setModel(loadData());
} catch (ClassNotFoundException | SQLException | IOException ex) {
ex.printStackTrace();
}
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table.getColumnModel().getColumn(0).setHeaderValue("Nazwa panstwa");
table.getColumnModel().getColumn(1).setHeaderValue("Przychod netto USD");
table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
table.getColumnModel().getColumn(1).setCellRenderer(centerRenderer);
}
当触发TabelStructureChanged
事件时,表格将重置渲染器
答案 1 :(得分:0)
我没有使用JTable,但解决方案应该类似于以下内容。
您应该将从数据库获取数据的语句移动到单独的方法,例如“getData()”。然后单击该按钮时,只需调用该方法并使用新返回的数据创建一个新的“DefaultTableModel”对象。然后调用JTable引用上的方法来设置新模型。