Java Swing JList
Asked Answered
K

3

5

I am using a JList in Java Swing, but when my Dialog opens, the List isn't shown.

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;
 }

The list is defined:

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

  }
  return jContentPane;
 }

It's a JContentPane (/Panel)

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

the code calling getJContentPane():

private void initialize() {
      this.setSize(300, 200); 
      this.setContentPane(getJContentPane()); 
      this.setTitle("Auswahl"); 
} 
Kirkuk answered 25/1, 2010 at 15:57 Comment(3)
Are you not seeing the list box or only its contents? also, BTW, you are making your list "visible" before adding it to the pane.Roofing
JContentPane is visible? did you called pack()? (only guessing)Lura
i don't see the box (set.visible was just a test :P )Kirkuk
P
8

I can't find where you are setting the model of the JList?

Something like

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

Please have a look at the Code Conventions for the Java Programming Language

FensterDreiinstead of fensterdrei
myListinstead of mylist

Phasis answered 25/1, 2010 at 17:53 Comment(1)
hi you are right, private DefaultListModel model2 = new DefaultListModel (); so --> if (mylist == null) { mylist = new JList(MODEL);Kirkuk
C
4

It's getContentPane not getJContentPane, and You're not supposed to overload it.

Instead, in your constructor (or other function that gets called right away) you do

getContentPane().setLayout(new BorderLayout());
getContentPane().add(getJList(), BorderLayout.CENTER);
Contingency answered 25/1, 2010 at 16:3 Comment(1)
hi, does it make a different when the method name is getContentPane or getJContentPane ? i think it's just the methode-name, isn't it? like that? private void initialize() { this.setSize(300, 200); this.setContentPane(getJContentPane()); this.setTitle("Auswahl"); getContentPane().add(getJList(), BorderLayout.CENTER); getContentPane().add(getJList(), BorderLayout.CENTER); }Kirkuk
I
1

To answer your question I would need to see the code that calls getJContentPane to make sure that you are actually adding that JPanel somewhere. I would also need to see if you have assigned something to jContentPane since you only add the list if that panel is null.

My guess is that you are not actually adding the returned panel to the dialog or that jContentPane has been assigned a non null value.

The call to myList.setVisible(true) makes no sense since it is not added to a Window yet. When a dialog is made visible all its children will be made visible as well.

Ibis answered 25/1, 2010 at 16:24 Comment(3)
hi, private void initialize() { this.setSize(300, 200); this.setContentPane(getJContentPane()); this.setTitle("Auswahl"); } private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getJList(), BorderLayout.CENTER); } return jContentPane; } do you mean that?Kirkuk
@Kirkuk - please edit your question to add the code there. DankePhasis
hi, so the solution has been found, i don't edit the command, ok?Kirkuk

© 2022 - 2024 — McMap. All rights reserved.