开发者

Persisting object value in Java Swing application

I have a swing UI having two buttons : 1. Loading database values to Hashmap 2. Comparing input values to database values. Previously both the operations were done using the same button so the Hashmap was getting populated and the inputs were compared correctly. But now its not so. After debugging I came to know that the hashmap values are null as the values are lost during the second operation.

How to approach this problem. So that the hash map values persist while i click the second button.

Code Example: Button1: Loading-

LoadMaps = new JButton( new AbstractAction("LoadMaps") {      开发者_如何学编程   

@Override         

public void actionPerformed( ActionEvent e ) {             

DRGCalc t = new DRGCalc();

t.loadHashMaps();
            }    
        }); 

Button2: Calculation-

public void actionPerformed(ActionEvent evt) {

DRGCalc d = new DRGCalc();

int i = d.calculateDRG(Codes);

}

As t and d are two seperate objects so the values in t wont persist when I am clicking d. For this I have included the method used in button 1 inside the calulateDRG call and its working fine but I want both to be separate. How to do this? Thanks


DRGCalc t = new DRGCalc(); 

This object only exist within the ActionListener. If you want the object to be available to other ActionListeners or other methods in your class then you need to make it a class variable. So in your class you need to define:

DRGCalc t; 

Then in the ActionListener you change the code to:

t = new DRGCalc(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜