2015-09-24 66 views
0

我有一個JCombobox在運行時填充,並基於窗體上的其他操作重新填充。問題是我只想通過用戶鼠標/鍵盤單擊來觸發actionListener。不幸的是,actionListener是由程序清除/加載組合框觸發的。如何在組合框actionListener中僅包含用戶操作?

我試圖規避這個問題是爲了檢查控件是否有焦點,這將是用戶直接操作它的唯一方式,而當其他控件清除/加載它的內容時將永遠不會出現這種情況。不幸的是,hasFocus()部分總是返回null。

這裏是我的形式簡裝例如,只包含問題:

package newProj; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.DefaultComboBoxModel; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JLayeredPane; 
import javax.swing.JTabbedPane; 

public class hasFocusTest { 

    private JFrame frame; 
    public static String DBurl; 
    private JComboBox<?> cmbEquipSpec; 

    public static void main(String[] args) { 
     hasFocusTest window = new hasFocusTest(); 
     window.frame.setVisible(true); 
    } 

    public hasFocusTest() { 
     initialize(); 
    } 

    public static void populateSpec(DefaultComboBoxModel<String> Speclist) { 

     Speclist.removeAllElements(); 
     Speclist.addElement("1"); 
     Speclist.addElement("2"); 
     Speclist.addElement("3"); 
    } 

    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 280, 200); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 
     this.frame.setFocusable(false); 

     JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); 
     tabbedPane.setBounds(10, 11, 240, 140); 
     frame.getContentPane().add(tabbedPane); 

     JLayeredPane p_1 = new JLayeredPane(); 
     tabbedPane.addTab("Define Function", null, p_1, null); 

     DefaultComboBoxModel<String> Speclist = new DefaultComboBoxModel<String>(); 
     populateSpec(Speclist); 

     cmbEquipSpec = new JComboBox<String>(Speclist); 
     cmbEquipSpec.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

       boolean sFocus = false; 

       try { 
         sFocus = cmbEquipSpec.hasFocus(); 
        } catch (NullPointerException e) { 

       } 

       System.out.println(sFocus); 
       System.out.println("cmbEquipSpec: " + cmbEquipSpec); 
      } 
     }); 
     cmbEquipSpec.setEditable(true); 
     cmbEquipSpec.setBounds(10, 31, 64, 20); 
     p_1.add(cmbEquipSpec); 

     JLabel lblEquipSpec = new JLabel("Equip Spec"); 
     lblEquipSpec.setBounds(15, 11, 74, 14); 
     p_1.add(lblEquipSpec); 
     p_1.setVisible(true); 
    } 
} 

如果我改變的中部到:

Component sFocus = null; 

try { 
     sFocus = frame.getFocusOwner(); 
    } catch (NullPointerException e) { 

} 

然後它告訴我,與組件焦點明顯不同於被點擊的焦點。

組件點擊: javax.swing.JComboBox[,10,31,64x20,invalid,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,isEditable=true,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=3]

重點組件: javax.swing.plaf.metal.MetalComboBoxEditor$1[,0,0,44x20,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,[email protected]d04923d,flags=8388904,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=9,columnWidth=0,command=,horizontalAlignment=LEADING]

我很新的這一點,是沒有意義的我,爲什麼組合框,我點擊是不是一個有焦點。但是,重新迭代,這是我的解決方案。如果有一種方法可以從actionListener中排除程序動作,那麼這也可以實現我的目標。我相信。

+1

'不幸的是,的ActionListener被觸發通過程序清除/加載combobox.' - 然後刪除ActionListener,然後清除/加載組合框,並在加載值後添加偵聽器。另外,不要使用空佈局。 Swing旨在與佈局經理一起使用。 – camickr

+0

我沒考慮過!我會試試看。至於'frame.getContentPane().setLayout(null);',Eclipse自動執行,所以我沒有問題。我會研究這方面,非常感謝你。 –

+0

工作,經過一些故障排除。如果你做出答案,我會標記它。 –

回答

1

不幸的是,actionListener是由程序清除/加載組合框觸發的。

  1. 從下拉列表框
  2. 明確刪除的ActionListener並重新加載組合框的項目
  3. 添加的ActionListener回到組合框
相關問題