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
Solved

JList not displaying

Posted on 2004-09-08
8
249 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 58
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 56
mysql jsp example issue 32 48
Eclipse Java import and method not resolved 4 45
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

792 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