Java Swing JList

I use JList in Java Swing, but when my dialog opens, the list is not displayed.

private JList getJList() { if (mylist == null) { mylist = new JList(); mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); mylist.setSize(new Dimension(154, 106)); model.addElement("test"); model.addElement("zwei"); mylist.setVisible(true); } return mylist; } 

Defined list:

 private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getJList(), BorderLayout.CENTER); } return jContentPane; } 

This is JContentPane (/ Panel)

 public fensterdrei(Frame owner) { super(owner); initialize(); } 

calling getJContentPane() code:

 private void initialize() { this.setSize(300, 200); this.setContentPane(getJContentPane()); this.setTitle("Auswahl"); } 
+4
source share
3 answers

I can not find where you are installing the JList model?

Sort of

 mylist = new JList(); mylist.setModel(model); 

Please read the code conventions for the Java programming language

FensterDrei instead of FensterDrei
myList instead of myList

+6
source

This getContentPane is not getJContentPane, and you should not overload it.

Instead, in your constructor (or other function called immediately) you execute

 getContentPane().setLayout(new BorderLayout()); getContentPane().add(getJList(), BorderLayout.CENTER); 
+4
source

To answer your question, I will need to see the code that getsJContentPane calls to make sure that you are actually adding JPanel. I will also need to see if jContentPane is assigned to you, since you only add a list if this panel is null.

My assumption is that you are not actually adding the returned panel to the dialog or that jContentPane is assigned a non-zero value.

Calling myList.setVisible (true) does not make sense, since it has not yet been added to the window. When a dialogue becomes visible, all his children will also be visible.

+1
source

Source: https://habr.com/ru/post/1299334/


All Articles