I searched through a few pages, but most seem to be just having comprehension issues as to what static means.The problem I have lies in that we are using a static class, FocusListener, and ActionListener. The class that has the event handling calls on the static class, and when a JTextfield is fill and tabbed away from FocusListener updates that static variable instantly. When all JTextfields are filled and FocusListener has updated the variables, there is a submit JButton. As soon as the button is clicked, the static methods are called to finish of any variables that are calculated using the previously updated variables. User is not aware of this. The variables do not update though, and I am curious if I am implementing this wrong? Thanks in advance.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WellParameters extends JInternalFrame implements FocusListener, ActionListener {
JLabel measuredDepthL, ..., pitGainL, drillBitSizeL,
mudInActivePitsL, mainPanelLabel;
JTextField measuredDepthT, ..., pitGainT, drillBitSizeT, mudInActivePitsT;
JPanel mainPanel, firstPanel, secondPanel, thirdPanel, fourthPanel, submitButtonPanel;
JButton submitButton;
WellParameters() {
super("Well Parameters", true, true, false, true);
this.setBounds(0, 0, 600, 385);
this.setVisible(true);
this.setLayout(new BorderLayout());
...//GUI Stuff
this.add(submitButtonPanel, BorderLayout.SOUTH);
}
@Override
public void focusGained(FocusEvent e) {} //Ignore this!
@Override
public void focusLost(FocusEvent e) {
try {
if(e.getSource() == measuredDepthT) {
KillWellCalculations.measuredDepth = Integer.parseInt(measuredDepthT.getText());
...//Others
} else if(e.getSource() == mudInActivePitsT) {
KillWellCalculations.mudInActivePits = Double.parseDouble(mudInActivePitsT.getText());
}
} catch (Exception ignore) {}
}
@Override
public void actionPerformed(ActionEvent e) {
try {
if(e.getSource() == submitButton) {
System.out.println(KillWellCalculations.pumpEfficiency);
KillWellCalculations.setPressureBeforeCasingBurstAndFormationFracture(); //Doesn't work
KillWellCalculations.setCirculatingPressures();
KillWellCalculations.setTriplexPumpCapacity();
System.out.println(KillWellCalculations.mudInActivePits);
System.out.println(KillWellCalculations.pumpFactor);
System.out.println(KillWellCalculations.finalCirculatingPressure);}
}
catch(Exception ignore) {}
}
}
That was the GUI, and this is the static class… They are 2 separate classes. Not in the same file.
package killwellsheet;
public class KillWellCalculations {
static int measuredDepth; //Total Depth from open hole to bottom
... //Tons of other variables
static double totalStrokes; //add strokes
//Used to set different circulating pressures for the well
public static void setCirculatingPressures() {
initialCirculatingPressure = circulatingPressureKillRate + shutInDrillPipePressure;
finalCirculatingPressure = circulatingPressureKillRate * (killMudWeight/currentMudWeight);
}
//Calculates capacity of anypipe
private static double pipeCapacity(double length, double insideDiameter) {
return length * ((insideDiameter*insideDiameter)/1029.4);
}
//Calculates capacity of the annulus/open hole
private static double annulusCapacity(double length, double insideDiameter, double outsideDiameter) {
return length * (((insideDiameter*insideDiameter)-(outsideDiameter*outsideDiameter))/1029.4);
}
... //Other functions
//Set the casing burst pressure
public static void setPressureBeforeCasingBurstAndFormationFracture() {
beforeCasingBurst = burstPressure*0.70;
beforeFormationFracture = (0.052*casingShoeDepth)*(fracGradientMWEquivalent - currentMudWeight);
}
public static void bariteNeedAndVolumeIncrease() {
bariteSacksRequired = (totalMudVolume/100)*((1099*(killMudWeight-currentMudWeight))/(28.35-killMudWeight));
increaseInMudVolume = 0.091*bariteSacksRequired;
}
public static void pumpStrokes() {
surfaceToBit = (mudInDrillString)/pumpFactor;
bitToSurface = (mudInAnnulus)/pumpFactor;
totalStrokes = surfaceToBit + bitToSurface;
}
}//end class
Some clarifications… By reading your code, the only thing I see is that your class WellParameters implements the Interfaces FocusListener and ActionListener (not static classes). As far as I can figure out from your code, the methods focusGained(FocusEvent e) and focusLost(FocusEvent e) are implemented from the former Interface and updates the values of the static class, and you would depend on same calculated values in the ActionListener event actionPerformed(). The problem you are facing here is race-condition regarding the static instance values.
Based on this Garbage Collection documentation, I suspect that ALL the static references of your class KillWellCalculations were eligible to be garbage collected after the call to the FocusEvent methods and, for this reason, they are not available by the time the event actionPerformed() is fired.
You can still use the static methods of the class KillWellCalculations as a utility class only if this class is used by other classes. If not, you can transform it into a Value class that holds the calculations for you, WITHOUT the static references. As you need to have a reference to an instance of a class that holds the values of the calculation… For instance:
Then, modify the constructor of the class to have an instance of the value object:
Then, update the reference with the calculation:
The the updates on the final step also using the instance reference:
I did some work way back related to GUI… You can see a very similar example at http://code.google.com/p/marcellodesales-cs-research/source/browse/trunk/grad-ste-ufpe-brazil/ptf-add-on-dev/src/br/ufpe/cin/stp/ptfaddon/view/swing/execution/JWizardInternalFrame.java
Good Luck!