如何在JFrame按钮点击时调用另一个类的公共方法
Got it, let's walk through how to trigger your display_all_Arrears() method from another JFrame's JButton click. Here's a practical solution that fits your existing code structure:
Step 1: Complete the
Arrears Class Method First, let's finish the display_all_Arrears() method (since your code snippet was truncated). We'll make it accept a reference to your First_Term_Arrears frame so it can update UI components like a JTable:
package Pack.billing; import Pack.First_Term_Arrears; import Pack.myKIDS; import java.sql.*; import javax.swing.*; import net.proteanit.sql.DbUtils; public class Arrears { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; public Arrears(){ conn = myKIDS.connectKids(); // Quick check to confirm database connection if (conn == null) { JOptionPane.showMessageDialog(null, "Database connection failed!"); } } // Completed method to fetch and display arrears data public void display_all_Arrears(First_Term_Arrears targetFrame) { String sqlQuery = "SELECT * FROM first_term_arrears"; // Adjust this to your actual table/columns try { pst = conn.prepareStatement(sqlQuery); rs = pst.executeQuery(); // Assuming your First_Term_Arrears frame has a JTable named 'arrearsTable' targetFrame.arrearsTable.setModel(DbUtils.resultSetToTableModel(rs)); } catch (SQLException e) { JOptionPane.showMessageDialog(null, "Error loading arrears: " + e.getMessage()); e.printStackTrace(); } finally { // Clean up database resources to avoid leaks try { if (rs != null) rs.close(); if (pst != null) pst.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
Next, in the First_Term_Arrears frame (or whichever JFrame has the JButton), add an ActionListener to the button that creates an Arrears instance and calls your method:
package Pack; import Pack.billing.Arrears; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class First_Term_Arrears extends JFrame { public JTable arrearsTable; // Make this accessible (or use a getter method if you prefer encapsulation) private JButton loadArrearsButton; public First_Term_Arrears() { // Initialize your UI components arrearsTable = new JTable(); loadArrearsButton = new JButton("Load All Arrears"); // Attach the click listener to the button loadArrearsButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Create an instance of Arrears (reuse this instance if you want to keep the DB connection open) Arrears arrearsManager = new Arrears(); // Call the method and pass this frame to update the table arrearsManager.display_all_Arrears(First_Term_Arrears.this); } }); // Add components to your frame (adjust layout as needed) add(new JScrollPane(arrearsTable)); // Wrap table in scroll pane for usability add(loadArrearsButton); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public static void main(String[] args) { // Run Swing code on the Event Dispatch Thread for thread safety SwingUtilities.invokeLater(() -> new First_Term_Arrears()); } }
Key Notes to Keep in Mind
- Encapsulation Tip: Instead of making
arrearsTablepublic, you could add a public method likesetArrearsTableModel(TableModel model)toFirst_Term_Arrears—this keeps your code cleaner and follows OOP best practices. - Reuse the Arrears Instance: If you don't want to create a new
Arrearsobject every time the button is clicked, initialize it once in the frame's constructor and reuse it. This saves you from re-establishing the database connection repeatedly. - Thread Safety: Always wrap Swing UI updates in
SwingUtilities.invokeLater()if you're doing background work (though in this simple case, the DB call runs on the EDT—for larger datasets, consider usingSwingWorkerto avoid freezing the UI).
内容的提问来源于stack exchange,提问作者Rodney Nart
相关产品推荐
相关产品推荐

