Solved

JList not displaying

Posted on 2004-09-08
8
246 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
 
LVL 37

Assisted Solution

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

    theScrollPane.setViewportView(list);
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now