버튼을 클릭 할 때 긴 작업을 실행하고 있습니다. 작업이 시작되었다는 메시지를 보여주고 싶습니다. swingworker를 사용하면 (자), JOptionPane는 메세지 박스를 작성 합니다만, 그 내용은 태스크가 완료 할 때까지 아무것도 공백입니다. 내 EDT가 차단되고 GUI가 작업이 완료되지 않으면 업데이트되지 않는다고 생각합니다.SwingWorker thread-doInBackground()에서 메시지 표시 (Joptionpane)
public class myClass {
private JFrame frame;
private display1 dis;
class display1 extends SwingWorker<Void,Void>
{
public Void doInBackground() throws InterruptedException
{
JOptionPane.showMessageDialog(null,
"Task Started");
return null;
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
myClass window = new myClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public myClass() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dis=new display1();
dis.execute();
System.out.println("starting");
for(int i=0;i<10000;i++)
System.out.println("this is " +i);// Long task
System.out.println("Finished");
}
});
btnNewButton.setBounds(166, 228, 89, 23);
frame.getContentPane().add(btnNewButton);
}
}
'display1'은 무엇입니까? EDT 컨텍스트에서 UI가 비어있는 이유를 설명하는 긴 작업을 실행 중입니다. – MadProgrammer
display1은 SwingWorker를 구현하는 클래스의 이름입니다. 나는 그것이 작동하지 않는 이유를 안다. 내가 묻는 것은 작업이 시작되고 동시에 작업을 실행할 수있는 대화 상자를 표시하는 방법입니다 (이 작업에는 특정 GUI 구성 요소가 포함되어 있습니다) – sam
[실행 가능한 예제] (https://stackoverflow.com/help/mcve) 제공을 고려하십시오.) 귀하의 문제를 보여줍니다. 이것은 코드 덤프가 아니지만 수행중인 문제의 예를 보여줍니다. 이렇게하면 더 적은 혼란과 더 나은 응답을 얻을 수 있습니다. – MadProgrammer