2011-06-13 2 views
2

나는 사용자가 모든 필드를 삽입하거나하지 않은 경우 어떻게 확인합니까 3 개 필드사용자가 JDialogBox에 무언가를 입력했는지 여부를 확인하는 방법은 무엇입니까?

public class Test{ 

    public static void main(String args[]) 

    { 

     JTextField firstName = new JTextField(); 

     JTextField lastName = new JTextField(); 

     JPasswordField password = new JPasswordField(); 

     final JComponent[] inputs = new JComponent[] { 

         new JLabel("First"), 

         firstName, 

         new JLabel("Last"), 

         lastName, 

         new JLabel("Password"), 

         password 

     }; 

     JOptionPane.showMessageDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE); 

     System.out.println("You entered " +firstName.getText() + ", " +lastName.getText() + ", " +password.getText()); 

    } 
} 

을 필요로하는 사용자 정의 대화 상자를 만들었습니다? 사용자 대화 상자를 닫더라도 그리고 사용자가 모든 삽입 된 경우 나 사용자 가까이 대화 상자

"You entered , , " 
+0

왜 필드에 'null'확인을 수행하지 않고 3 개의 필드 중 하나라도 null이면 모든 필드가 필요하다는 것을 나타내는 대화 상자를 다시 표시하십시오. – mre

+0

참조 : http://stackoverflow.com/questions/4608124/how-to-assign-a-sepecifc-action-to-the-cancel-button-within-joptionpane-show –

+0

감사합니다. 닫기 작업에 대해 – sanu

답변

1

을 dsplaying하지 않고도 확인할 수있는 경우 필드와 가까운 응용 프로그램에서 사용자의 입력을 확인 할

You entered , , 

표시 필드 여부를 다음과 같이

if(firstName.getText() != "" && lastName.getText() != "" && password.getText() != "") 
    System.out.println("All fields have been filled!"); 
else 
    System.out.println("Some fields are need to be filled!"); 

편집 :

myframe.addWindowListener(new WindowAdapter() 
{ 
    public void windowClosing(WindowEvent e) 
    { 
     JOptionPane.showMessageDialog(null,"You entered " + firstName.getText() + ", " + lastName.getText() + ", " +password.getText()); 
    } 
}); 

EDIT2 :

public class Test 
{ 
    public static void main(String args[]) 
    { 
     JTextField firstName = new JTextField(); 
     JTextField lastName = new JTextField(); 
     JPasswordField password = new JPasswordField(); 
     final JComponent[] inputs = new JComponent[] 
     { 
      new JLabel("First"), 
      firstName, 
      new JLabel("Last"), 
      lastName, 
      new JLabel("Password"), 
      password   
     }; 
     int i = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE); 
     if(i == 0) System.out.println("You entered " + firstName.getText() + ", " + lastName.getText() + ", " + password.getText()); 
    } 
} 
:

OK, 나는 지금 당신의 질문을 이해 생각이 시도 대화 상자를 닫은 후 메시지를 표시하기 위해

, 당신은 그것을 좋아 할 수

+0

OP가 찾고있는 것 같지 않습니다 ... – mre

+0

@mre : 적어도 이것은 OP 양식을 이해하는 것입니다 –

+0

@ Eng.Fouad, 어떻게 창 수신기를 ' JOptionPane' 대화 상자? – mre

1

각 데이터를 구분하는 다른 접근 방식을 제안합니다.

import javax.swing.JComponent; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 

public class Test { 

public static void main(String args[]) 
    { 
     JTextField firstName = new JTextField(); 
     JTextField lastName = new JTextField(); 
     JPasswordField password = new JPasswordField(); 
     final JComponent[] inputs = new JComponent[] { 
       new JLabel("First"), 
       firstName, 
       new JLabel("Last"), 
       lastName, 
       new JLabel("Password"), 
       password 
     }; 

     JOptionPane.showMessageDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE); 

     System.out.println("You entered '" + 
      firstName.getText() + "', '" + 
      lastName.getText() + "', '" + 
      //don't ignore deprecation warnings! 
      new String(password.getPassword()) + "'."); 
    } 
} 

그런 식으로, 당신은에서 누락되는 필드 (들)을 알 수 있습니다.

You entered '', 'johnson', ''. 
You entered 'john', '', ''. 
You entered '', '', 'johnsjohnson'. 
1

짧은 대답을 인쇄하기 전에 문자열에 남아있는 일이 있는지 확인합니다.

if(firstName.getText().equals("")) { 
    //Respond appropriately to empty string 
} else { 
    // Respond appropriately to non-empty string 
} 
관련 문제