2013-01-21 12 views
1

이 클래스는 JPanel에 JLabel을 추가해야하지만, 어떤 이유로 그것을 수행하지 않습니다. JPanel에 JLabel을 추가하지 않는 이유는 무엇입니까? 이것은 done() 방법JPanel에 JLabel이 추가되지 않았습니다.

package sscce; 

import java.io.IOException; 
import java.io.StringReader; 
import java.io.StringWriter; 
import java.util.ArrayList; 
import javax.swing.JLabel; 
import javax.swing.SwingWorker; 
import javax.swing.UIManager; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.OutputKeys; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import org.w3c.dom.CharacterData; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

public class sscce extends javax.swing.JFrame{ 
    protected XML xml = new XML(); 

    public sscce(){ 
     initComponents(); 
     GetNotes getNotes = new GetNotes(); 
     getNotes.execute(); 
    } 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     notesPanel = new javax.swing.JPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout notesPanelLayout = new javax.swing.GroupLayout(notesPanel); 
     notesPanel.setLayout(notesPanelLayout); 
     notesPanelLayout.setHorizontalGroup(
      notesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 494, Short.MAX_VALUE) 
     ); 
     notesPanelLayout.setVerticalGroup(
      notesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 433, Short.MAX_VALUE) 
     ); 

     jScrollPane1.setViewportView(notesPanel); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE) 
     ); 

     pack(); 
    }// </editor-fold> 

    public static void main(String args[]){ 
     try{ 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     }catch( ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex){ 
      java.util.logging.Logger.getLogger(sscce.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 

     java.awt.EventQueue.invokeLater(new Runnable(){ 
      public void run(){ 
       new sscce().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JPanel notesPanel; 
    // End of variables declaration 

    public class GetNotes extends SwingWorker{ 

     protected ArrayList<XML> notes; 

     @Override 
     public void done(){ 
      System.out.println(notes.size()); 
      for(XML note : notes){ 
       String n = note.getNode("notes", "note"); 
       System.out.println(n); 
       JLabel lbl = new JLabel(n); 
       lbl.setVisible(true); 
       notesPanel.add(lbl); 
      } 
     } 

     @Override 
     public String doInBackground(){ 
      String xmlStr = "<?xml version=\"1.0\"?>" 
        + "<root>" 
        + " <success>true</success>" 
        + " <notes>" 
        + "  <note>Note 1</note>" 
        + " </notes>" 
        + " <notes>" 
        + "  <note>Note 2</note>" 
        + " </notes>" 
        + " <notes>" 
        + "  <note>Note 3</note>" 
        + " </notes>" 
        + "</root>"; 
      xml.parse(xmlStr); 
      notes = xml.getNodes("root", "notes"); 
      return null; 
     } 
    } 
} 

class XML{ 

    private Document doc; 

    public XML(){ 
    } 

    public void parse(String xml){ 
     try{ 
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
      InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(xml)); 

      doc = db.parse(is); 
     }catch(ParserConfigurationException | SAXException | IOException ex){ 
      //Logger.getLogger(XML.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    public Document getDoc(){ 
     return this.doc; 
    } 

    public ArrayList<XML> getNodes(String root, String name){ 
     ArrayList<XML> elList = new ArrayList<>(); 
     NodeList nodes = doc.getElementsByTagName(root); 
     for(int i = 0; i < nodes.getLength(); i++){ 
      Element element = (Element)nodes.item(i); 
      NodeList nl = element.getElementsByTagName(name); 
      for(int c = 0; c < nl.getLength(); c++){ 
       Element e = (Element)nl.item(c); 
       String xmlStr = this.nodeToString(e); 
       XML xml = new XML(); 
       xml.parse(xmlStr); 
       elList.add(xml); 
      } 
     } 
     return elList; 
    } 

    private String nodeToString(Node node){ 
     StringWriter sw = new StringWriter(); 
     try{ 
      Transformer t = TransformerFactory.newInstance().newTransformer(); 
      t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 
      t.transform(new DOMSource(node), new StreamResult(sw)); 
     }catch(TransformerException te){ 
      System.out.println("nodeToString Transformer Exception"); 
     } 
     return sw.toString(); 
    } 

    public String getNode(String root, String name){ 
     NodeList nodes = doc.getElementsByTagName(root); 
     for(int i = 0; i < nodes.getLength(); i++){ 
      Element element = (Element)nodes.item(i); 

      NodeList n = element.getElementsByTagName(name); 
      Element e = (Element)n.item(0); 
      return getCharacterDataFromElement(e); 
     } 
     return ""; 
    } 

    public static String getCharacterDataFromElement(Element e){ 
     Node child = e.getFirstChild(); 
     if(child instanceof CharacterData){ 
      CharacterData cd = (CharacterData)child; 
      return cd.getData(); 
     } 
     return ""; 
    } 
} 
+1

'notesPanel'에 대한 레이아웃 관리자를'GroupLayout' 이외의 다른 것으로 변경하는 것으로 시작하십시오.이 레이아웃 관리자는 실제로 손으로 처리 할 의도가 없습니다. – MadProgrammer

+0

나는 시도 : Gridbag, 흐름, 상자, 그리고 그들 중 누구도 일한 적이 없다. –

+0

내 대답을 참조하십시오 또한 실행중인 예제 +1, 감사합니다 – MadProgrammer

답변

2

1 변경 notesPanel의 레이아웃 매니저에 내 SwigWorker 수업 시간에 일어난다. GroupLayout은 이와 같이 손으로 코딩하는 것을 의미하지 않습니다. 내 테스트에서는 FlowLayout을 사용했습니다.

//  javax.swing.GroupLayout notesPanelLayout = new javax.swing.GroupLayout(notesPanel); 
//  notesPanel.setLayout(notesPanelLayout); 
//  notesPanelLayout.setHorizontalGroup(
//     notesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
//    .addGap(0, 494, Short.MAX_VALUE)); 
//  notesPanelLayout.setVerticalGroup(
//     notesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
//    .addGap(0, 433, Short.MAX_VALUE)); 
     notesPanel.setLayout(new FlowLayout()); 

2- 레이블 추가를 마친 후 JComponent#validate에 전화를 겁니다.

System.out.println(notes.size()); 
for (XML note : notes) { 
    String n = note.getNode("notes", "note"); 
    System.out.println(n); 
    JLabel lbl = new JLabel(n); 
    lbl.setVisible(true); 
    notesPanel.add(lbl); 
} 
notesPanel.validate(); 

이렇게하면 프레임에서 하위 구성 요소를 다시 레이아웃해야합니다.

+0

Ahh! 감사! 나는 그것을 검증 할 필요가 있다는 것을 잊었다. –

관련 문제