[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

JList not displaying

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
monica_w
Asked:
monica_w
3 Solutions
 
zzynxSoftware engineerCommented:
Where do you add the list to the scrollpane?
0
 
armoghanCommented:
You have not added list in Frame
0
 
objectsCommented:
> scrollPane = new JScrollPane();

scrollPane = new JScrollPane(list);
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zzynxSoftware engineerCommented:
Try adding

    theScrollPane.setViewportView(list);
0
 
armoghanCommented:
add
scrollPane.getViewPort().add(list)
0
 
Nick_72Commented:
Hmm, you haven't added the list to the scrollpane

scrollPane.getViewport().add(list);
0
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
Thanks for accepting
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now