3 import java.awt.event.*;
6 public class whoOnlineWindow extends JFrame {
7 DefaultListModel users;
11 public whoOnlineWindow() {
12 setTitle( "Who is online" );
14 JPanel p = new JPanel();
15 p.setLayout( new BorderLayout() );
16 p.setBorder( BorderFactory.createTitledBorder(
17 BorderFactory.createEtchedBorder(), "Users" ) );
19 theList = new JList();
20 users = new DefaultListModel();
21 theList.setModel( users );
23 theList.addMouseListener( new MouseAdapter() {
24 public void mouseClicked( MouseEvent e ) {
26 int i = theList.getSelectedIndex();
28 new pageUserWindow( unpad( (String)users.elementAt( i )) );
31 p.add( "Center", new JScrollPane( theList ) );
33 getContentPane().setLayout( new BorderLayout() );
34 getContentPane().add( "Center", p );
38 p.add( b = new JButton( "Refresh" ) );
39 b.addActionListener( new ActionListener() {
40 public void actionPerformed( ActionEvent e ) {
44 p.add( b = new JButton( "Close" ) );
45 b.addActionListener( new ActionListener() {
46 public void actionPerformed( ActionEvent e ) {
50 getContentPane().add( "South", p );
52 addWindowListener( new WindowAdapter() {
53 public void windowClosing( WindowEvent e ) {
57 citadel.me.registerWindow( this );
64 public void closeWin() {
65 citadel.me.removeWindow( this );
70 public void refresh() {
71 citadel.me.networkEvent( "RWHO", new CallBack() {
72 public void run( citReply r ) {
75 users.removeAllElements();
79 while( (s = r.getLine( i++ )) != null ) {
80 int j = s.indexOf( '|' ) + 1;
81 int k = s.indexOf( '|', j );
82 int l = s.indexOf( '|', k + 1 );
83 String user = s.substring( j, k );
84 String room = s.substring( k+1, l );
85 users.addElement( pad( user, room ) );
92 public String pad( String u, String r ) {
93 StringBuffer s = new StringBuffer( u );
94 while( s.length() < 30 )
100 public String unpad( String p ) {
101 StringBuffer s = new StringBuffer( p.substring( 0, Math.min( 29, p.length() ) ) );
102 while( s.charAt( s.length() - 1) == ' ' )
103 s.setLength( s.length() -1 );
111 class MyCellRenderer extends JLabel implements ListCellRenderer {
112 final static ImageIcon longIcon = new ImageIcon("long.gif");
113 final static ImageIcon shortIcon = new ImageIcon("short.gif");
115 // This is the only method defined by ListCellRenderer. We just
116 // reconfigure the Jlabel each time we're called.
118 public Component getListCellRendererComponent(
120 Object value, // value to display
121 int index, // cell index
122 boolean isSelected, // is the cell selected
123 boolean cellHasFocus) // the list and the cell have the focus
125 String s = value.toString();
127 setIcon((s.length() > 10) ? longIcon : shortIcon);
129 setBackground(list.getSelectionBackground());
130 setForeground(list.getSelectionForeground());
133 setBackground(list.getBackground());
134 setForeground(list.getForeground());
136 setEnabled(list.isEnabled());
137 setFont(list.getFont());
142 String[] data = {"one", "two", "free", "four"};
143 JList dataList = new JList(data);
144 dataList.setCellRenderer(new MyCellRenderer());