I have an application for which I need to marshall and unmarshall a list of computer parts in a JList to an XML file. The app has a JList on the left side with computer parts which are selected and transferred to the right side JList. I need a save() method that marshalls the list to an XML file. I also need a load() method that unmarshalls the saved XML file back into an object. I have searched on this site and elsewhere but the code I used to create the app does not quite match what I have seen for other solutions and I cannot resolve it. Here is the code I have:
public class Window {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Window() {
initialize();
}
/**
* Initialize the contents of the frame. This is where Window Builder
* will generate its code.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmLoad = new JMenuItem("Load");
mnFile.add(mntmLoad);
JMenuItem mntmSave = new JMenuItem("Save");
mnFile.add(mntmSave);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// This code makes the program close.
System.exit(0);
}
});
mnFile.add(mntmExit);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, 1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
// Here is where I populate the left panel with the list of computer parts.
final DefaultListModel<String> leftModel = new DefaultListModel<String>();
leftModel.addElement("Case");
leftModel.addElement("Motherboard");
leftModel.addElement("CPU");
leftModel.addElement("GPU");
leftModel.addElement("PSU");
leftModel.addElement("RAM");
leftModel.addElement("HDD");
final JList<String> leftList = new JList<String>(leftModel);
leftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
GridBagConstraints gbc_leftList = new GridBagConstraints();
gbc_leftList.gridwidth = 4;
gbc_leftList.gridheight = 6;
gbc_leftList.insets = new Insets(0, 0, 5, 5);
gbc_leftList.fill = GridBagConstraints.BOTH;
gbc_leftList.gridx = 0;
gbc_leftList.gridy = 0;
frame.getContentPane().add(leftList, gbc_leftList);
final DefaultListModel<Object> rightModel = new DefaultListModel<Object>();
final JList<Object> rightList = new JList<Object>(rightModel);
GridBagConstraints gbc_rightList = new GridBagConstraints();
gbc_rightList.gridheight = 6;
gbc_rightList.gridwidth = 4;
gbc_rightList.insets = new Insets(0, 0, 5, 5);
gbc_rightList.fill = GridBagConstraints.BOTH;
gbc_rightList.gridx = 5;
gbc_rightList.gridy = 0;
frame.getContentPane().add(rightList, gbc_rightList);
JButton button = new JButton(">>");
/**
* The 'for' statement below transfers the selected values from
* the left to the right JList.
* It includes an 'if' statement to avoid moving a value from
* the left if it is already in the right JList.
*/
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(Object selectedValue:leftList.getSelectedValuesList()){
if(!rightModel.contains(selectedValue)) {
rightModel.addElement(selectedValue);
}
}
}
});
GridBagConstraints gbc_button = new GridBagConstraints();
gbc_button.insets = new Insets(0, 0, 5, 5);
gbc_button.gridx = 4;
gbc_button.gridy = 2;
frame.getContentPane().add(button, gbc_button);
JButton button_1 = new JButton("<<");
// This code deletes a selected value from the right JList.
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(Object selectedValue:rightList.getSelectedValuesList()){
rightModel.removeElement(selectedValue);
}
}
});
GridBagConstraints gbc_button_1 = new GridBagConstraints();
gbc_button_1.insets = new Insets(0, 0, 5, 5);
gbc_button_1.gridx = 4;
gbc_button_1.gridy = 3;
frame.getContentPane().add(button_1, gbc_button_1);
}
I also have a Parts class annotated for JAXB, but I cannot determine how to use it in the save() or load () methods:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Parts {
@XmlElement(name = "part")
private List<String> part;
public List<String> getPart() { return part; }
public void setPart(List<String> part) { this.part = part; }
}
Can anyone help with this?
No comments:
Post a Comment