Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Swing problem displaying JList elements in IntelliJ

  Asked By: Ella    Date: Mar 04    Category: Java    Views: 2390
  

I am having a problem displaying my JList elements using Idea
IntelliJ. I can create a JList, I can add the elements but for some
reason I can't visually see them on the screen. If I loop through
the JList and print the elements they are printed out.

So is there a particular command or the likes that I am missing. I
am trying to get a simple example working. Here is some code I have
tried,

String[] data = {"one", "two", "three", "four"};
this.list1 = new JList(data);
list1.setVisible(true);

and I have tried it this way as well,

listModel = new DefaultListModel();
listAdded = new JList(listModel);
listModel.addElement("Testproperties1");
listModel.addElement("Testproperties2");
listModel.addElement("Testproperties3");
listModel.addElement("Testproperties4");
listModel.addElement("Testproperties5");
listModel.addElement("Testproperties6");
listAdded.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

but each time the JList just appears blank on my screen.

Any suggestions or help appreciated.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Umaiza Hashmi     Answered On: Mar 04

In my example  im using a Vector to manipulate the
JList, u can add/remove/clear elements using methods
in Vector class & dont' forgot to call
JList.updateUI() method after modifying the source
Vector...

(By using vector u can dynamically add  or remove
items in runtime. Unlike arrays u can change the
size() of Vector in runtime, this will be helpful
while making dynamic JList or JComboBox)



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

public class JListDemo extends JFrame implements
ActionListener
{
Vector vStatic = new Vector();
Vector vData = new Vector();
JList list1 = new JList(vData);

JTextField jtfItemName = new JTextField(12);

JButton jbtnAdd = new JButton("Add Item");
JButton jbtnRefresh = new JButton("Load Static
Items");
JButton jbtnRemove = new JButton("Remove All Items");

public JListDemo()
{
super("JList Demo");

vStatic.add("One");
vStatic.add("Two");
vStatic.add("Three");
vStatic.add("Four");

JPanel jpnlNorth = new JPanel(new
FlowLayout(FlowLayout.LEFT));
jpnlNorth.add(new JLabel("Add a new Item to list"));
jpnlNorth.add(jtfItemName);
jpnlNorth.add(jbtnAdd);

JPanel jpnlSouth = new JPanel(new
FlowLayout(FlowLayout.CENTER));
jpnlSouth.add(jbtnRefresh);
jpnlSouth.add(jbtnRemove);

Container c = getContentPane();

c.add("North",jpnlNorth);
c.add("Center",new JScrollPane(list1));
c.add("South",jpnlSouth);
validate();
pack();
show();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jbtnAdd.addActionListener(this);
jbtnRefresh.addActionListener(this);
jbtnRemove.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == jbtnAdd)
{
vData.add(jtfItemName.getText().trim());
}
else if(ae.getSource() == jbtnRefresh)
{
vData.removeAllElements();
vData.addAll(vStatic);
}
else if(ae.getSource() == jbtnRemove)
{
vData.removeAllElements();
}


// DON'T FORGOT THESE LINES ARE IMPORTANT TO UPDATE
JList

vData.trimToSize();
list1.updateUI();
validate();
pack();
}

public static void main(String args[])
{
new JListDemo();
}
}

 
Answer #2    Answered By: Barachias Levi     Answered On: Mar 04

try with this tutorial

java.sun.com/.../list.html

and this isn't an intelliJ problem!

 
Answer #3    Answered By: Naomi Lee     Answered On: Mar 04

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));
}

}
}

 
Answer #4    Answered By: Bathilda Schmidt     Answered On: Mar 04

I emailed IntelliJ support and got the solution. I was using the
following:

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);

There was no need to initialise the JList as its done already in the
GUI designer so the code  should have been,

Vector fileList = new Vector(Arrays.asList(original.list()));
fileList.retainAll(Arrays.asList(translated.list()));
fileList.trimToSize();
listAdded.setListData(fileList);

 
Didn't find what you were looking for? Find more on Swing problem displaying JList elements in IntelliJ Or get search suggestion and latest updates.




Tagged: