Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2006

Tip: Looking for answers? Try searching our database.

Create a bunch of jCheckboxes based off an XML file?

Thread view: 
henderjm@gmail.com - 03 Aug 2006 23:02 GMT
Does anyone know how to create a bunch of jCheckboxes based off of some

XML file?  I'm trying to add some checkboxes in a jPanel, but can't
figure out how.
The XML file would probably look similar to:
<?xml version="1.0"?>
<SAMPLES>
<SAMPLE ID="1">
   <Name>Some Name</Name>
   <Description>Some Descr</Description>
   <Additional_Info>Some info</Additional_Info>
   <Warning_Level>0 through 5</Warning_Level>
   <Warning_Description>Some Warning description</Warning_Description>

 </SAMPLE>
  <SAMPLE ID="2">
   <Name>Some Other Name</Name>
   <Description>Some Other Descr</Description>
   <Additional_Info>Some Other info</Additional_Info>
   <Warning_Level>0 through 5</Warning_Level>
   <Warning_Description>Some Other Warning
description</Warning_Description>
 </SAMPLE>
</SAMPLES>
Michael Rauscher - 04 Aug 2006 02:38 GMT
henderjm@gmail.com schrieb:
> Does anyone know how to create a bunch of jCheckboxes based off of some
>
> XML file?  I'm trying to add some checkboxes in a jPanel, but can't
> figure out how.

With two sentences, you've mentioned three problems and my crystal ball
is unable to tell me which is the one you want to have a solution for:

a) how to process an XML file
b) how to create instances of JCheckBox
c) how to add instances of JCheckBox to a container

So, below is a small (quick & dirty) example that creates instances of
JCheckBox based on the text content of an XML document's "Name" nodes.
The document is considered to be stored in a file "file.xml".

import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class CheckBoxFactory {
    private Document doc;

    public CheckBoxFactory( InputStream xmlStream ) throws
            ParserConfigurationException, org.xml.sax.SAXException,
            IOException {
        DocumentBuilderFactory builderFactory =
                DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        doc = builder.parse( xmlStream );
    }

    public List<JCheckBox> getCheckBoxes() {
        NodeList nodes = doc.getElementsByTagName("Name");
        int n = nodes.getLength();

        List<JCheckBox> result = new ArrayList<JCheckBox>(n);

        for ( int i = 0; i < n; i++ ) {
            Element name = (Element)nodes.item(i);
            result.add( new JCheckBox(name.getTextContent()) );
        }

        return result;
    }

    public static final void main( String args[] ) throws Exception {
        CheckBoxFactory factory = new CheckBoxFactory(
                new FileInputStream("file.xml") );

        JPanel panel = new JPanel( new GridLayout(0,1) );
        for ( JCheckBox cb : factory.getCheckBoxes() )
            panel.add( cb );

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.getContentPane().add( panel, BorderLayout.NORTH );
        frame.pack();
        frame.setVisible( true );
    }
}

Bye
Michael


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.