Solved

JList not displaying

Posted on 2004-09-08
8
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 54
Error trying to install RTMT Win7 5 41
reverse digits of a number using for loop 5 36
arguments to jar 5 15
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

896 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

14 Experts available now in Live!

Get 1:1 Help Now