
Untitled
By: a guest on
May 4th, 2012 | syntax:
None | size: 1.81 KB | hits: 12 | expires: Never
How to create Listview in Java? [closed]
import java.awt.*;
import javax.swing.*;
class ShowHtmlList {
public static void main(String[] args) {
final StringBuilder sb = new StringBuilder();
// invoke HTML renderring in HTML aware components
sb.append("<html>");
// start (the body &) an ordered list
sb.append("<body><ol>");
Font[] fonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
String name = font.getName();
// will be rendered using a *** ListView ***
sb.append("<li style='font-family: " +
name + "; font-size: 20px;'>");
sb.append(name);
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JScrollPane sp = new JScrollPane(
new JLabel(sb.toString()));
Dimension d = sp.getPreferredSize();
sp.setPreferredSize(new Dimension(d.width, 150));
JOptionPane.showMessageDialog(null, sp);
}
});
}
}
public class Sample extends JFrame {
public Sample(){
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setText("<ol id='foo'><li>One</li><li>Two</li></ol>");
HTMLDocument doc = (HTMLDocument) pane.getDocument();
add(pane);
//Get the ref of foo element
Element ele=doc.getElement("foo");
ListView view=new ListView(ele);
System.out.println(ele.getElementCount());
try{
doc.insertBeforeEnd(ele.getElement(0), "<ul><li>Test");
}catch(Exception ex){}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
}
}