Bishal Gautam
Bishal Gautam

Reputation: 390

JList not showing array of string

initComponents();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();

DefaultListModel dlm = new DefaultListModel();
jList1 = new JList(dlm);
for (int i = 0; i < fonts.length; i++) {
    dlm.addElement(fonts[i]);
}
jList1.setSelectedIndex(0);
jList1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
jList1.setLayoutOrientation(JList.VERTICAL);
String s = (String) jList1.getSelectedValue();
System.out.println(s);

I have this code. jList1 is already dragged and dropped in the design view. The output of this code gives the value of the 0th index fonts. I have created a JFrame form and then added JPanel and JList on it. Do help me.

Upvotes: 0

Views: 98

Answers (1)

Beniton Fernando
Beniton Fernando

Reputation: 1533

As you have already initialized the list no need to create new list again.

        //jList1 = new JList(dlm);
        jList1.setModel(dlm);

You just need to set the model.

Upvotes: 4

Related Questions