?
Solved

Xml data to JList

Posted on 2010-01-09
8
Medium Priority
?
1,060 Views
Last Modified: 2013-11-23
Hi,

I just want to display xml Data in a JList.

Here is what i have done (it does not display the data itself but its address (sth like this: [Ljava.lang.Object;@1ac04e8) ):

// my xml file is ok !


package tamSoft;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ListTest extends JPanel {

	static Element racine;

	static org.jdom.Document document;
	DefaultListModel listModel = new DefaultListModel();
	JList list = new JList(listModel);

	public ListTest() {
		setLayout(new BorderLayout());

		SAXBuilder sxb = new SAXBuilder();
		try {

			document = sxb.build(new File("test.xml"));
		} catch (Exception e5) {
		}

		racine = document.getRootElement();

		List listJob = racine.getChildren("job");

		Iterator i = listJob.iterator();

		Object[] data = new Object[listJob.size()];

		int cpt = 0;

		while (i.hasNext()) {

			Element courant = (Element) i.next();

			Object[] d2 = { courant.getChild("customer").getChild("name")
					.getText() };

			data[cpt] = d2;
			cpt++;
		}
		listModel.addElement(data);

		JButton button = new JButton("Print");
		JScrollPane pane = new JScrollPane(list);

		DefaultListSelectionModel m = new DefaultListSelectionModel();
		m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		m.setLeadAnchorNotificationEnabled(false);
		list.setSelectionModel(m);

		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				System.out.println(e.toString());
			}
		});
		button.addActionListener(new PrintListener());

		add(pane, BorderLayout.NORTH);
		add(button, BorderLayout.SOUTH);
	}

	public static void main(String s[]) {
		JFrame frame = new JFrame("List Example");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(new ListTest());
		frame.pack();
		frame.setVisible(true);
	}

	// An inner class to respond to clicks on the Print button
	class PrintListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int selected[] = list.getSelectedIndices();
			System.out.println("Selected Elements:  ");

			for (int i = 0; i < selected.length; i++) {
				String element = (String) list.getModel().getElementAt(
						selected[i]);
				System.out.println("  " + element);
			}
		}
	}
}

Open in new window

0
Comment
Question by:Dr_p
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 26281700
Not that I expect this to be the main reason of your problem, but I would replace these lines:

>>                DefaultListSelectionModel m = new DefaultListSelectionModel();
>>                m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
>>                m.setLeadAnchorNotificationEnabled(false);
>>                list.setSelectionModel(m);

by just these:

DefaultListSelectionModel m = (DefaultListSelectionModel) list.getSelectionModel();
m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
m.setLeadAnchorNotificationEnabled(false);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 26281721
Concerning the real problem:

I would remove this line:

>> listModel.addElement(data);

and replace it by this line inside the loop:

listModel.addElement(d2);

So that becomes:
while (i.hasNext()) {

    Element courant = (Element) i.next();

    Object[] d2 = { courant.getChild("customer").getChild("name").getText() };
    listModel.addElement(d2);
    // data[cpt] = d2;  //<<<<<<  I think you don't need that data array anymore
    cpt++;
}

Open in new window

0
 

Author Comment

by:Dr_p
ID: 26287805
thanks for the reply, but i still get the same result :  [Ljava.lang.Object;@765291
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:zzynx
ID: 26290940
Please provide
* your test.xml in attachment
* once again your changed program
0
 

Author Comment

by:Dr_p
ID: 26291511
Here in attached my xml file:
test.xml
0
 

Author Comment

by:Dr_p
ID: 26291519
and my changed program:
listTest2.java
0
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
ID: 26292399
I have problems to make it run due to the jdom stuff I miss.
But anyway, I know why you get that [Ljava.lang.Object;@765291

Try this:
Replace

Object[] d2 = { courant.getChild("customer").getChild("name").getText() };
listModel.addElement(d2);

by

String customerName = courant.getChild("customer").getChild("name").getText();  // I suppose getText() returns a String
listModel.addElement(customerName);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 26296830
Thanx 4 axxepting
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question