Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9176347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:05:29+00:00 2026-06-17T17:05:29+00:00

I have this class, which should add a JLabel to a JPanel, but for

  • 0

I have this class, which should add a JLabel to a JPanel, but for some reason it isn’t doing that. Why is it not adding the JLabel to the JPanel? This takes place in my SwigWorker class in the done() method

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:05:30+00:00Added an answer on June 17, 2026 at 5:05 pm

    1- Change the layout manager for the notesPanel. GroupLayout really isn’t meant for hand coding like this. In my test, I used 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- Add a call to JComponent#validate after you’ve finished adding the labels.

    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();
    

    This should encourage the frame to re-layout it’s child components.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this class which extends an activity. But I want to draw
So I have this builder that runs a buildClass method which should be public,
I have this class which creates a grid: class GridPane extends JPanel{ public GridPane(int
I have this class which i would like to map: public class Contract {
I have this class which I want to pass around Windows as LPARAM parameter.
I have this class in which I run a for loop 10 times. This
I have a class which has implemented INotifyPropertyChanged . This class UserInfo has a
I'm trying to validate an ID. I have this class called ManejadorTickets in which
I have class A which extends the Activity class. This class is in package
have written this little class, which generates a UUID every time an object of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.