我在我的jframe上以编程方式创建了输入字段。现在我想创建一个保存按钮,将所有结果保存到数据库。我是JAVA的初学者,因此不熟悉。我有一个结果的实体对象,以及一个保存到数据库的控制器。这不是问题,但我的问题是编写事件处理程序。
private void jComboBoxSurveyFocusLost(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PTSchemePU");
EntityManager em = emf.createEntityManager();
jPanelTests.setLayout(new GridLayout(0, 1));
Query surveyQ = em.createNamedQuery("Survey.findBySurveyDescription");
surveyQ.setParameter("surveyDescription", jComboBoxSurvey.getSelectedItem().toString());
Survey survey = (Survey) surveyQ.getSingleResult();
int testset_ID = survey.getTestset();
Query testsetQ = em.createNamedQuery("TestsetV.findByTestSet");
testsetQ.setParameter("testsetid", testset_ID);
List<TestsetV> TestSetList = (List<TestsetV>)testsetQ.getResultList();
totalTests = TestSetList.size();
JPanel[] myPanel = new JPanel[totalTests];
JLabel[] myTestID = new JLabel[totalTests];
JTextField [] mytextfield = new JTextField[totalTests];
JComboBox[] myCombo = new JComboBox[totalTests];
JCheckBox[] myNotReturn = new JCheckBox[totalTests];
JCheckBox[] myNotEval = new JCheckBox[totalTests];
JComboBox[] myReason = new JComboBox[totalTests];
JButton[] mySave = new JButton[totalTests];
jLabelTestcount.setText(Integer.toString(totalTests));
jLabelToberesulted.setText(Integer.toString(totalTests));
for (int tst=0; tst< TestSetList.size(); tst++) {
myPanel[tst] = new JPanel();
myTestID[tst] = new JLabel();
mytextfield[tst] = new JTextField();
myNotReturn[tst] = new JCheckBox("Not Returned");
myNotEval[tst] = new JCheckBox("Not Evaluated");
myReason[tst] = new JComboBox();
mySave[tst] = new JButton("Save");
Query qR = em.createNamedQuery("Lookups.findByLookupType");
qR.setParameter("lookupType","REAS");
java.util.List<Lookups> reasonList = (java.util.List<Lookups>)qR.getResultList();
for (int i = 0 ; i < reasonList.size(); i++) {
Lookups lu = reasonList.get(i);
myReason[tst].addItem(lu.getLookupDescription());
}
myPanel[tst].setLayout(new SpringLayout());
int rows =1;
int cols = 5;
myPanel[tst].setSize(10, 10);
myPanel[tst].setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
myPanel[tst].setVisible(true);
jPanelTests.add(myPanel[tst]);
mytextfield[tst].setSize(10, 10);
myNotReturn[tst].setName("No Return");
myNotEval[tst].setName("Not Evauated");
JLabel testName = new JLabel(TestSetList.get(tst).getTestDesctiption());
myPanel[tst].add(new JLabel(Integer.toString(TestSetList.get(tst).getTestsId())));
myPanel[tst].add(testName);
switch (TestSetList.get(tst).getTestSetup()){
case "TEXT" : {
myPanel[tst].add(mytextfield[tst]);
break;
}
case "COMB" : {
Query q = em.createNamedQuery("Lookups.findByLookupType");
q.setParameter("lookupType",TestSetList.get(tst).getLookup() );
java.util.List<Lookups> lookupList = (java.util.List<Lookups>)q.getResultList();
for (int i = 0 ; i < lookupList.size(); i++) {
Lookups lu = lookupList.get(i);
cb.addItem(lu.getLookupCode());
}
myPanel[tst].add(cb);
break;
}
case "SPIN" : {
myPanel[tst].add(sp);
break;
}
} // end switch
myPanel[tst].add(myNotReturn[tst]);
myPanel[tst].add(myNotEval[tst]);
myPanel[tst].add(myReason[tst]);
myPanel[tst].add(mySave[tst]);
SpringUtilities.makeCompactGrid(myPanel[tst], 1, myPanel[tst].getComponentCount(),6,6,6,6);
}
pack();
jPanelTests.setVisible(true);
}
然后,当面板失去焦点时,我创建了一个事件handeler
myPanel[tst].addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void focusLost(FocusEvent e) {
myResults[tst].setResultValue(mytextfield[tst].getText());
}
});
但是现在我得到一个错误(本地变量从内部类重新引用,必须是有效的最终结果)myResults[tst].setResultValue(mytextfield[tst].getText());
如何引用jTextField / JComboBox / jSpinner中输入的值
谢谢
答案 0 :(得分:1)
你的计数器变量tst不是final(意味着没有使用final关键字),也不是有效的final(意思是它被修改),因此,它不能从内部类中引用 - 在你的情况下是一个FocusListener的实例。
你修改它时不能将tst标记为final - 但是你可以在循环体中创建一个新变量。
请注意,有效的最终概念是Java8的新概念,在早期版本中,您必须是显式的,并将最终关键字添加到从内部类访问的所有局部变量。
研究以下示例:
public class TestFinalVariables {
public static void main(String[] args) {
String effectivelyFinal = "aaa";
for (int tst=0; tst< 10; ++tst) {
final int j = tst; //explicit final not necessary here
new Runnable() {
@Override
public void run() {
System.out.println(effectivelyFinal);
System.out.println(j);
//System.out.println(tst); Won't compile
}
}.run();
}
}
}