Sorry I printed out a response and I don't think it was sent
properly so I'll try again....
Thanks for your replies but I am still not getting it to work right.
Basically the elements are being added but are being added to the
side of the form instead of where the JList is.
In IntelliJ you create the form in the GUI editor and then bind the
elements to the code in the corresponding class. I don't think it is
necessary to physically add the elements to the JDialog or JFrame as
that is done in the binding.
These 2 lines are showing the elements, if I don't use them I don't
see them. But the may explain why it seems like a new JList is being
added.
Here is my complete code for the class:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.util.Vector;
import java.util.Arrays;
public class CompareFile extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JScrollPane listCommonScrollPane;
private JPanel listPane;
private JButton buttonMissing;
private JButton buttonRemove;
private JButton buttonAdd;
private JList listAdded;
private JList listOriginal;
private JButton buttonCompare;
private JList list1;
private JList list2;
private JScrollPane listAddedScrollPane;
private JPanel contentPane2;
public CompareFile(File originalDirectory, File
localisedDirectory) {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonCompare);
setTitle("Java Property Common Files");
buttonCompare.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});
buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
});
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
fillList(originalDirectory, localisedDirectory);
pack();
setVisible(true);
}
private void onOK() {
// add your code here
dispose();
}
private void onCancel() {
// add your code here if necessary
dispose();
}
private void fillList(File original, File translated){
try{
Vector fileList = new Vector(Arrays.asList(original.list
()));
fileList.retainAll(Arrays.asList(translated.list()));
fileList.trimToSize();
listAdded = new JList(fileList);
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(this.listAdded);
System.out.println(original.list().length);
}
catch(Exception ex){
System.out.println(ex.toString());
}
for(int i = 0; i < listAdded.getModel().getSize(); i++) {
System.out.println(listAdded.getModel().getElementAt
(i));
}
}
}