Java GUI 面板重新生效

Java GUI panel revalidation

我有一个主程序 class,它控制所有应用程序,包括所有面板的显示。显示主应用程序面板的方法是:

private void displayMainApplicationPanel() {
  String[] columnNames = { "Media ID", "Title", "Pricipal Actors", "Type", "Duration", "Launch Date", "Price",
    "Status" };
ResultSet resultSet = databaseLogicController.showMediaInfo();
String[][] data = parseMediaResultSet(resultSet, 8);

SwingUtilities.invokeLater(new Runnable() {

  @Override
  public void run() {
    graphicController.showMainApplicationPanel(data, columnNames);
    addMainApplicationPanelSearchButtonActionListener();
    addMainApplicationPanelSearchTextFieldKeyListener();
    addMainApplicationPanelShowMyIdButtonActionListener();
    addMainApplicationPanelBorrowMediaButtonActionListener();
    addMainApplicationPanelMakeInternetRezervationButtonActionListener();
    addMainApplicationPanelShowVHSInformationButtonActionListener();
    addMainApplicationPanelShowDVDInformationButtonActionListener();
    addMainApplicationPanelShowInternetRezervationsActionListener();
    addMainApplicationPanelShowClientsInformationButtonActionListener();
    addMainApplicationPanelInsertClientButtonActionListener();
    addMainApplicationPanelInsertMovieButtonActionListener();
    addMainApplicationPanelInsertVHSButtonActionListener();
    addMainApplicationPanelInsertDVDButtonActionListener();
  }
});

}

在那个主面板上,我有一个 table 显示所有当前媒体。我希望能够通过搜索功能更新 table。

搜索按钮动作侦听器的代码是:

private void addMainApplicationPanelSearchButtonActionListener() {
    graphicController.getMainApplicationPanel().addSearchButtonActionListener(new    ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
    String[] columnNames = { "Media ID", "Title", "Pricipal Actors", "Type", "Duration", "Launch Date", "Price",
        "Status" };
    String title = graphicController.getMainApplicationPanel().getSearchTextField().getText();
    ResultSet resultSet = databaseLogicController.showParticularMediaInfo(title);
    String[][] data = parseMediaResultSet(resultSet, 8);

    graphicController.getMainApplicationPanel().setMediaTable(new JTable(data, columnNames));
    graphicController.getMainApplicationPanel().repaint();
    graphicController.getMainApplicationPanel().revalidate();
    graphicController.getMainFrame().repaint();
    graphicController.getMainFrame().revalidate();
  }
});

}

现在,我根据搜索条件在动作侦听器中创建一个新的 table 并将其设置在主应用程序面板中,然后在主框架和主面板上调用重绘和重新验证.为什么没有显示新的 table?

不推荐你使用这条线路

graphicController.getMainApplicationPanel().setMediaTable(new JTable(data, columnNames));

你可以更好地获得 table 然后 ((DefaultTableModel)myTable.getModel()).setRowCount(0);((DefaultTableModel)myTable.getModel()).addRow(new Object[]{data1, data2, ...});

Now, I create a new table in the action listener based on the search criteria

不要创建新的 table。最简单的方法是用新的 TableModel 更新现有的 table:

table.setModel(...);