【问题标题】:How to delete the reflected row of the second JTable if the user selects another row in the first JTable?如果用户在第一个 JTable 中选择另一行,如何删除第二个 JTable 的反射行?
【发布时间】:2021-06-01 00:12:53
【问题描述】:

我在用 Java 删除我的 Products Stock In GUI 的第二个表的前一行时遇到问题。

我的 GUI 的功能流程是这样的:

  1. 如果用户将在 GUI 的第一个表中选择一行,这些行的值将反映在第二行上

  2. 如果用户将在第一个表中选择另一个/不同的行,则必须删除在第一个表中选择的先前反映的行并替换为当前选择的行。

总而言之,如果我要在第一个表中选择不同的行,然后将之前选择的行替换为当前选择的行,我必须替换/删除之前的反射行。

这是我的源代码:

private void firstTableMouseClicked(java.awt.event.MouseEvent evt) {
  DefaultTableModel secondTblmodel = (DefaultTableModel) secondTable.getModel();
  int selectPRoww = firstTable.getSelectedRow();
  for (int x = 0; x < ProductAllData2[selectPRoww].length; x++) {
    //ProductAllData2 is a 3D array
    if (ProductAllData2[selectPRoww][x][0] != null) {
      if (setRowCountID2 == 0)
        secondTblmodel.setRowCount(0);
      //Reflects and displays the values of the selected
      // row from the first table to the second table
      secondTblmodel.addRow(ProductAllData2[selectPRoww][x]);
      // deletes previous row if index of selected row is
      // greater than the previous selected row (doesn't work)
      if (selectPRoww > selectPRoww) {
        secondTblmodel.removeRow(selectPRoww - 1);
      }
      // deletes previous row if index of selected row is
      // less than the previous selected row (doesn't work)
      if (selectPRoww < selectPRoww) {
        secondTblmodel.removeRow(selectPRoww + 1);
      }
      setRowCountID2++;
    }
  }
}

我的功能中是否缺少某些内容,或者我是否需要修改 for 循环?

【问题讨论】:

    标签: java if-statement user-interface multidimensional-array row


    【解决方案1】:

    在我看来,你的逻辑不应该是这样的。

    由于第二个表(视图)依赖于第一个表的选择,那么第二个表应该总是清空的,那么你就做这个逻辑吧。

    //Reflects and displays the values of the selected
    // row from the first table to the second table
    secondTblmodel.addRow(data);
    

    重点:请在table1上使用ListSelectionListener,而不是依赖鼠标点击。

    使用示例:

    table1.getSelectionModel().addListSelectionListener(new SharedListSelectionHandler());
    
    class SharedListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();
    
            int firstIndex = e.getFirstIndex();
            int lastIndex = e.getLastIndex();
            boolean isAdjusting = e.getValueIsAdjusting();
            output.append("Event for indexes "
                    + firstIndex + " - " + lastIndex
                    + "; isAdjusting is " + isAdjusting
                    + "; selected indexes:");
    
            if (lsm.isSelectionEmpty()) {
                output.append(" <none>");
            } else {
                // Find out which indexes are selected.
                int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();
                for (int i = minIndex; i <= maxIndex; i++) {
                    if (lsm.isSelectedIndex(i)) {
                        output.append(" " + i);
                    }
                }
            }
            output.append(newline);
        }
    }
    

    How to Write a List Selection Listener | The Java™ Tutorials

    类似的 StackOverflow:Deleting row from JTable after valueChanged event is triggered

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2013-12-19
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      相关资源
      最近更新 更多