Solved

JList not displaying

Posted on 2004-09-08
8
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

634 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