JTable无法更改单元格值

时间:2013-08-02 09:33:54

标签: swing jtable jframe global-variables tablecelleditor

我创建了一个创建JTable的方法,如下所示:

public void refTable(String jobNo) {
        Report rp = new Report();
        final String noJob = jobNo;

        Map<Integer, String> jMap = rp.getReportInfo(jobNo);
        Map<Integer, String> sortedMap = new TreeMap<Integer, String>(jMap);

        String[] row = new String[sortedMap.size()];
        Integer[] no = new Integer[sortedMap.size()];
        String[] stat = new String[sortedMap.size()];
        Boolean[] dev = new Boolean[sortedMap.size()];
        String[] remark = new String[sortedMap.size()];
        Boolean[] rem = new Boolean[sortedMap.size()];
        userRemark = new String[sortedMap.size()];
        tabSize = sortedMap.size();
        int i = 0;

        for (Integer key : sortedMap.keySet()) {
            no[i] = key;
            String[] val = sortedMap.get(key).split("###");
            if (val[0].trim().equals("DEV")) {
                stat[i] = "FAIL";
            } else {
                stat[i] = val[0].trim();
            }
            row[i] = val[1].trim();
            dev[i] = false;
            remark[i] = "";
            //remark[i] = false;
           //if(userRemark1[i]!=null)
            userRemark[i] = RemarkDropDownList.userOthersReamrk;
            //else
            //userRemark[i] ="";   
            rem[i] = false;
            i++;
        }

        DefaultTableModel model = new DefaultTableModel();
        model.fireTableDataChanged();
        jTable1.setModel(model);

        model.addColumn("No:", no);
        model.addColumn("Status:", stat);
        model.addColumn("Details:", row);
        model.addColumn("Non-Deviation", dev);
        model.addColumn("Remarks", remark);
        model.addColumn("Remove", rem);
        model.addColumn("UR", userRemark);

        TableColumn col1 = jTable1.getColumnModel().getColumn(0);
        col1.setPreferredWidth(30);

        TableColumn col2 = jTable1.getColumnModel().getColumn(1);
        col2.setPreferredWidth(30);

        TableColumn col3 = jTable1.getColumnModel().getColumn(2);
        TextRenderer renderer = new TextRenderer();
        col3.setCellRenderer(renderer);
        col3.setPreferredWidth(350);

        CellRenderer cellRender = new CellRenderer();
        TableColumn col4 = jTable1.getColumnModel().getColumn(3);
        col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
        col4.setCellRenderer(cellRender);
        col4.setPreferredWidth(50);

        TableButton buttonEditor = new TableButton("Button");
        buttonEditor.addTableButtonListener(new TableButtonListener() {
            //@Override
            public void tableButtonClicked(int row, int col) {
                RemarkDropDownList rmk = new RemarkDropDownList(noJob, row);
                rmk.setVisible(true);
                //userRemark1[row] = RemarkDropDownList.userOthersReamrk;
                //String test = RemarkDropDownList.userOthersReamrk;
            }
        });
        TableColumn col5 = jTable1.getColumnModel().getColumn(4);
        col5.setCellRenderer(buttonEditor);
        col5.setCellEditor(buttonEditor);

        TableColumn col6 = jTable1.getColumnModel().getColumn(5);
        col6.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
        col6.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
        col6.setPreferredWidth(50);

        jTable1.setShowGrid(true);
        jTable1.setGridColor(Color.BLACK);
        jTable1.setAutoCreateRowSorter(true);
    }

在我的JTable中,我在第5栏中创建了一个按钮。

TableButton buttonEditor = new TableButton("Button");
        buttonEditor.addTableButtonListener(new TableButtonListener() {
            //@Override
            public void tableButtonClicked(int row, int col) {
                RemarkDropDownList rmk = new RemarkDropDownList(noJob, row);
                rmk.setVisible(true);
                //userRemark1[row] = RemarkDropDownList.userOthersReamrk;
                //String test = RemarkDropDownList.userOthersReamrk;
            }
        });
        TableColumn col5 = jTable1.getColumnModel().getColumn(4);
        col5.setCellRenderer(buttonEditor);
        col5.setCellEditor(buttonEditor);

该按钮基本上会在其中创建一个带有组合框的新JFrame

public class RemarkDropDownList extends javax.swing.JFrame {
    public static String userOthersReamrk = "test";
    private String userSelectedItem = "File Issue";
    String jobno;
    int rowNo;
  public RemarkDropDownList() {
        initComponents();
        this.lblOthers.setVisible(false);
        this.txtOthers.setVisible(false);
    }
  public RemarkDropDownList(String jobNo, int row) {
        initComponents();
        this.lblOthers.setVisible(false);
        this.txtOthers.setVisible(false);
        this.jobno = jobNo;
        this.rowNo = row;
    }
 private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:    
        JComboBox cb = (JComboBox)evt.getSource();
         userSelectedItem = (String)cb.getSelectedItem();
                if(userSelectedItem.equalsIgnoreCase("others"))
                {
                    this.lblOthers.setVisible(true);
                    this.txtOthers.setVisible(true);                    
                }
                else
                {
                    this.lblOthers.setVisible(false);
                    this.txtOthers.setVisible(false);
                }
    }  
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
       if(userSelectedItem.equalsIgnoreCase("others"))
       {
        userOthersReamrk = txtOthers.getText().toString();
        if(userOthersReamrk == null || userOthersReamrk.equalsIgnoreCase(""))
        {
            JOptionPane.showMessageDialog(null, "Please enter Remark");            
        }
       }
       else
       {
           userOthersReamrk = userSelectedItem;
           //String [] remark = new String [1000];
           ///remark[rowNo] = userOthersReamrk;
           ReportPanelMin rpm = new ReportPanelMin();
           //rpm.userRemark1[rowNo] = remark[rowNo];
           rpm.refTable(jobno);
           this.setVisible(false);
       }       
    }  
}

一切正常,但最后一部分。我在userOthersReamrk类(新类)中创建了一个静态全局变量(RemarkDropDownList),以保持用户下拉选定值。我再次调用refTable函数重新加载表,以便我可以将userOthersReamrk值分配给第7列,如userRemark = new String[sortedMap.size()];。当我调试时,我可以看到用户选择的值被分配给userRemark数组,但它不在表中。该列显示我在test中分出的默认值RemarkDropDownList。我知道代码很长,但是我发布了所有内容以便更好地理解我在做什么。我需要做什么/哪里进行更改,以便我的用户选择的值显示在第7列而不是默认值。

0 个答案:

没有答案