2016-05-31 13 views
1

keyTypedメソッドのifチェックの内側には貸し付けません。コードのfollowiing部分があります:編集可能か確認するjcombobox

ここ

私はコンボボックスを初期化:

私が入力したかどうか確認この時点で
private void initComponents() 
{ 
    this.cboDayModel = new DefaultComboBoxModel<ListItem>(); 
    this.cboDay = new JComboBox<ListItem>(this.cboDayModel); 
    this.cboDay.addItemListener(this); 
    this.cboDay.setName("cboDay"); 
    this.cboDay.setBackground(this.einAusClass.hPHB.btnColor); 
    this.cboDay.setEditable(true); 
    this.cboDay.getEditor().getEditorComponent().addKeyListener(this); 
    this.cboDay.getEditor().getEditorComponent().addFocusListener(this); 
    this.add(this.cboDay); 
} 

:keyTypedに方法は

@Override 
    public void keyTyped(KeyEvent e) 
    { 
    if (!(Character.isDigit(e.getKeyChar()))) 
    { 
     e.consume(); 
     return; 
    } 
    if (e.getSource() instanceof JComboBox) // <-------************* 
    { 
     System.out.println("zz2"); 
     this.cbo = (JComboBox<ListItem>) e.getSource(); 

     String str = ((JTextField) cbo.getEditor(). 
          getEditorComponent()).getText(); 
     int zahl = Integer.parseInt("0" + str); 
     System.out.println(str + "" + zahl); 
     if (this.cbo == cboDay) 
     { 
     if (zahl < 1 || zahl > 31) 
     { 
      e.consume(); 
      return; 
     } 
     } 
    } 
} 

私は

をチェックしますか
if (e.getSource() instanceof JComboBox) 

なぜこのif文の中に入りませんか?

e.getSource()

答えて

0

これは)メソッドgetSourceのメソッド宣言(あるhttps://docs.oracle.com/javase/7/docs/api/java/util/EventObject.htmlに従ってJComboBoxobjectを返しません。 public Object getSource()これはEventが最初にあなたが実際にjcombobox元のオブジェクトに e.getSourceをキャストする必要がありますので、発生したobject返します。

cboDay.setActionCommand("combo"); 

public void keyTyped(KeyEvent e) 
    String action = e.getActionCommand(); 
     if (action.equals("combo")) { 
     System.out.println("done!"); 
     } 
} 
actioncommand EXを使用して

if(cboDay == (jcombobox)e.getSource()) 

あなたは同じ機能を実現できます

関連する問題