Solved

JList not displaying

Posted on 2004-09-08
8
250 Views
Last Modified: 2010-03-31
I am using the following code to display a JList in a applet. But nothing appears

package swingprac;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TJList1 extends JApplet {

   JList list = null;
   MyListModel model = null;
   JScrollPane scrollPane = null;
   Container container = null;

   public void init() {
     container = this.getContentPane();
     container.setLayout(new GridLayout(1,1));
     model = new MyListModel();

     list = new JList(model);
     scrollPane = new JScrollPane();
     container.add(scrollPane);
     container.setVisible(true);
     list.setVisible(true);
     scrollPane.setVisible(true);
     this.setVisible(true);
     setVisible(true);
     show();
   }
}

class MyListModel extends AbstractListModel {
  int startYear = 1900;
  int endYear = 2000;
  int leapYear;

  public Object getElementAt(int index) {
    index = index + 1;
    if((startYear+index)%4==0)
      return "Year "+(startYear + index);
    else
      return null;
  }

  public int getSize() {
    return (endYear-startYear);
  }
}
0
Comment
Question by:monica_w
8 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12004994
Where do you add the list to the scrollpane?
0
 
LVL 18

Assisted Solution

by:armoghan
armoghan earned 20 total points
ID: 12004996
You have not added list in Frame
0
 
LVL 92

Accepted Solution

by:
objects earned 35 total points
ID: 12004998
> scrollPane = new JScrollPane();

scrollPane = new JScrollPane(list);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 20 total points
ID: 12005001
Try adding

    theScrollPane.setViewportView(list);
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12005002
add
scrollPane.getViewPort().add(list)
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 12005013
Hmm, you haven't added the list to the scrollpane

scrollPane.getViewport().add(list);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12005043
>> Where do you add the list to the scrollpane?
That was no real question of course ;°)
You don't.

In your code you should

1) pass the list in the scroll pane constructor  (cf. object's post)

OR

2) pass the list in the scrollpane's setViewportView() function

         scrollPane.setViewportView(list);

Both are valid an lead to the same result.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12005048
Thanks for accepting
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question