Search code examples
javaswingjpaneljscrollpanejlist

Dynamic Jlist Java Swing Problems


Hello I am trying to project a Jlist using word-builder plugin. Heres all the code that is related:

JPanel panel_1 = new JPanel();
panel_1.setBounds(26, 109, 629, 220);
MainFrame.getContentPane().add(panel_1);

final JList list = new JList();
panel_1.add(list);

JScrollPane scrollPane = new JScrollPane();
panel_1.add(scrollPane);

btnNewButton.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        DefaultListModel<String> model1=new DefaultListModel<String>();
        model1.addElement("hello");
        model1.addElement("hello");
        model1.addElement("hello");
        list.setModel(model1);
    }
});
btnNewButton.setBounds(364, 23, 89, 23);
MainFrame.getContentPane().add(btnNewButton);

but when I click the button this appears enter image description here

All in all I want the list to be located at the left side of UI instead of center and how can I customize it, font,colour,border etc.

Thx in advance !


Solution

  • panel_1.add(list);
    
    JScrollPane scrollPane = new JScrollPane();
    panel_1.add(scrollPane);
    

    Should better be:

    //panel_1.add(list);
    
    JScrollPane scrollPane = new JScrollPane(list);
    panel_1.add(scrollPane);