This example program presents a list of users on a system in a list pane, and allows selection of one or more users.
/////////////////////////////////////////////////////////////////////////
//
// VUserList example. This program presents a list of users on
// a system in a list pane, and allows selection of one or more
// users.
//
// Command syntax:
// VUserListExample system
//
/////////////////////////////////////////////////////////////////////////
import com.ibm.as400.access.*;
import com.ibm.as400.vaccess.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VUserListExample
{
private static AS400ListPane listPane;
public static void main (String[] args)
{
// If a system is not specified, display help text and
// exit.
if (args.length != 1)
{
System.out.println("Usage: VUserListExample system");
return;
}
try
{
// Create an AS400 object. The system name is passed
// as the first command line argument.
AS400 system = new AS400 (args[0]);
// Create a VUserList. This represents a list of users
// displayed in the list pane.
VUserList userList = new VUserList (system);
// Create a frame.
JFrame f = new JFrame ("VUserList example");
// Create an error dialog adapter. This displays
// any errors to the user.
ErrorDialogAdapter errorHandler = new ErrorDialogAdapter (f);
// Create a list pane to display the user list.
// Use load to get the information from the server.
listPane = new AS400ListPane (userList);
listPane.addErrorListener (errorHandler);
listPane.load ();
// When the frame closes, report the selected
// users and exit.
f.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent event)
{
reportSelectedUsers ();
System.exit (0);
}
});
// Layout the frame with the list pane.
f.getContentPane ().setLayout (new BorderLayout ());
f.getContentPane ().add ("Center", listPane);
f.pack ();
f.show ();
}
catch (Exception e)
{
System.out.println ("Error: " + e.getMessage ());
System.exit (0);
}
}
private static void reportSelectedUsers ()
{
VObject[] selectedUsers = listPane.getSelectedObjects ();
if (selectedUsers.length == 0)
System.out.println ("No users were selected.");
else
{
System.out.println ("The selected users were:");
for (int i = 0; i < selectedUsers.length; ++i)
System.out.println (selectedUsers[i]);
}
}
}