简体   繁体   English

Java GUI面板重新验证

[英]Java GUI panel revalidation

I am having a main class which controls all the application, including the displaying of all the panels. 我有一个主类,它控制所有应用程序,包括所有面板的显示。 The method to display the main application panel is: 显示主应用程序面板的方法是:

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();
  }
});

} }

On that main panel, I have a table which shows all the current medias. 在该主面板上,我有一个表,显示所有当前媒体。 I want to be able to update that table via a search function. 我希望能够通过搜索功能更新该表。

The code for the search button action listener is: 搜索按钮操作侦听器的代码为:

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();
  }
});

} }

Now, I create a new table in the action listener based on the search criteria and set it in the main application panel, followed by calls to repaint and revalidate on both the main frame and the main panel. 现在,我根据搜索条件在动作侦听器中创建一个新表,并在主应用程序面板中进行设置,然后在主框架和主面板上调用重画和重新验证。 Why isn't the new table shown? 为什么未显示新表?

I don't recommand you using this line 我不建议您使用此行

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

You can better get that table then ((DefaultTableModel)myTable.getModel()).setRowCount(0); 您可以更好地获取该表,然后((DefaultTableModel)myTable.getModel()).setRowCount(0); and ((DefaultTableModel)myTable.getModel()).addRow(new Object[]{data1, data2, ...}); ((DefaultTableModel)myTable.getModel()).addRow(new Object[]{data1, data2, ...});

Now, I create a new table in the action listener based on the search criteria 现在,我根据搜索条件在动作侦听器中创建一个新表

Don't create a new table. 不要创建新表。 The easiest approach is to update the existing table with a new TableModel: 最简单的方法是使用新的TableModel更新现有表:

table.setModel(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM