2016-06-14 2 views
0

내 애플리케이션에서 사용자가 입력 할 때 JOptionPane.showOptionDialog를 자주 표시해야 할 필요가 있습니다.사용자 정의 패널을 사용하는 JOptionPane.showOptionDialog가 제대로 작동하지 않습니다.

가끔 표시되지 않고 사용자 입력 및 정지를 기다립니다.

여기에 동일한 코드를 재현하기 위해 샘플 코드를 첨부했습니다.

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridBagLayout; 

import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.JTextPane; 
import javax.swing.ToolTipManager; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DefaultStyledDocument; 
import javax.swing.text.Style; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyleContext; 
import javax.swing.text.StyledDocument; 

public class TestJOptionPane { 

public static void main(String[] args) { 

    for (int index = 0; index < 20; index++) { 
     String serverMessage = "Test question message index = "+index; 
     String hintText = "Test question hint index = "+index; 

     JPanel mainPanel = new JPanel(new BorderLayout(0,4)); 
     JPanel answerPanel = new JPanel(new GridBagLayout()); 

     StyleContext context = new StyleContext(); 
     StyledDocument document = new DefaultStyledDocument(context); 

     Style style = context.getStyle(StyleContext.DEFAULT_STYLE); 
     StyleConstants.setFontFamily(style, "Dialog"); 
     StyleConstants.setFontSize(style, 12); 
     StyleConstants.setBold(style, true); 

     try { 
      document.insertString(document.getLength(), serverMessage, style); 
     } catch (BadLocationException badLocationException) { 
      System.out.println(badLocationException.getMessage()); 
     } 

     JTextPane questionTA = new JTextPane(document); 
     questionTA.setEditable(false); 
     questionTA.setOpaque(false); 

     final JTextField answerField = new JTextField(); 
     answerField.setPreferredSize(new Dimension(410,23)); 
     answerField.requestFocus(); 

     JLabel hint = new JLabel(); 

     ToolTipManager.sharedInstance().setDismissDelay(10000); 
     hint.setText("Hint"); 
    // hint.setIcon(Utility.getIcon("HintQuestion.png")); 

     answerPanel.add(answerField); 
     answerPanel.add(new JPanel().add(hint));       
     answerPanel.setPreferredSize(new Dimension(450, 30)); 
     mainPanel.add(questionTA,BorderLayout.NORTH); 
     mainPanel.add(answerPanel,BorderLayout.SOUTH); 

     hint.setToolTipText(hintText); 

     String[] options = {"OK"}; 
     JOptionPane.showOptionDialog(null, mainPanel, "Test showOptionDialog", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 
    } 
} 
} 

오/P는

"테스트 질문 메시지 인덱스 = 0"을 클릭 OK

"테스트 질문 메시지 인덱스 = 1"랜덤에서 OK

을 클릭

같이 표시 , JOptionPane.showOptionDialog는 표시되지 않습니다.

누구든지 나를 도울 수 있습니다! 미리 감사드립니다.

+1

[* 초기 스레드 *] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)를 참조하십시오. – trashgod

답변

2

다음과 같이 코드를 변경해야합니다.

 try { 
      SwingUtilities.invokeAndWait(new Runnable() { 

       @Override 
       public void run() { 
        int result = JOptionPane.showOptionDialog(null, mainPanel, "Test showOptionDialog", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 
       } 
      }); 
     } catch (InvocationTargetException e) { 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
+0

귀하의 회신에 감사드립니다 베니 튼. 난의 invokeAndWait를 사용하는 경우 사실 내 방법은 공공 개체 SendResponse처럼 보이는 (문자열 상태, 문자열 SERVERMESSAGE, 문자열 FUNCTIONNAME, 문자열 hintText)는 RemoteException을 던졌습니다 { // 위의 코드와 서버 에 사용자 입력을 반환}는 , 그것은이다 별도의 스레드를 누른 다음 어떻게 호출자 메서드에 answerField.getText()를 반환합니까? – Tamil

+1

@Lokesh ObserverPattern을 구현할 수 있습니다. 텍스트 필드를 관찰 가능하게 만들고 관찰자에게 userInput을 반환 할 때 알림을 받아야합니다. – Beniton