-2

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 !

mKorbel
  • 109,525
  • 20
  • 134
  • 319
JmRag
  • 1,443
  • 7
  • 19
  • 56
  • 1
    `panel_1.setBounds(26, 109, 629, 220);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 22 '14 at 13:25
  • 1
    *"Heres all the code that is related:"* For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 22 '14 at 13:26
  • @AndrewThompson if I comment out panel_1.setBounds(..); then the panel is not visible , is there any other function to adjust it? – JmRag Apr 22 '14 at 14:20
  • @AndrewThompson if I comment out the setborders function then I cannot see anything. Is there any other function to append? – JmRag Apr 22 '14 at 15:00
  • Append an MCVE if you want me to look further at that code. – Andrew Thompson Apr 23 '14 at 00:19

1 Answers1

2
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);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433