Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.81 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to create Listview in Java? [closed]
  2. import java.awt.*;
  3. import javax.swing.*;
  4.  
  5. class ShowHtmlList {
  6.     public static void main(String[] args) {
  7.         final StringBuilder sb = new StringBuilder();
  8.         // invoke HTML renderring in HTML aware components
  9.         sb.append("<html>");
  10.         // start (the body &) an ordered list
  11.         sb.append("<body><ol>");
  12.         Font[] fonts = GraphicsEnvironment.
  13.             getLocalGraphicsEnvironment().getAllFonts();
  14.         for (Font font : fonts) {
  15.             String name = font.getName();
  16.             // will be rendered using a *** ListView ***
  17.             sb.append("<li style='font-family: " +
  18.                 name + "; font-size: 20px;'>");
  19.             sb.append(name);
  20.         }
  21.         SwingUtilities.invokeLater(new Runnable() {
  22.             public void run() {
  23.                 JScrollPane sp = new JScrollPane(
  24.                     new JLabel(sb.toString()));
  25.                 Dimension d = sp.getPreferredSize();
  26.                 sp.setPreferredSize(new Dimension(d.width, 150));
  27.                 JOptionPane.showMessageDialog(null, sp);
  28.             }
  29.         });
  30.     }
  31. }
  32.        
  33. public class Sample extends JFrame   {
  34.     public Sample(){
  35.         JEditorPane pane = new JEditorPane();
  36.         pane.setContentType("text/html");
  37.         pane.setText("<ol id='foo'><li>One</li><li>Two</li></ol>");
  38.         HTMLDocument doc = (HTMLDocument) pane.getDocument();
  39.         add(pane);
  40.  
  41.         //Get the ref of foo element
  42.         Element ele=doc.getElement("foo");
  43.         ListView view=new ListView(ele);
  44.         System.out.println(ele.getElementCount());
  45.         try{
  46.              doc.insertBeforeEnd(ele.getElement(0), "<ul><li>Test");          
  47.         }catch(Exception ex){}
  48.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49.         setSize(300,300);
  50.         setVisible(true);
  51.     }
  52. }