2014-12-08 2 views
0

자바로 브라우저를 만들려고합니다. 여기에는 JProgressBar, JTextField 및 JEditorPane 인 이 포함되어 있지만 프로그램을 실행했을 때 문제가 있음을 발견했습니다. 편집기 창이 페이지를 검색 할 때 JProgressBar가 작동하지 않습니다. JProgressBar가 완료되면Java에서 JProgressBar를 사용하여 브라우저를 만드는 방법은 무엇입니까?

String az = jTextField1.getText(); 


     if(az.contains("1")){ 
      String hh = WorkSpace.jTextField1.getText(); 


    try { 
     WorkSpace.jEditorPane1.setPage("" + hh); 
     WorkSpace.jProgressBar1.setValue(); // which value? 

    } catch (Exception e) { 

    } 

그래서 편집기 창 페이지 설정해야합니다

나는이 코드를 시도했습니다?

어떻게 할 수 있습니까?

+0

'setPage'는 페이지가 편집기에 의해로드 될 때까지 블로킹 메소드입니다. ... – MadProgrammer

+0

@MadProgrammer 그래서! 해결책은 무엇입니까? – Ahmed

+0

아, 이제는 이해합니다 ... 아마 EditorPane에 DocumentListener를 추가하고 들어오는 Text를 수신 한 다음 진행률 막대를 업데이트 할 수 있습니다. 그러나 당신은 어쩌면 얼마나 많은 문자가 예상되는지 알 수 없으므로 이산 값으로 JProgressBar를 업데이트 할 수 없습니다. –

답변

1

원하는 동작을 얻으려면 편집기 창에 URL을 설정하고 진행 막대가있는 브라우저가 있어야합니다. brwoser 코딩은 고통스럽고 복잡한 작업이며 JEditorPane입니다. 가능한 모든 가능성을 다루고 있습니다.

그러나 귀하의 필요를 충족시키기 위해 귀하가 직접 (소켓 또는 httpclient 또는 다른 lib를 사용하여) 표시하려고하는 페이지의 내용을 가져와야한다고 생각합니다. 서버에서 바이트를 수신하는 동안 진행 막대를 업데이트 할 수 있습니다. 모든 바이트를받은 후에 한 단계에서 내용을 표시 할 창으로 내용을 설정합니다.

편집 : 사용하여 소켓 당신은 (이 신속하고 더러운는 오류 처리의 어떤 종류없이 참고) 다음을 수행해야합니다 꽤 많이

// Suppose you want to display http://www.target.com/page 
Socket s = new Socket("target.com", 80); 
PrintWriter out = new PrintWriter(s.getOutputStream()); 
// Tell the server you want to get "/page" 
out.println("GET /page HTTP/1.1"); 
out.println("Host: target.com"); 
out.println(); 

// The target-Server now send you the content of "/page" 
// Now you need to know a little about the HTTP-Protocol. 
// In short: The server sends you a header and a body. 
// The header and the body is separated using two newlines. 
// You need to read line by line from the server until the 
// body starts and interpret the stuff from the header because it 
// contains the information how many bytes you will receive with the body 
// (-> Content-Length: xyz) 
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 

// Read the Header and interpret that stuff. 
doReadHeader(in); 

// Now, because of the Content-Length Header you know how many bytes you need to read 
// from the InputStream until all the bytes are receive. Thus you can update your 
// progressbar while receiving the bytes 
doReadBody(in); 

즉. 이 모든 것을받은 후에 EditorPane에 본문 1 : 1을 설정할 수 있습니다. 그러나 EditorPane은 HTML과 CSS를 거의 다루지 않습니다. 그래서 FlyingSaucer 나 CSSBox와 같은 다른 HTML 창으로 가야 할 수도 있습니다 ...

+0

죄송합니다! 어떤 코드를 주시거나 더 설명해 주시겠습니까? – Ahmed

+0

아이디어를 제공하기 위해 내 게시물을 편집했습니다. –

+0

고맙습니다. @BlackEye – Ahmed

관련 문제