将值传递给jDialog并在打开时运行proc

时间:2014-12-23 12:30:15

标签: java parameters jdialog

我需要编写这样的行为: 从主框架与buttonlistener我打开“历史”窗口(jDialog?jFrame?)。 此“历史”窗口应使用参数(Param)对数据库执行查询 从主窗口传递并打开结果。

所以我创建了DlgHistory私有iParam和方法来设置它。 但是我应该在哪里运行我的proc getHistory? 如果我将它设置在“public DlgHistory()”的末尾 - 它在setParam之前运行,所以iParam为空 (虽然以后设置得当)

调用窗口:

JButton btnHistory = new JButton("History");
btnHistory.setBounds(176, 413, 113, 23);
frame.getContentPane().add(btnHistory);
btnHistory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
    System.out.println("Running history");
    DlgHistory Hist= new DlgHistory();
    Hist.setParam(iParam);
    Hist.setVisible(true);  
});

历史jDialog:

public class DlgHistory extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private int iParam;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            DlgHistory dialog = new DlgHistory();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * Create the dialog.
     */
    public DlgHistory() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            // buttons definitions
        }
        // This proc should run after setParam
        getHistory(iParam);
    }
    public void setParam(int Param){
        System.out.println("Setting param" + Param);
        this.iParam = Param;
    }

    private void getHistory(int iParam) {
        System.out.println(" run query with param:  "  " + iParam);
        // run query
    }

}

1 个答案:

答案 0 :(得分:0)

JButton btnHistory = new JButton("History");
btnHistory.setBounds(176, 413, 113, 23);
frame.getContentPane().add(btnHistory);
btnHistory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
    System.out.println("Running history");
   //Some function that will do your query
    iParam=somefunction();
    DlgHistory Hist= new DlgHistory(iParam);
    Hist.setVisible(true);  
});

并对DlgHistory构造函数进行更改并使用它最初执行的操作