2016-07-13 8 views
0

내 응용 프로그램은 별도의 스레드에서 JOptionPane 대화 상자를 열 것입니다 타사 독립 실행 형 응용 프로그램과 통합되어 열려있는 모든 대화 상자를 닫으려면 스레드를 실행하고 있습니다. 닫기 전에 내가 얻을 필요가 대화 상자에 쓰여진 메시지.프로그래밍 방식으로 JOptionPane 메시지 내용을 얻는 방법

내가 달성하기 위해 노력하고있는

내 샘플 메인 프로그램 :

public static void main(String[] args)throws Exception{ 
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); 
    executor.scheduleAtFixedRate(() -> { 
     Window[] possibleWindow = Window.getWindows(); 
     if (possibleWindow != null && possibleWindow.length > 0) { 
      System.out.println("Found " + possibleWindow.length + "Window(s) " + possibleWindow[0].getClass().getSuperclass()); 
      for (int i = possibleWindow.length - 1; i >= 0; i--) { 
       try { 
        Window window = possibleWindow[i]; 
        //here where I need to get the dialog box message before closing it. 
        window.dispose(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }, 1, 1, TimeUnit.SECONDS); 

    JOptionPane.showMessageDialog(null, "test !!!!"); 
} 
+0

당신이 possibleWindow 배열의 모든 참조를위한 JOptionPane의 인스턴스가 될 것이라고 확신? 그렇다면 응용 프로그램에서 Extension (직접 확장 또는 캡슐화에 의해) JOptionPane이 생성 될 때 창 내용을 등록하는 새 클래스를 만드는 것이 더 합리적입니까? 그렇게하면 결국 청소를하려고하지 않을 것입니다. – Teto

+1

방금 ​​확인한 바에 따르면 JOptionPane은 Window의 하위 클래스가 아닙니다. 그래서 나는 그것이 가능한 윈도우 배열에 어떻게 나타나는지를 보지 못했다. 그러나 다른 방법으로 기존 JOptionPane에 대한 참조를 얻을 수 있다면 JOptionPane.getMessage()를 호출하여 메시지 객체를 가져올 수 있습니다. – Teto

+0

내 응용 프로그램은 타사 스윙 독립형 응용 프로그램과 통합되어 있으며 메시지 대화 상자를 표시하는 JOptionPane 객체를 만들지 않았습니다. –

답변

1

를 내가 제대로 질문을받을 경우, 상자의 JOptionPane의 객체와 그들에게 메시지를 줄; 나중에 당신이 그들에게 준 메시지를 알고 싶습니까?

그렇다면 간단한 해결책은 Map<JOptionPane, String>과 같은 중앙지도를 만드는 것입니다. 새 JOptionPane을 만들 때마다 그 JOptionPane을 기억합니다. 그리고 정리시; 아직 올려지고있는 JOptionPane 객체의 메세지를 간단하게 꺼냅니다.

+0

내 응용 프로그램이 타사 스윙 독립 실행 형 응용 프로그램과 통합되어 있으며 메시지 대화 상자를 표시하는 JOptionPane 객체를 만들지 않았습니다. –

0

윈도우의 모든 구성 요소를 재귀 적으로 필요로합니다. 이 솔루션은 귀하의 경우에 작동합니다

public static String getMess(Container w){    
    for (Component component : w.getComponents()) { 
     if (component instanceof JLabel) { 
      return ((JLabel) component).getText(); 
     } 
     else if (component instanceof JTextField){ 
      return ((JTextField) component).getText(); 
     } 
     else if (component instanceof Container){ 
      String s = getMess((Container) component); 
      if (!s.isEmpty()){ 
       return s; 
      } 
     } 
    } 
    return ""; 
} 
+0

해결책을 시도했지만 저를 위해 작동하지 않았습니다. 필자는 샘플 메인 메소드 프로그램을 사용해 보았습니다. –

0

이 솔루션은 나를 위해 일한 :

if (window instanceof JDialog) { 
     System.out.println("text : " + ((JOptionPane)((JDialog) window).getContentPane().getComponents()[0]).getMessage()); 
    }