从重写方法获取对象

时间:2013-02-05 23:13:06

标签: java events methods jtable listselectionlistener

public class Job_GUI extends javax.swing.JFrame {

    private JobDTO jdto;

    public Job_GUI() {
        initComponents();
    }

      private void menuEditJobActionPerformed(java.awt.event.ActionEvent evt) {
            editJob.setVisible(true);
            //here I want to obtain the updated dto. 
        }

  } 


public class JobDTO extends BaseDTO {
//setters and getters

}


class ListDataUI { 

    private void initListeners() {
        summaryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        summaryTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    final int selectedRowIndex = summaryTable.getSelectedRow();
                    if (selectedRowIndex != -1) {
                        BaseDTO dto = data.get(summaryTable.convertRowIndexToModel(selectedRowIndex));
                    } else {
                    }
                }
            }
        });

    }

}

我不确定如何获取BaseDTO对象dtomenuEditJobActionPerformed方法,以便显示对象的所有值。事件发生时如何从valueChanged传递此信息?我只是使用一个表,当选择一行时,dto状态发生变化,需要将这个新状态传递给类Job_GUI actionperformed方法

1 个答案:

答案 0 :(得分:1)

dto声明后立即声明class GUI {

这样它就具有全局范围,所以你的所有功能都可以看到它。

class GUI {
  BaseDTO dto;

  private void menuEditJobActionPerformed(java.awt.event.ActionEvent evt) {
        editJob.setVisible(true);
        //Now you can access "dto"
    }

  } 

class ListDataUI { 

    private void initListeners() {
        summaryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        summaryTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    final int selectedRowIndex = summaryTable.getSelectedRow();
                    if (selectedRowIndex != -1) {
                        /*BaseDTO */dto = data.get(summaryTable.convertRowIndexToModel(selectedRowIndex));
                        //not declaring a new object
                    } else {
                    }
                }
            }
        });

    }

}