2017-08-13 59 views
0

我有一個使用Eclipse構建的Java SWT GUI。我正在使用while循環來引用文本文件。 while循環遍歷文本文件的每一行,併爲每行上的特定項目構建一系列組合或文本框。每行代表GUI中的一個可視列,並且根據文本文件中有多少項,GUI將向右移動。爲了簡單起見,我只包含了我想要弄清楚的代碼。例如,假設我有三行在GUI中創建六個組合框(三列兩行)。我想在第二列的第一行上進行更改,以在最底一行(也在第二列)上執行一個監聽器。然而,現在聽衆循環了所有的組合,並對所有三個組合進行了更改,而不僅僅是我想要的組合。我無法弄清楚如何使這項工作。請參閱下面的代碼。我很感激幫助。用於Eclipse環狀組合的Java監聽器SWT

private void buildMultipleSatPulldowns() { 
    try { 
     FileReader fr = new FileReader("MultipleSatellites.txt"); 
     BufferedReader br = new BufferedReader(fr); 

     String line = null; 
     String[] tempS; 
     String constellation = null; 
     String satellite = null; 

     while ((line = br.readLine()) != null) { 
      tempS = line.split("~"); 
      constellation = tempS[4]; 
      satellite = tempS[6]; 

      constNameCombo = new Combo(satellitesComposite2, SWT.NONE); 
      constNameCombo.setToolTipText("Pulldown constellation name"); 
      constNameCombo.setBounds(startX + x2, 71, 125, 28); 
      constNameCombo.setItems(constNameArray); 
      constNameCombo.setText(constellation); 
      constNameCombos.add(constNameCombo); 

      constNameCombo.addModifyListener(new ModifyListener() { // captures changed combo values 
       public void modifyText(ModifyEvent arg0) { 
        setConstellationPD(); 
       } 
      }); 

      sPullDown(constellation); // builds the satellite array for the constellation and populates each pulldown 

      satNameCombo = new Combo(satellitesComposite2, SWT.NONE); 
      satNameCombo.setToolTipText("Pulldown satellite name"); 
      satNameCombo.setBounds(startX + x2, 106, 125, 28); 
      satNameCombo.setItems(satNameArray); 
      satNameCombo.setText(satellite); 
      satNameCombos.add(satNameCombo); 

      startX = startX + nextX; 

     } 

     br.close(); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

private void setConstellationPD() { 

    int constellations = 0; 

    for (Combo constNameCombo : constNameCombos) { 

     // What do I do here so that only the desired satNameCombo changes to reflect the desired pull down? 

     setSatellitesPD(constellations, constNameCombo) 

     constellations++; 
    } 

} 

private void setSatellitesPD(int c, String cN) { 
    int satellites = 0; 

    for (Combo satNameCombo : satNameCombos) { 
     if (c == satellites) { 
      satNameCombo.setText(satNameCombos.get(satellites).toString()); 
      satNameCombo.removeAll(); 
      sPullDown(cN); 
      satNameCombo.setText("select Satellite"); 
     } 
     satellites++; 
    } 

} 

private void sPullDown(String cName) { 
    // sPullDown takes the constellation name and returns a String Array of all objects in the constellation. This code works correctly when called. 
} 
+0

作爲一個方面說明,你應該看看使用[佈局](https://www.eclipse.org/articles/Article-Understanding-Layouts/Understanding-Layouts.htm)。如果您在具有不同字體大小的不同平臺上運行代碼,則使用setBounds會導致問題。 –

+0

我會檢查一下。謝謝。 –

回答

2

如果我理解正確,您需要一種方法來知道哪個組合觸發事件以影響其他組件。

SWT事件如ModifyEvent的方法有getSource(),它將返回發生事件的對象。

有了這些,您只需要正確識別它;例如,您可以簡單地使用constNameCombos.indexOf(eventCombo)來檢索其索引。

或者,更有效地,你可以用該方法setData附加一些數據到你的連擊和與getData檢索它的事件中,例如在循環內:

// "i" would be the index of the combo 
constNameCombo.setData("index", i); 
i++; 

和事件:

Combo eventCombo = (Combo) arg0.getSource(); 
int index = eventCombo.getData("index"); 

有了這些信息,您應該能夠識別您想要更改的其他組件。

+0

使用你給我的東西,我能夠根據需要使它工作。非常感謝你的幫助。 –