Dr_p
asked on
Xml data to JList
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;@1ac04e 8) ):
// my xml file is ok !
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;@1ac04e
// 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);
}
}
}
}
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:
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++;
}
ASKER
thanks for the reply, but i still get the same result : [Ljava.lang.Object;@765291
Please provide
* your test.xml in attachment
* once again your changed program
* your test.xml in attachment
* once again your changed program
ASKER
Here in attached my xml file:
test.xml
test.xml
ASKER
and my changed program:
listTest2.java
listTest2.java
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanx 4 axxepting
>> DefaultListSelectionModel m = new DefaultListSelectionModel(
>> m.setSelectionMode(ListSel
>> m.setLeadAnchorNotificatio
>> list.setSelectionModel(m);
by just these:
DefaultListSelectionModel m = (DefaultListSelectionModel
m.setSelectionMode(ListSel
m.setLeadAnchorNotificatio