Java GUI面板重新验证

时间:2015-01-09 20:54:53

标签: java user-interface repaint

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

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

}

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

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

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

}

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

2 个答案:

答案 0 :(得分:0)

我不建议你使用这一行

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

您可以更好地获取该表格((DefaultTableModel)myTable.getModel()).setRowCount(0);((DefaultTableModel)myTable.getModel()).addRow(new Object[]{data1, data2, ...});

答案 1 :(得分:0)

  

现在,我根据搜索条件

在动作侦听器中创建一个新表

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

table.setModel(...);