pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin - collaborative debugging tool View Help


Posted by Jack9 on Sun 13 Jul 06:40 (modification of post by view diff)
report abuse | download | new post

  1. import java.awt.Color;
  2. import java.awt.Component;
  3. import java.awt.Font;
  4. import java.awt.GraphicsEnvironment;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.io.Serializable;
  8.  
  9. import javax.swing.DefaultListCellRenderer;
  10. import javax.swing.DefaultListModel;
  11. import javax.swing.JFrame;
  12. import javax.swing.JList;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextField;
  15.  
  16. public class testMain
  17. {
  18.         public testMain()
  19.         {}
  20.        
  21.         public static void main(String args[])
  22.         {
  23.                 class MessageListItemModel implements Serializable
  24.                 {
  25.                         public static final long serialVersionUID = 0000000000000000001L;
  26.                        
  27.                         private String text;
  28.                         private Color textColor;
  29.                         private Font textFont;
  30.                        
  31.                         public MessageListItemModel(String text)
  32.                         {
  33.                                 this.text = text;
  34.                                 this.textColor = Color.BLUE;
  35.                                 this.textFont = getFont(2);
  36.                                 this.text = text;
  37.                         }
  38.  
  39.                         public final Color getTextColor() {
  40.                                 return textColor;
  41.                         }
  42.                      
  43.                         public final String getText() {
  44.                                 return text;
  45.                         }
  46.  
  47.                         public final Font getTextFont()
  48.                         {
  49.                                 return textFont;
  50.                         }
  51.  
  52.                         public Font getFont(int fnt)
  53.                         {
  54.                                 Font font = null;
  55.                                 switch(fnt)
  56.                                 {
  57.                                         case 2:
  58.                                                 font = new Font("Courier", Font.BOLD, 12);
  59.                                                 break;
  60.                                 }
  61.                                 return font;
  62.                         }       
  63.                 }
  64.  
  65.                 class MessageListItemView extends DefaultListCellRenderer// implements ListCellRenderer
  66.                 {
  67.                         public static final long serialVersionUID = 0000000000000000001L;
  68.                        
  69.                         public MessageListItemView ()
  70.                         {
  71.                         }
  72.  
  73.                         // Set the attributes of the
  74.                         //class and return a reference
  75.                         public Component getListCellRendererComponent(  JList list,   // datasource
  76.                                                                                                                 Object value, // value to display
  77.                                                                                                                 int index,    // cell index
  78.                                                                                                                 boolean iss,  // is selected
  79.                                                                                                                 boolean chf)  // cell has focus?
  80.                         {
  81.                                 // Set the text and color
  82.                                 setText(((MessageListItemModel)value).getText());
  83.                                 setForeground(((MessageListItemModel)value).getTextColor());
  84.                                 setBackground(Color.WHITE);
  85.                                 setFont(((MessageListItemModel)value).getTextFont());
  86.                                 setSize(this.getPreferredSize());
  87.                                 System.out.println("MessageListItemView index:"+index+" has bounds:"+this.getBounds().toString());
  88.                                 return this;
  89.                         }
  90.                 }
  91.                
  92.                 class GenericDisplay extends JFrame
  93.                 {
  94.                         public static final long serialVersionUID = 0000000000000000001L;
  95.                    
  96.                         public javax.swing.JList messageList;
  97.                     public javax.swing.JScrollPane messageListScrollPane;
  98.                     public javax.swing.JTextField inputTextField;
  99.                     protected MessageListItemView view;
  100.                    
  101.                         public GenericDisplay()
  102.                         {
  103.                                 setComponents();
  104.                                 setDefaults();
  105.                         }
  106.                        
  107.                         protected void setDefaults()
  108.                         {
  109.                         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  110.                        
  111.                                 // Maximization
  112.                                 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  113.                                 /*
  114.                                 The next line determines if the taskbar (win) is covered
  115.                                 if unremarked, the task will not be covered by
  116.                                 the maximized JFRAME.
  117.                                 */
  118.                                 setMaximizedBounds(env.getMaximumWindowBounds());
  119.                                 setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
  120.                                 pack();
  121.                         }
  122.                        
  123.                         protected void setComponents()
  124.                         {
  125.                                 getContentPane().setLayout(null);
  126.                                 inputTextField = new JTextField();
  127.                                 messageList = new JList(new DefaultListModel());
  128.                                 //messageList.setPrototypeCellValue(new MessageListItemModel("111111111111111111111111111111111111111111111111111"));
  129.                         messageList.setBorder(javax.swing.BorderFactory.createTitledBorder("Chat"));
  130.                         view = new MessageListItemView();
  131.                         messageList.setCellRenderer(view);
  132.                        
  133.                         //messageListModel = new DefaultListModel();
  134.                                 //messageList.setModel(messageListModel);
  135.                                 messageList.setVisibleRowCount(21);
  136.                                 messageListScrollPane = new JScrollPane();
  137.                         messageListScrollPane.getViewport().setView(messageList);
  138.                        
  139.                         messageList.setPrototypeCellValue(new MessageListItemModel("123-45-6789"));
  140.                         messageList.setFixedCellWidth(614);
  141.                                 getContentPane().setLayout(null);
  142.                         messageListScrollPane.setBounds(12,11,614,423);
  143.                         inputTextField.setBounds(12,446,614,20);
  144.                                 getContentPane().add(messageListScrollPane);
  145.                                 getContentPane().add(inputTextField);
  146.                                
  147.                                 inputTextField.addKeyListener(
  148.                                 new KeyListener()
  149.                                 {
  150.                                    public void keyPressed( KeyEvent e )
  151.                                    {
  152.                                            if (e.getKeyCode() == KeyEvent.VK_ENTER)
  153.                                            {
  154.                                                    addToModel(inputTextField.getText().trim());
  155.                                                    inputTextField.setText("");
  156.                                            }                       
  157.                                    }
  158.                                    public void keyTyped( KeyEvent e ){}
  159.                                    public void keyReleased( KeyEvent e ){}
  160.                                 }
  161.                              );
  162.                         }
  163.                         public void addToModel(String msg)
  164.                         {
  165.                                 ((DefaultListModel)messageList.getModel()).addElement(new MessageListItemModel(msg));   
  166.                                 System.out.println("Model len:"+((DefaultListModel)messageList.getModel()).getSize());
  167.                                 int lastIndex = (((DefaultListModel)messageList.getModel()).getSize());
  168.                                 if (lastIndex > 0)
  169.                                 {
  170.                                         System.out.println("Trying to ensure index is visible:"+lastIndex);
  171.                                         System.out.println("Should only see 1 MessageListItemView");
  172.                                         messageList.ensureIndexIsVisible(lastIndex);
  173.                                 }
  174.                         }
  175.                 }
  176.                
  177.                 GenericDisplay display = new GenericDisplay();
  178.                 display.setSize(800,600);
  179.                 display.setVisible(true);
  180.         }
  181. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me