简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM