Using JLists in Frame-based Applications

A JList, unlike a JComboBox, allows multiple selection. The list is not drop-down, so usually needs scrollbars as in the second example. The method getSelectedIndices returns an array of the indices of the selected items in the array that you passed to the JList constructor. We show the names currently selected in a JTextArea.

The code follows a screenshot.

Application in action

Application in action

The code in jlist_app.pas

namespace jlist_app1;

interface

uses
  java.util, java.awt.*, javax.swing.*;
   
type
  JListDemo = public class(JFrame, ListSelectionListener)
  private
    lstForenames : JList;
    taChosen : JTextArea;
    verticalBox : Box;
    forenames : array[0 .. 4] of String := ['Bradley', 'Jessica', 'Andy', 'Mo', 'Laura'];
   
  public
    constructor;
    class method Main(args : array of String); 
    method valueChanged(e : ListSelectionEvent);  
  end;

implementation

constructor JListDemo;
begin
  taChosen := new JTextArea;
  lstForenames := new JList(forenames);
  lstForenames.addListSelectionListener(self);
  verticalBox := Box.createVerticalBox;
  verticalBox.add(lstForenames);
  verticalBox.add(taChosen);
  add(verticalBox);

  //Ensure app closes when you click the close button
  DefaultCloseOperation := JFrame.EXIT_ON_CLOSE;
  //Set window caption and size
  Title := 'JList Demo';
  Size := new Dimension(250, 150);		
  setVisible(true); 
end;

class method JListDemo.Main(args : array of String);
begin
  new JListDemo;
end;

method JListDemo.valueChanged(e : ListSelectionEvent);
var
  selectedIndices : array of Integer;
  selectedForenames : String;
  i : Integer;
begin
  selectedForenames := '';
  if e.getSource = lstForenames then
    begin
      selectedIndices := lstForenames.getSelectedIndices;
      for i := 0 to length(selectedIndices) - 1 do
        selectedForenames := selectedForenames + forenames[selectedIndices[i]] + ' ';
      taChosen.setText(selectedForenames);
    end;
end;

end.

JList in a JScrollPane

This demonstration uses a horizontal Box and puts the JList in a JScrollPane with a vertical scrollbar only. The code follows a screenshot.

Application with JScrollBar in action

Application with JScrollBar in action

namespace jlist_app2;

interface

uses
  java.util, java.awt.*, javax.swing.*;
   
type
  JListDemo = public class(JFrame, ListSelectionListener)
  private
    lstForenames : JList;
    taChosen : JTextArea;
    horizontalBox : Box;
    sp : JScrollPane;
    forenames : array[0 .. 7] of String := ['Andy', 'Bradley', 'Chris', 'Greg', 'Jessica', 'Laura', 'Mo', 'Rebecca'];
   
  public
    constructor;
    class method Main(args : array of String); 
    method valueChanged(e : ListSelectionEvent);  
  end;

implementation

constructor JListDemo;
begin
  taChosen := new JTextArea;
  taChosen.setLineWrap(true);
  taChosen.setWrapStyleWord(true);
  lstForenames := new JList(forenames);
  lstForenames.addListSelectionListener(self);
  sp := new JScrollPane(lstForenames, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  horizontalBox := Box.createHorizontalBox;
  horizontalBox.add(sp);
  horizontalBox.add(taChosen);
  add(horizontalBox);

  //Ensure app closes when you click the close button
  DefaultCloseOperation := JFrame.EXIT_ON_CLOSE;
  //Set window caption and size
  Title := 'JList Demo 2';
  Size := new Dimension(230, 100);		
  setVisible(true); 
end;

class method JListDemo.Main(args : array of String);
begin
  new JListDemo;
end;

method JListDemo.valueChanged(e : ListSelectionEvent);
var
  selectedIndices : array of Integer;
  selectedForenames : String;
  i : Integer;
begin
  selectedForenames := '';
  if e.getSource = lstForenames then
    begin
      selectedIndices := lstForenames.getSelectedIndices;
      for i := 0 to length(selectedIndices) - 1 do
        selectedForenames := selectedForenames + forenames[selectedIndices[i]] + ' ';
      taChosen.setText(selectedForenames);
    end;
end;

end.

Programming - a skill for life!

How to use widgets such as JButtons, JRadioButtons, JLists, JTables and JDesktopPanes in Oxygene for Java Applications